Skip to content

Commit

Permalink
refact(client.go): group config parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaly Bolychev committed Dec 17, 2018
1 parent dde1c2a commit 94d85de
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,23 @@ const (
)

type client struct {
url string
key string
signature string
config ClientConfig
httpClient *http.Client
}

func NewClient(url, key, signature string, httpClient *http.Client) Client {
type ClientConfig struct {
URL string
Key string
Signature string
}

func NewClient(cfg ClientConfig, httpClient *http.Client) Client {
if httpClient == nil {
httpClient = http.DefaultClient
}

return &client{
url: url,
key: key,
signature: signature,
config: cfg,
httpClient: httpClient,
}
}
Expand Down Expand Up @@ -68,13 +74,13 @@ func (c *client) SendSMS(ctx context.Context, msgReq *SendSMSRequest) (*SendSMSR
}

func (c *client) postUrlEncoded(ctx context.Context, path string, vals url.Values) (ret []byte, err error) {
u, err := url.Parse(c.url + path)
u, err := url.Parse(c.config.URL + path)
if err != nil {
return
}

vals.Add("api_key", c.key)
vals.Add("api_signature", c.signature)
vals.Add("api_key", c.config.Key)
vals.Add("api_signature", c.config.Signature)
vals.Add("api_format", "JSON")

req, err := http.NewRequest(http.MethodPost, u.String(), bytes.NewBuffer([]byte(vals.Encode())))
Expand Down

0 comments on commit 94d85de

Please sign in to comment.