|
| 1 | +// httpclient/download.go |
| 2 | +package httpclient |
| 3 | + |
| 4 | +import ( |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/deploymenttheory/go-api-http-client/authenticationhandler" |
| 9 | + "github.com/deploymenttheory/go-api-http-client/headers" |
| 10 | + "github.com/deploymenttheory/go-api-http-client/response" |
| 11 | + "go.uber.org/zap" |
| 12 | +) |
| 13 | + |
| 14 | +// DoDownloadRequest performs a download from a given URL. It follows the same authentication, |
| 15 | +// header setting, and URL construction as the DoMultipartRequest function. The downloaded data |
| 16 | +// is written to the provided writer. |
| 17 | +// |
| 18 | +// Parameters: |
| 19 | +// - method: The HTTP method to use (e.g., GET). |
| 20 | +// - endpoint: The API endpoint from which the file will be downloaded. |
| 21 | +// - out: A writer where the downloaded data will be written. |
| 22 | +// |
| 23 | +// Returns: |
| 24 | +// - A pointer to the http.Response received from the server. |
| 25 | +// - An error if the request could not be sent or the response could not be processed. |
| 26 | +// |
| 27 | +// The function first validates the authentication token, constructs the full URL for |
| 28 | +// the request, sets the required headers (including Authorization), and sends the request. |
| 29 | +// |
| 30 | +// If debug mode is enabled, the function logs all the request headers before sending the request. |
| 31 | +// After the request is sent, the function checks the response status code. If the response is |
| 32 | +// not within the success range (200-299), it logs an error and returns the response and an error. |
| 33 | +// If the response is successful, the function writes the response body to the provided writer. |
| 34 | +// |
| 35 | +// Note: |
| 36 | +// The caller should handle closing the response body when successful. |
| 37 | +func (c *Client) DoDownloadRequest(method, endpoint string, out io.Writer) (*http.Response, error) { |
| 38 | + log := c.Logger |
| 39 | + |
| 40 | + // Auth Token validation check |
| 41 | + clientCredentials := authenticationhandler.ClientCredentials{ |
| 42 | + Username: c.clientConfig.Auth.Username, |
| 43 | + Password: c.clientConfig.Auth.Password, |
| 44 | + ClientID: c.clientConfig.Auth.ClientID, |
| 45 | + ClientSecret: c.clientConfig.Auth.ClientSecret, |
| 46 | + } |
| 47 | + |
| 48 | + valid, err := c.AuthTokenHandler.CheckAndRefreshAuthToken(c.APIHandler, c.httpClient, clientCredentials, c.clientConfig.ClientOptions.Timeout.TokenRefreshBufferPeriod.Duration()) |
| 49 | + if err != nil || !valid { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + // Construct URL using the ConstructAPIResourceEndpoint function |
| 54 | + url := c.APIHandler.ConstructAPIResourceEndpoint(endpoint, log) |
| 55 | + |
| 56 | + // Create the request |
| 57 | + req, err := http.NewRequest(method, url, nil) |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + |
| 62 | + // Initialize HeaderManager |
| 63 | + headerHandler := headers.NewHeaderHandler(req, c.Logger, c.APIHandler, c.AuthTokenHandler) |
| 64 | + |
| 65 | + // Use HeaderManager to set headers |
| 66 | + headerHandler.SetRequestHeaders(endpoint) |
| 67 | + headerHandler.LogHeaders(c.clientConfig.ClientOptions.Logging.HideSensitiveData) |
| 68 | + |
| 69 | + // Execute the request |
| 70 | + resp, err := c.httpClient.Do(req) |
| 71 | + if err != nil { |
| 72 | + log.Error("Failed to send request", zap.String("method", method), zap.String("endpoint", endpoint), zap.Error(err)) |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + |
| 76 | + // Check for successful status code |
| 77 | + if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
| 78 | + // Handle error responses |
| 79 | + return nil, response.HandleAPIErrorResponse(resp, log) |
| 80 | + } |
| 81 | + |
| 82 | + // Write the response body to the provided writer |
| 83 | + defer resp.Body.Close() |
| 84 | + if _, err := io.Copy(out, resp.Body); err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + |
| 88 | + return resp, nil |
| 89 | +} |
0 commit comments