Skip to content

Commit

Permalink
fix: clean up downloadFile
Browse files Browse the repository at this point in the history
  • Loading branch information
daulet committed Nov 5, 2024
1 parent 05a1570 commit 7de9a11
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions tokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,27 +173,18 @@ func downloadFile(url, destination string, authToken *string) error {
return nil
}

// Create the destination file
out, err := os.Create(destination)
if err != nil {
return fmt.Errorf("failed to create file %s: %w", destination, err)
}
defer out.Close()

// Create a new HTTP request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return fmt.Errorf("failed to create request for %s: %w", url, err)
}

// If authToken is provided, set the Authorization header
if authToken != nil && *authToken != "" {
if authToken != nil {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *authToken))
}

// Perform the HTTP request
client := &http.Client{}
resp, err := client.Do(req)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("failed to download from %s: %w", url, err)
}
Expand All @@ -204,6 +195,13 @@ func downloadFile(url, destination string, authToken *string) error {
return fmt.Errorf("failed to download from %s: status code %d", url, resp.StatusCode)
}

// Create the destination file
out, err := os.Create(destination)
if err != nil {
return fmt.Errorf("failed to create file %s: %w", destination, err)
}
defer out.Close()

// Write the response body to the file
_, err = io.Copy(out, resp.Body)
if err != nil {
Expand Down

0 comments on commit 7de9a11

Please sign in to comment.