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

Add response code to common http_client #52

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 3 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
13 changes: 7 additions & 6 deletions common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ func ParseConnectorMetadata() (ConnectorMetadata, error) {
}

// HandleHTTPRequest sends message and headers data to HTTP endpoint using POST method and returns response on success or error in case of failure
func HandleHTTPRequest(message string, headers http.Header, data ConnectorMetadata, logger *zap.Logger) (*http.Response, error) {
func HandleHTTPRequest(message string, headers http.Header, data ConnectorMetadata, logger *zap.Logger) (int, *http.Response, error) {
ranoble marked this conversation as resolved.
Show resolved Hide resolved

var resp *http.Response
for attempt := 0; attempt <= data.MaxRetries; attempt++ {
// Create request
req, err := http.NewRequest("POST", data.HTTPEndpoint, strings.NewReader(message))
if err != nil {
return nil, errors.Wrapf(err, "failed to create HTTP request to invoke function. http_endpoint: %v, source: %v", data.HTTPEndpoint, data.SourceName)
// Request not sent.
return -1, nil, errors.Wrapf(err, "failed to create HTTP request to invoke function. http_endpoint: %v, source: %v", data.HTTPEndpoint, data.SourceName)
}

// Add headers
Expand All @@ -83,18 +84,18 @@ func HandleHTTPRequest(message string, headers http.Header, data ConnectorMetada
}
if err == nil && resp.StatusCode >= 200 && resp.StatusCode < 300 {
// Success, quit retrying
return resp, nil
return resp.StatusCode, resp, nil
}
}

if resp == nil {
return nil, fmt.Errorf("every function invocation retry failed; final retry gave empty response. http_endpoint: %v, source: %v", data.HTTPEndpoint, data.SourceName)
return -1, nil, fmt.Errorf("every function invocation retry failed; final retry gave empty response. http_endpoint: %v, source: %v", data.HTTPEndpoint, data.SourceName)
}

if resp.StatusCode < 200 || resp.StatusCode > 300 {
return nil, fmt.Errorf("request returned failure: %v. http_endpoint: %v, source: %v", resp.StatusCode, data.HTTPEndpoint, data.SourceName)
return resp.StatusCode, resp, fmt.Errorf("request returned failure: %v. http_endpoint: %v, source: %v", resp.StatusCode, data.HTTPEndpoint, data.SourceName)
}
return resp, nil
return resp.StatusCode, resp, nil
}

//GetAwsConfig get's the configuration required to connect to aws
Expand Down