diff --git a/client.go b/client.go index d783a58..0c36812 100644 --- a/client.go +++ b/client.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "net/http" "net/http/httptest" + "net/url" ) type HttpClient interface { @@ -34,14 +35,17 @@ func NewMockClient(handler http.HandlerFunc) *Client { } } -func (c Client) newRequest(ctx context.Context, method string, url string, body interface{}) (*http.Request, error) { +func (c Client) newRequest(ctx context.Context, method string, uri string, body interface{}) (*http.Request, error) { bodyJson, err := json.Marshal(body) if err != nil { return nil, err } - url = c.baseUrl + url - req, err := http.NewRequestWithContext(ctx, method, url, bytes.NewBuffer(bodyJson)) + uri, err = url.JoinPath(c.baseUrl, uri) + if err != nil { + return nil, err + } + req, err := http.NewRequestWithContext(ctx, method, uri, bytes.NewBuffer(bodyJson)) if err != nil { return nil, err } diff --git a/ozon/ozon.go b/ozon/ozon.go index b6a783a..5ee6c8a 100644 --- a/ozon/ozon.go +++ b/ozon/ozon.go @@ -10,6 +10,15 @@ const ( DefaultAPIBaseUrl = "https://api-seller.ozon.ru" ) +type ClientOptions struct { + client core.HttpClient + + baseUri string + + apiKey string + clientId string +} + type Client struct { client *core.Client @@ -110,10 +119,46 @@ func (c Client) Barcodes() *Barcodes { return c.barcodes } -func NewClient(httpClient core.HttpClient, clientId, apiKey string) *Client { - coreClient := core.NewClient(httpClient, DefaultAPIBaseUrl, map[string]string{ - "Client-Id": clientId, - "Api-Key": apiKey, +type ClientOption func(c *ClientOptions) + +func WithHttpClient(httpClient core.HttpClient) ClientOption { + return func(c *ClientOptions) { + c.client = httpClient + } +} + +func WithURI(uri string) ClientOption { + return func(c *ClientOptions) { + c.baseUri = uri + } +} + +func WithClientId(clientId string) ClientOption { + return func(c *ClientOptions) { + c.clientId = clientId + } +} + +func WithAPIKey(apiKey string) ClientOption { + return func(c *ClientOptions) { + c.apiKey = apiKey + } +} + +func NewClient(opts ...ClientOption) *Client { + // default values + options := &ClientOptions{ + client: http.DefaultClient, + baseUri: DefaultAPIBaseUrl, + } + + for _, opt := range opts { + opt(options) + } + + coreClient := core.NewClient(options.client, options.baseUri, map[string]string{ + "Client-Id": options.clientId, + "Api-Key": options.apiKey, }) return &Client{ diff --git a/ozon/ozon_test.go b/ozon/ozon_test.go new file mode 100644 index 0000000..794ccf0 --- /dev/null +++ b/ozon/ozon_test.go @@ -0,0 +1,27 @@ +package ozon + +import ( + "net/http" + "testing" +) + +const ( + apiKey = "some_key" + clientId = "some_client_id" +) + +func TestNewClient(t *testing.T) { + client := NewClient( + WithAPIKey(apiKey), + WithClientId(clientId), + WithURI(DefaultAPIBaseUrl), + WithHttpClient(http.DefaultClient), + ) + + if client.client.Options["Api-Key"] != apiKey { + t.Errorf("expected api key: %s, but got: %s", apiKey, client.client.Options["Api-Key"]) + } + if client.client.Options["Client-Id"] != clientId { + t.Errorf("expected client id: %s, but got: %s", clientId, client.client.Options["Client-Id"]) + } +}