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

Improve MakeGenericRequest to return response #65

Merged
merged 1 commit into from
May 18, 2024
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: 3 additions & 1 deletion babyapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1425,9 +1425,11 @@ func TestGetAllResponseWrapperWithClient(t *testing.T) {
require.NoError(t, err)

var albums AllAlbumsWrapper
err = client.MakeGenericRequest(req, &albums)
resp, err := client.MakeGenericRequest(req, &albums)
require.NoError(t, err)
require.Equal(t, "Album", albums[0].Title)
require.NotNil(t, resp.Data)
require.Equal(t, http.StatusOK, resp.Response.StatusCode)
})

t.Run("GetAllCLI", func(t *testing.T) {
Expand Down
18 changes: 12 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,27 +401,33 @@

// MakeGenericRequest allows making a request without specifying the return type. It accepts a pointer receiver
// to pass to json.Unmarshal. This allows returning any type using the Client.
func (c *Client[T]) MakeGenericRequest(req *http.Request, target any) error {
func (c *Client[T]) MakeGenericRequest(req *http.Request, target any) (*Response[any], error) {
resp, err := makeRequest(req, c.client, c.requestEditor)
if err != nil {
return err
return nil, err

Check warning on line 407 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L407

Added line #L407 was not covered by tests
}

result := &Response[any]{
Response: resp,
Data: target,
}

if resp.Body == nil {
return nil
return result, nil

Check warning on line 416 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L416

Added line #L416 was not covered by tests
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error decoding error response: %w", err)
return nil, fmt.Errorf("error decoding error response: %w", err)

Check warning on line 421 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L421

Added line #L421 was not covered by tests
}
result.Body = string(body)

err = json.Unmarshal(body, target)
if err != nil {
return fmt.Errorf("error decoding response body %q: %w", string(body), err)
return nil, fmt.Errorf("error decoding response body %q: %w", string(body), err)

Check warning on line 427 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L427

Added line #L427 was not covered by tests
}

return nil
return result, nil
}

// MakeRequest generically sends an HTTP request after calling the request editor and checks the response code
Expand Down
Loading