|
| 1 | +// SPDX-FileCopyrightText: the secureCodeBox authors |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package hooksdk |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "encoding/json" |
| 10 | + "fmt" |
| 11 | + "io" |
| 12 | + "log/slog" |
| 13 | + "net/http" |
| 14 | + "strings" |
| 15 | +) |
| 16 | + |
| 17 | +type FileClient interface { |
| 18 | + DownloadText(ctx context.Context, url string) (string, error) |
| 19 | + DownloadJSON(ctx context.Context, url string, v any) error |
| 20 | + Upload(ctx context.Context, url string, contentType string, body []byte) error |
| 21 | +} |
| 22 | + |
| 23 | +type httpFileClient struct { |
| 24 | + client *http.Client |
| 25 | + logger *slog.Logger |
| 26 | +} |
| 27 | + |
| 28 | +func NewFileClient(logger *slog.Logger) FileClient { |
| 29 | + return &httpFileClient{client: &http.Client{}, logger: logger} |
| 30 | +} |
| 31 | + |
| 32 | +func (f *httpFileClient) DownloadText(ctx context.Context, url string) (string, error) { |
| 33 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) |
| 34 | + if err != nil { |
| 35 | + return "", fmt.Errorf("create request: %w", err) |
| 36 | + } |
| 37 | + resp, err := f.client.Do(req) |
| 38 | + if err != nil { |
| 39 | + return "", fmt.Errorf("download file: %w", err) |
| 40 | + } |
| 41 | + defer resp.Body.Close() |
| 42 | + if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices { |
| 43 | + body, _ := io.ReadAll(resp.Body) |
| 44 | + return "", fmt.Errorf("file download failed with status %d: %s", resp.StatusCode, body) |
| 45 | + } |
| 46 | + body, err := io.ReadAll(resp.Body) |
| 47 | + if err != nil { |
| 48 | + return "", fmt.Errorf("read response body: %w", err) |
| 49 | + } |
| 50 | + return string(body), nil |
| 51 | +} |
| 52 | + |
| 53 | +func (f *httpFileClient) DownloadJSON(ctx context.Context, url string, v any) error { |
| 54 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) |
| 55 | + if err != nil { |
| 56 | + return fmt.Errorf("create request: %w", err) |
| 57 | + } |
| 58 | + resp, err := f.client.Do(req) |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("download file: %w", err) |
| 61 | + } |
| 62 | + defer resp.Body.Close() |
| 63 | + if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices { |
| 64 | + body, _ := io.ReadAll(resp.Body) |
| 65 | + return fmt.Errorf("file download failed with status %d: %s", resp.StatusCode, body) |
| 66 | + } |
| 67 | + decoder := json.NewDecoder(resp.Body) |
| 68 | + decoder.DisallowUnknownFields() |
| 69 | + if err := decoder.Decode(v); err != nil { |
| 70 | + return fmt.Errorf("decode JSON: %w", err) |
| 71 | + } |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +func (f *httpFileClient) Upload(ctx context.Context, url string, contentType string, body []byte) error { |
| 76 | + req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, strings.NewReader(string(body))) |
| 77 | + if err != nil { |
| 78 | + return fmt.Errorf("create request: %w", err) |
| 79 | + } |
| 80 | + req.Header.Set("Content-Type", contentType) |
| 81 | + resp, err := f.client.Do(req) |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("upload file: %w", err) |
| 84 | + } |
| 85 | + defer resp.Body.Close() |
| 86 | + if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices { |
| 87 | + responseBody, _ := io.ReadAll(resp.Body) |
| 88 | + f.logger.Error("file upload failed", slog.Int("status_code", resp.StatusCode), slog.String("response_body", string(responseBody))) |
| 89 | + return fmt.Errorf("file upload failed with status %d: %s", resp.StatusCode, responseBody) |
| 90 | + } |
| 91 | + return nil |
| 92 | +} |
0 commit comments