Skip to content

Commit

Permalink
Improve client init (#35)
Browse files Browse the repository at this point in the history
* Improve client init

* Fix tests

* Update client.go
  • Loading branch information
vikmeup authored Jan 21, 2021
1 parent fe20c33 commit a77a394
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
4 changes: 2 additions & 2 deletions blockchain/binance/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type Client struct {
req client.Request
}

func InitClient(url, apiKey string) Client {
request := client.InitJSONClient(url)
func InitClient(url, apiKey string, errorHandler client.HttpErrorHandler) Client {
request := client.InitJSONClient(url, errorHandler)
request.AddHeader("apikey", apiKey)
return Client{
req: request,
Expand Down
37 changes: 19 additions & 18 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,37 @@ import (
)

type Request struct {
BaseUrl string
Headers map[string]string
HttpClient *http.Client
ErrorHandler func(res *http.Response, uri string) error
BaseUrl string
Headers map[string]string
HttpClient *http.Client
HttpErrorHandler HttpErrorHandler
}

type HttpErrorHandler func(res *http.Response, uri string) error

func (r *Request) SetTimeout(seconds time.Duration) {
r.HttpClient.Timeout = time.Second * seconds
}

func InitClient(baseUrl string) Request {
func InitClient(baseUrl string, errorHandler HttpErrorHandler) Request {
if errorHandler == nil {
errorHandler = DefaultErrorHandler
}
return Request{
Headers: make(map[string]string),
HttpClient: DefaultClient,
ErrorHandler: DefaultErrorHandler,
BaseUrl: baseUrl,
Headers: make(map[string]string),
HttpClient: DefaultClient,
HttpErrorHandler: errorHandler,
BaseUrl: baseUrl,
}
}

func InitJSONClient(baseUrl string) Request {
headers := map[string]string{
func InitJSONClient(baseUrl string, errorHandler HttpErrorHandler) Request {
client := InitClient(baseUrl, errorHandler)
client.Headers = map[string]string{
"Content-Type": "application/json",
"Accept": "application/json",
}
return Request{
Headers: headers,
HttpClient: DefaultClient,
ErrorHandler: DefaultErrorHandler,
BaseUrl: baseUrl,
}
return client
}

var DefaultClient = &http.Client{
Expand Down Expand Up @@ -112,7 +113,7 @@ func (r *Request) Execute(method string, url string, body io.Reader, result inte
return err
}

err = r.ErrorHandler(res, url)
err = r.HttpErrorHandler(res, url)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestRequest_GetBase(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := InitClient(tt.fields.baseUrl)
r := InitClient(tt.fields.baseUrl, nil)
if got := r.GetBase(tt.path); got != tt.want {
t.Errorf("Request.GetBase() = %v, want %v", got, tt.want)
}
Expand Down
2 changes: 1 addition & 1 deletion mock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestCreateMockedAPI(t *testing.T) {

server := httptest.NewServer(CreateMockedAPI(data))
defer server.Close()
client := client.InitClient(server.URL)
client := client.InitClient(server.URL, nil)

var resp response
err := client.Get(&resp, "1", nil)
Expand Down

0 comments on commit a77a394

Please sign in to comment.