Skip to content

Commit

Permalink
Merge pull request #201 from deploymenttheory/dev
Browse files Browse the repository at this point in the history
Added function for file download from a url path
  • Loading branch information
ShocOne authored May 28, 2024
2 parents 5e99d5a + 4f51adc commit c96c66a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
89 changes: 89 additions & 0 deletions httpclient/downloadrequest.go
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
}
1 change: 0 additions & 1 deletion httpclient/multipartrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ func (c *Client) DoMultipartRequest(method, endpoint string, fields map[string]s
// Check for successful status code
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
// Handle error responses
//return nil, c.handleErrorResponse(resp, log, "Failed to process the HTTP request", method, endpoint)
return nil, response.HandleAPIErrorResponse(resp, log)
} else {
// Handle successful responses
Expand Down

0 comments on commit c96c66a

Please sign in to comment.