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

Add ability to use custom HTTP client. #6

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 14 additions & 6 deletions rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ const (
Delete Method = "DELETE"
)

var defaultClient = &Client{HTTPClient: http.DefaultClient}

// Client performs API calls using its HTTP client.
type Client struct {
HTTPClient *http.Client
}

// Request holds the request to an API Call.
type Request struct {
Method Method
Expand Down Expand Up @@ -59,11 +66,7 @@ func BuildRequestObject(request Request) (*http.Request, error) {

// MakeRequest makes the API call.
func MakeRequest(req *http.Request) (*http.Response, error) {
var Client = &http.Client{
Transport: http.DefaultTransport,
}
res, err := Client.Do(req)
return res, err
return defaultClient.HTTPClient.Do(req)
}

// BuildResponse builds the response struct.
Expand All @@ -83,6 +86,11 @@ func BuildResponse(res *http.Response) (*Response, error) {

// API is the main interface to the API.
func API(request Request) (*Response, error) {
return defaultClient.API(request)
}

// API is the main interface to the API.
func (c *Client) API(request Request) (*Response, error) {
// Add any query parameters to the URL.
if len(request.QueryParams) != 0 {
request.BaseURL = AddQueryParameters(request.BaseURL, request.QueryParams)
Expand All @@ -95,7 +103,7 @@ func API(request Request) (*Response, error) {
}

// Build the HTTP client and make the request.
res, err := MakeRequest(req)
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
Expand Down
30 changes: 30 additions & 0 deletions rest_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package rest

import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)

Expand Down Expand Up @@ -47,6 +49,7 @@ func TestBuildResponse(t *testing.T) {
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "{\"message\": \"success\"}")
}))
defer fakeServer.Close()
baseURL := fakeServer.URL
method := Get
request := Request{
Expand Down Expand Up @@ -74,6 +77,7 @@ func TestRest(t *testing.T) {
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "{\"message\": \"success\"}")
}))
defer fakeServer.Close()
host := fakeServer.URL
endpoint := "/test_endpoint"
baseURL := host + endpoint
Expand Down Expand Up @@ -105,3 +109,29 @@ func TestRest(t *testing.T) {
t.Errorf("Rest failed to make a valid API request. Returned error: %v", e)
}
}

func TestCustomClient(t *testing.T) {
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "{\"message\": \"success\"}")
}))
defer fakeServer.Close()
wantErr := errors.New("pass")
c := &Client{&http.Client{Transport: errorTransport{wantErr}}}
_, err := c.API(Request{BaseURL: fakeServer.URL})
if err == nil {
t.Error("got nil err, want %q", wantErr)
}
if urlError, ok := err.(*url.Error); ok {
if urlError.Err != wantErr {
t.Errorf("err: got %#v, want %q", err, wantErr)
}
}
}

type errorTransport struct {
err error
}

func (t errorTransport) RoundTrip(r *http.Request) (*http.Response, error) {
return nil, t.err
}