generated from deploymenttheory/Template
-
Notifications
You must be signed in to change notification settings - Fork 0
Added function for file download from a url path #201
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// httpclient/download.go | ||
package httpclient | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
|
||
"github.com/deploymenttheory/go-api-http-client/authenticationhandler" | ||
"github.com/deploymenttheory/go-api-http-client/headers" | ||
"github.com/deploymenttheory/go-api-http-client/response" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// DoDownloadRequest performs a download from a given URL. It follows the same authentication, | ||
// header setting, and URL construction as the DoMultipartRequest function. The downloaded data | ||
// is written to the provided writer. | ||
// | ||
// Parameters: | ||
// - method: The HTTP method to use (e.g., GET). | ||
// - endpoint: The API endpoint from which the file will be downloaded. | ||
// - out: A writer where the downloaded data will be written. | ||
// | ||
// Returns: | ||
// - A pointer to the http.Response received from the server. | ||
// - An error if the request could not be sent or the response could not be processed. | ||
// | ||
// The function first validates the authentication token, constructs the full URL for | ||
// the request, sets the required headers (including Authorization), and sends the request. | ||
// | ||
// If debug mode is enabled, the function logs all the request headers before sending the request. | ||
// After the request is sent, the function checks the response status code. If the response is | ||
// not within the success range (200-299), it logs an error and returns the response and an error. | ||
// If the response is successful, the function writes the response body to the provided writer. | ||
// | ||
// Note: | ||
// The caller should handle closing the response body when successful. | ||
func (c *Client) DoDownloadRequest(method, endpoint string, out io.Writer) (*http.Response, error) { | ||
log := c.Logger | ||
|
||
// Auth Token validation check | ||
clientCredentials := authenticationhandler.ClientCredentials{ | ||
Username: c.clientConfig.Auth.Username, | ||
Password: c.clientConfig.Auth.Password, | ||
ClientID: c.clientConfig.Auth.ClientID, | ||
ClientSecret: c.clientConfig.Auth.ClientSecret, | ||
} | ||
|
||
valid, err := c.AuthTokenHandler.CheckAndRefreshAuthToken(c.APIHandler, c.httpClient, clientCredentials, c.clientConfig.ClientOptions.Timeout.TokenRefreshBufferPeriod.Duration()) | ||
if err != nil || !valid { | ||
return nil, err | ||
} | ||
|
||
// Construct URL using the ConstructAPIResourceEndpoint function | ||
url := c.APIHandler.ConstructAPIResourceEndpoint(endpoint, log) | ||
|
||
// Create the request | ||
req, err := http.NewRequest(method, url, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Initialize HeaderManager | ||
headerHandler := headers.NewHeaderHandler(req, c.Logger, c.APIHandler, c.AuthTokenHandler) | ||
|
||
// Use HeaderManager to set headers | ||
headerHandler.SetRequestHeaders(endpoint) | ||
headerHandler.LogHeaders(c.clientConfig.ClientOptions.Logging.HideSensitiveData) | ||
|
||
// Execute the request | ||
resp, err := c.httpClient.Do(req) | ||
if err != nil { | ||
log.Error("Failed to send request", zap.String("method", method), zap.String("endpoint", endpoint), zap.Error(err)) | ||
return nil, err | ||
} | ||
|
||
// Check for successful status code | ||
if resp.StatusCode < 200 || resp.StatusCode >= 300 { | ||
// Handle error responses | ||
return nil, response.HandleAPIErrorResponse(resp, log) | ||
} | ||
|
||
// Write the response body to the provided writer | ||
defer resp.Body.Close() | ||
if _, err := io.Copy(out, resp.Body); err != nil { | ||
return nil, err | ||
} | ||
|
||
return resp, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.