|
| 1 | +package generic |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +type GenericAPI interface { |
| 12 | + GET(ctx context.Context, path string) (interface{}, *http.Response, error) |
| 13 | + POST(ctx context.Context, path string, body io.Reader) (interface{}, *http.Response, error) |
| 14 | +} |
| 15 | + |
| 16 | +// APIConfig defines the available configuration options |
| 17 | +// to customize the API client settings |
| 18 | +type Config struct { |
| 19 | + // HTTPClient is a custom HTTP client |
| 20 | + HTTPClient *http.Client |
| 21 | + // Debug enables debug-level logging |
| 22 | + Debug bool |
| 23 | + // BaseURL sets a custom API server base URL |
| 24 | + BaseURL string |
| 25 | +} |
| 26 | + |
| 27 | +func NewGenericAPIClient(cfg *Config) GenericAPI { |
| 28 | + if cfg.HTTPClient == nil { |
| 29 | + cfg.HTTPClient = http.DefaultClient |
| 30 | + } |
| 31 | + |
| 32 | + c := APIClient{ |
| 33 | + baseURL: cfg.BaseURL, |
| 34 | + httpClient: cfg.HTTPClient, |
| 35 | + } |
| 36 | + |
| 37 | + return &c |
| 38 | +} |
| 39 | + |
| 40 | +type APIClient struct { |
| 41 | + httpClient *http.Client |
| 42 | + baseURL string |
| 43 | +} |
| 44 | + |
| 45 | +func (c *APIClient) GET(ctx context.Context, path string) (interface{}, *http.Response, error) { |
| 46 | + url := c.baseURL + path |
| 47 | + |
| 48 | + req, err := http.NewRequest("GET", url, nil) |
| 49 | + if err != nil { |
| 50 | + return err, nil, err |
| 51 | + } |
| 52 | + |
| 53 | + req = req.WithContext(ctx) |
| 54 | + |
| 55 | + req.Header.Set("Accept", "application/json") |
| 56 | + |
| 57 | + resp, err := c.httpClient.Do(req) |
| 58 | + if err != nil { |
| 59 | + return nil, resp, err |
| 60 | + } |
| 61 | + if resp.StatusCode > http.StatusBadRequest { |
| 62 | + return nil, resp, errors.New(resp.Status) |
| 63 | + } |
| 64 | + defer resp.Body.Close() |
| 65 | + |
| 66 | + b, err := io.ReadAll(resp.Body) |
| 67 | + if err != nil { |
| 68 | + return nil, resp, err |
| 69 | + } |
| 70 | + |
| 71 | + return string(b), resp, err |
| 72 | +} |
| 73 | + |
| 74 | +func (c *APIClient) POST(ctx context.Context, path string, body io.Reader) (interface{}, *http.Response, error) { |
| 75 | + url := c.baseURL + path |
| 76 | + bodyBinary, err := io.ReadAll(body) |
| 77 | + |
| 78 | + if err != nil { |
| 79 | + return err, nil, err |
| 80 | + } |
| 81 | + requestBody := strings.NewReader(string(bodyBinary)) |
| 82 | + req, err := http.NewRequest("POST", url, requestBody) |
| 83 | + if err != nil { |
| 84 | + return nil, nil, err |
| 85 | + } |
| 86 | + |
| 87 | + req = req.WithContext(ctx) |
| 88 | + req.Header.Set("Content-Type", "application/json") |
| 89 | + req.Header.Set("Accept", "application/json") |
| 90 | + |
| 91 | + resp, err := c.httpClient.Do(req) |
| 92 | + if err != nil { |
| 93 | + return nil, resp, err |
| 94 | + } |
| 95 | + if resp.StatusCode > http.StatusBadRequest { |
| 96 | + return nil, resp, errors.New(resp.Status) |
| 97 | + } |
| 98 | + defer resp.Body.Close() |
| 99 | + |
| 100 | + b, err := io.ReadAll(resp.Body) |
| 101 | + if err != nil { |
| 102 | + return nil, resp, err |
| 103 | + } |
| 104 | + |
| 105 | + return string(b), resp, err |
| 106 | +} |
0 commit comments