Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client options #53

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
)

type HttpClient interface {
Expand Down Expand Up @@ -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
}
Expand Down
53 changes: 49 additions & 4 deletions ozon/ozon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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{
Expand Down
27 changes: 27 additions & 0 deletions ozon/ozon_test.go
Original file line number Diff line number Diff line change
@@ -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"])
}
}
Loading