@@ -39,6 +39,18 @@ var userAgent = fmt.Sprintf(
3939
4040var errAPIKey = errors .New ("apiKey cannot be an empty string" )
4141
42+ // Option is a functional option for configuring the Client.
43+ type Option func (* Client )
44+
45+ // WithHTTPClient sets the underlying *http.Client used by the Client. This
46+ // allows callers to configure custom timeouts, proxies, transports, or
47+ // instrument the client with tracing (e.g. OpenTelemetry).
48+ func WithHTTPClient (httpClient * http.Client ) Option {
49+ return func (c * Client ) {
50+ c .c = httpClient
51+ }
52+ }
53+
4254// Client is the struct to represent the functionality presented by the
4355// https://ipdata.co API.
4456type Client struct {
@@ -47,34 +59,46 @@ type Client struct {
4759 k string // api key
4860}
4961
50- // NewClient takes an API key and returns a Client that uses the default
51- // endpoint (https://api.ipdata.co/).
52- func NewClient (apiKey string ) (Client , error ) {
62+ // NewClient takes an API key and optional Options, and returns a Client that
63+ // uses the default endpoint (https://api.ipdata.co/).
64+ func NewClient (apiKey string , opts ... Option ) (Client , error ) {
5365 if len (apiKey ) == 0 {
5466 return Client {}, errAPIKey
5567 }
5668
57- return Client {
69+ c := Client {
5870 c : newHTTPClient (),
5971 e : apiEndpoint ,
6072 k : apiKey ,
61- }, nil
73+ }
74+
75+ for _ , opt := range opts {
76+ opt (& c )
77+ }
78+
79+ return c , nil
6280}
6381
64- // NewEUClient takes an API key and returns a Client that uses the EU endpoint
65- // (https://eu-api.ipdata.co/). This ensures that all requests are routed
66- // through EU data centers only (Frankfurt, Paris, Ireland), which can be
67- // useful for GDPR compliance.
68- func NewEUClient (apiKey string ) (Client , error ) {
82+ // NewEUClient takes an API key and optional Options, and returns a Client that
83+ // uses the EU endpoint (https://eu-api.ipdata.co/). This ensures that all
84+ // requests are routed through EU data centers only (Frankfurt, Paris, Ireland),
85+ // which can be useful for GDPR compliance.
86+ func NewEUClient (apiKey string , opts ... Option ) (Client , error ) {
6987 if len (apiKey ) == 0 {
7088 return Client {}, errAPIKey
7189 }
7290
73- return Client {
91+ c := Client {
7492 c : newHTTPClient (),
7593 e : euAPIEndpoint ,
7694 k : apiKey ,
77- }, nil
95+ }
96+
97+ for _ , opt := range opts {
98+ opt (& c )
99+ }
100+
101+ return c , nil
78102}
79103
80104type apiErr struct {
0 commit comments