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

Change function signature from rest.API to rest.Send. #19

Merged
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func main() {
Method: method,
BaseURL: baseURL,
}
response, err := rest.API(request)
response, err := rest.Send(request)
if err != nil {
fmt.Println(err)
} else {
Expand Down Expand Up @@ -117,7 +117,7 @@ func main() {
QueryParams: queryParams,
Body: Body,
}
response, err := rest.API(request)
response, err := rest.Send(request)
if err != nil {
fmt.Println(err)
} else {
Expand Down
12 changes: 6 additions & 6 deletions examples/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func main() {
Headers: Headers,
QueryParams: queryParams,
}
response, err := rest.API(request)
response, err := rest.Send(request)
if err != nil {
fmt.Println(err)
} else {
Expand All @@ -62,7 +62,7 @@ func main() {
QueryParams: queryParams,
Body: Body,
}
response, err = rest.API(request)
response, err = rest.Send(request)
if err != nil {
fmt.Println(err)
} else {
Expand Down Expand Up @@ -92,7 +92,7 @@ func main() {
BaseURL: baseURL + "/" + apiKey,
Headers: Headers,
}
response, err = rest.API(request)
response, err = rest.Send(request)
if err != nil {
fmt.Println(err)
} else {
Expand All @@ -113,7 +113,7 @@ func main() {
Headers: Headers,
Body: Body,
}
response, err = rest.API(request)
response, err = rest.Send(request)
if err != nil {
fmt.Println(err)
} else {
Expand All @@ -138,7 +138,7 @@ func main() {
Headers: Headers,
Body: Body,
}
response, err = rest.API(request)
response, err = rest.Send(request)
if err != nil {
fmt.Println(err)
} else {
Expand All @@ -156,7 +156,7 @@ func main() {
Headers: Headers,
QueryParams: queryParams,
}
response, err = rest.API(request)
response, err = rest.Send(request)
if err != nil {
fmt.Println(err)
} else {
Expand Down
11 changes: 8 additions & 3 deletions rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,14 @@ func BuildResponse(res *http.Response) (*Response, error) {
return &response, nil
}

// API is the main interface to the API.
// Function for support old implementation (deprecated)
func API(request Request) (*Response, error) {
return DefaultClient.API(request)
return Send(request)
}

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

// The following functions enable the ability to define a
Expand All @@ -120,7 +125,7 @@ func (c *Client) MakeRequest(req *http.Request) (*http.Response, error) {
}

// API is the main interface to the API.
func (c *Client) API(request Request) (*Response, error) {
func (c *Client) Send(request Request) (*Response, error) {
// Build the HTTP request object.
req, err := BuildRequestObject(request)
if err != nil {
Expand Down
11 changes: 9 additions & 2 deletions rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,16 @@ func TestBuildBadResponse(t *testing.T) {

func TestRest(t *testing.T) {
t.Parallel()
testingApi(t, Send)
testingApi(t, API)
}

func testingApi(t *testing.T, fn func(request Request) (*Response, error)) {
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 @@ -180,7 +186,8 @@ func TestRest(t *testing.T) {
fmt.Println("Request :", string(requestDump))
//End Print Request

response, e := API(request)
response, e := fn(request)

if response.StatusCode != 200 {
t.Error("Invalid status code")
}
Expand Down Expand Up @@ -264,7 +271,7 @@ func TestCustomHTTPClient(t *testing.T) {
}

customClient := &Client{&http.Client{Timeout: time.Millisecond * 10}}
_, err := customClient.API(request)
_, err := customClient.Send(request)
if err == nil {
t.Error("A timeout did not trigger as expected")
}
Expand Down