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

S-108582 Improve http client error and redirection handling #58

Merged
merged 3 commits into from
May 3, 2024
Merged
Changes from 1 commit
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
45 changes: 29 additions & 16 deletions http/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,24 +110,12 @@ func (httpClient *HttpClient) sendRequest(ctx context.Context, method string, pa
})
}

// sendRequestWithCustomHeaders sends an HTTP request with a custom configuration and headers.
// sendRequestWithCustomHeaders sends an HTTP request with a custom configuration and headers, returns HTTP response body.
func (httpClient *HttpClient) sendRequestWithCustomHeaders(ctx context.Context, config *RequestConfig) ([]byte, error) {
client := httpClient.client
if client == nil {
return nil, fmt.Errorf("http client is uninitialized")
}
theUrl := httpClient.createUrl(config.Path, config.QueryParams...)
req, err := http.NewRequestWithContext(ctx, config.method, theUrl, bytes.NewBuffer(config.Body))
resp, err := httpClient.doSendHttpRequest(ctx, config)
if err != nil {
return nil, err
}
setHeaders(req, httpClient.headers)
setHeaders(req, config.Headers)

resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("%s error: %v", config.method, err)
}

defer func(Body io.ReadCloser) {
if deferredErr := Body.Close(); deferredErr != nil {
Expand All @@ -140,14 +128,39 @@ func (httpClient *HttpClient) sendRequestWithCustomHeaders(ctx context.Context,
return nil, fmt.Errorf("read body error: %v", err)
}

//TODO: handle 3xx statuses
if resp.StatusCode >= 299 {
if resp.StatusCode >= 300 && resp.StatusCode < 400 {
return nil, fmt.Errorf("%v redirect status: use sendHttpRequest instead to handle redirection", resp.StatusCode)
}

if resp.StatusCode >= 400 {
return data, fmt.Errorf("%v - %s", resp.StatusCode, string(data[:]))
}

return data, err
}

// doSendHttpRequest sends an HTTP request with a custom configuration and headers, returns whole HTTP response.
func (httpClient *HttpClient) doSendHttpRequest(ctx context.Context, config *RequestConfig) (*http.Response, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it DoSendHttpRequest so it's usable outside of the package

client := httpClient.client
if client == nil {
return nil, fmt.Errorf("http client is uninitialized")
}
theUrl := httpClient.createUrl(config.Path, config.QueryParams...)
req, err := http.NewRequestWithContext(ctx, config.method, theUrl, bytes.NewBuffer(config.Body))
if err != nil {
return nil, err
}
setHeaders(req, httpClient.headers)
setHeaders(req, config.Headers)

resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("%s error: %v", config.method, err)
}

return resp, nil
}

// setHeaders sets the headers of an HTTP request based on the provided map of headers.
func setHeaders(request *http.Request, headers map[string][]string) {
if headers != nil {
Expand Down
Loading