Skip to content

Commit

Permalink
make APIResponse match more like http.Response
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobbednarz committed Aug 25, 2022
1 parent faf1303 commit b0f5893
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,16 @@ func (api *API) makeRequestWithAuthType(ctx context.Context, method, uri string,
return api.makeRequestWithAuthTypeAndHeaders(ctx, method, uri, params, authType, nil)
}

// Struct for makeRequestWithAuthTypeAndHeaders's return value.
// Wrapper methods can return just the Body if that's all the caller needs.
// APIResponse holds the structure for a response from the API. It looks alot
// like `http.Response` however, uses a `[]byte` for the `Body` instead of a
// `io.ReadCloser`.
//
// Allows callers to see the Content-Type header and HTTP Status codes in case
// there is additional meaning for particular API calls. This is especially
// useful if the response is a multipart/form-data because the boundary string
// is stored as a parameter of the content type header. Caller can parse using mime/multipart.
// This may go away in the experimental client in favour of `http.Response`.
type APIResponse struct {
Body []byte
ContentType string
StatusCode int
Body []byte
Status string
StatusCode int
Headers http.Header
}

func (api *API) makeRequestWithAuthTypeAndHeaders(ctx context.Context, method, uri string, params interface{}, authType int, headers http.Header) ([]byte, error) {
Expand Down Expand Up @@ -298,7 +297,7 @@ func (api *API) makeRequestWithAuthTypeAndHeadersComplete(ctx context.Context, m
}
break
}
} // for loop
}

// still had an error after all retries
if respErr != nil {
Expand Down Expand Up @@ -365,9 +364,10 @@ func (api *API) makeRequestWithAuthTypeAndHeadersComplete(ctx context.Context, m
}

return &APIResponse{
Body: respBody,
StatusCode: resp.StatusCode,
ContentType: resp.Header.Get("content-type"),
Body: respBody,
StatusCode: resp.StatusCode,
Status: resp.Status,
Headers: resp.Header,
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func (api *API) downloadWorkerWithName(ctx context.Context, scriptName string) (
}

// Check if the response type is multipart, in which case this was a module worker
mediaType, mediaParams, _ := mime.ParseMediaType(res.ContentType)
mediaType, mediaParams, _ := mime.ParseMediaType(res.Headers.Get("content-type"))
if strings.HasPrefix(mediaType, "multipart/") {
bytesReader := bytes.NewReader(res.Body)
mimeReader := multipart.NewReader(bytesReader, mediaParams["boundary"])
Expand Down

0 comments on commit b0f5893

Please sign in to comment.