@@ -22,24 +22,20 @@ type Translator struct {
2222// TranslatorOption is a functional option for configuring the Translator
2323type TranslatorOption func (* Translator ) error
2424
25- // ServerURL allows overriding the default server url
26- func ServerURL (url string ) TranslatorOption {
25+ // WithServerURL allows overriding the default server url
26+ func WithServerURL (url string ) TranslatorOption {
2727 return func (t * Translator ) error {
2828 t .serverURL = url
2929 return nil
3030 }
3131}
3232
33- // parseOptions apllies the supplied functional options to the Translator
34- func (t * Translator ) parseOptions (opts ... TranslatorOption ) error {
35- for _ , opt := range opts {
36- err := opt (t )
37- if err != nil {
38- return err
39- }
33+ // WithHTTPClient allows overriding the default http client
34+ func WithHTTPClient (c HTTPClient ) TranslatorOption {
35+ return func (t * Translator ) error {
36+ t .client = c
37+ return nil
4038 }
41-
42- return nil
4339}
4440
4541// NewTranslator creates a new translator
@@ -52,25 +48,34 @@ func NewTranslator(authKey string, opts ...TranslatorOption) (*Translator, error
5248 serverURL = ServerURLPro
5349 }
5450
55- // Set up default http client
56- timeout := time .Second * 30
57-
51+ // Set up with default http client
5852 t := & Translator {
5953 client : & http.Client {
60- Timeout : timeout ,
54+ Timeout : 10 * time . Second ,
6155 },
6256 serverURL : serverURL ,
6357 authKey : authKey ,
6458 }
6559
66- // Parse and apply options
67- if err := t .parseOptions (opts ... ); err != nil {
60+ if err := t .applyOptions (opts ... ); err != nil {
6861 return nil , err
6962 }
7063
7164 return t , nil
7265}
7366
67+ // applyOptions apllies the supplied functional options to the Translator
68+ func (t * Translator ) applyOptions (opts ... TranslatorOption ) error {
69+ for _ , opt := range opts {
70+ err := opt (t )
71+ if err != nil {
72+ return err
73+ }
74+ }
75+
76+ return nil
77+ }
78+
7479// callAPI calls the supplied API endpoint with the provided parameters and returns the response
7580func (t * Translator ) callAPI (method string , endpoint string , data url.Values , headers http.Header ) (* http.Response , error ) {
7681 url := fmt .Sprintf ("%s/%s" , t .serverURL , endpoint )
0 commit comments