Client options (#53)
This commit is contained in:
10
client.go
10
client.go
@@ -7,6 +7,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HttpClient interface {
|
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)
|
bodyJson, err := json.Marshal(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
url = c.baseUrl + url
|
uri, err = url.JoinPath(c.baseUrl, uri)
|
||||||
req, err := http.NewRequestWithContext(ctx, method, url, bytes.NewBuffer(bodyJson))
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req, err := http.NewRequestWithContext(ctx, method, uri, bytes.NewBuffer(bodyJson))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
53
ozon/ozon.go
53
ozon/ozon.go
@@ -10,6 +10,15 @@ const (
|
|||||||
DefaultAPIBaseUrl = "https://api-seller.ozon.ru"
|
DefaultAPIBaseUrl = "https://api-seller.ozon.ru"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ClientOptions struct {
|
||||||
|
client core.HttpClient
|
||||||
|
|
||||||
|
baseUri string
|
||||||
|
|
||||||
|
apiKey string
|
||||||
|
clientId string
|
||||||
|
}
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
client *core.Client
|
client *core.Client
|
||||||
|
|
||||||
@@ -110,10 +119,46 @@ func (c Client) Barcodes() *Barcodes {
|
|||||||
return c.barcodes
|
return c.barcodes
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(httpClient core.HttpClient, clientId, apiKey string) *Client {
|
type ClientOption func(c *ClientOptions)
|
||||||
coreClient := core.NewClient(httpClient, DefaultAPIBaseUrl, map[string]string{
|
|
||||||
"Client-Id": clientId,
|
func WithHttpClient(httpClient core.HttpClient) ClientOption {
|
||||||
"Api-Key": apiKey,
|
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{
|
return &Client{
|
||||||
|
|||||||
27
ozon/ozon_test.go
Normal file
27
ozon/ozon_test.go
Normal file
@@ -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"])
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user