Skip to content

Commit

Permalink
chore: added another helper method to create response without body
Browse files Browse the repository at this point in the history
Signed-off-by: Bernd Warmuth <[email protected]>
  • Loading branch information
warber authored and jskelin committed Nov 11, 2024
1 parent 4a7af80 commit e19fc3c
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion api/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ func AsResponseOrError(resp *http.Response, err error) (*Response, error) {
return &response, nil
}

func NewResponseFromHTTPResponse(resp *http.Response) Response {
return Response{
Header: resp.Header,
StatusCode: resp.StatusCode,
Request: NewRequestInfoFromRequest(resp.Request),
}
}

func NewResponseFromHTTPResponseAndBody(resp *http.Response, body []byte) Response {
return Response{
Header: resp.Header,
Expand Down Expand Up @@ -190,8 +198,9 @@ func NewAPIErrorFromResponseAndBody(resp *http.Response, body []byte) APIError {
func NewAPIErrorFromResponse(resp *http.Response) error {
apiErr := APIError{
StatusCode: resp.StatusCode,
Request: rest.RequestInfo{Method: resp.Request.Method, URL: resp.Request.URL.String()},
Request: NewRequestInfoFromRequest(resp.Request),
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return errors.Join(apiErr, fmt.Errorf("unable to read API response body: %w", err))
Expand Down

0 comments on commit e19fc3c

Please sign in to comment.