|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +// PaginationInfo holds page metadata for list responses. |
| 13 | +type PaginationInfo struct { |
| 14 | + Total int `json:"total"` |
| 15 | + Limit int `json:"limit"` |
| 16 | + Offset int `json:"offset"` |
| 17 | +} |
| 18 | + |
| 19 | +// HTTPConfigForTimeout returns an HTTP client config with the given timeout and no connection pooling. |
| 20 | +func HTTPConfigForTimeout(timeout time.Duration) HTTPClientConfig { |
| 21 | + return HTTPClientConfig{ |
| 22 | + Timeout: timeout, |
| 23 | + InsecureSkipVerify: true, |
| 24 | + PoolConnections: false, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// DeleteURL sends a DELETE request and accepts 200 or 204 as success. |
| 29 | +func DeleteURL(ctx context.Context, rawURL string, config HTTPClientConfig) error { |
| 30 | + _, statusCode, err := DoDELETE(ctx, rawURL, config) |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + |
| 35 | + if statusCode != http.StatusOK && statusCode != http.StatusNoContent { |
| 36 | + return fmt.Errorf("unexpected status %d", statusCode) |
| 37 | + } |
| 38 | + |
| 39 | + return nil |
| 40 | +} |
| 41 | + |
| 42 | +// AppendQueryParams returns rawURL with the non-empty params added as an encoded query string. |
| 43 | +func AppendQueryParams(rawURL string, params map[string]string) string { |
| 44 | + query := url.Values{} |
| 45 | + |
| 46 | + for key, val := range params { |
| 47 | + if val != "" { |
| 48 | + query.Set(key, val) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if len(query) == 0 { |
| 53 | + return rawURL |
| 54 | + } |
| 55 | + |
| 56 | + return rawURL + "?" + query.Encode() |
| 57 | +} |
| 58 | + |
| 59 | +// GetJSON sends a GET request, requires HTTP 200 and decodes the JSON body into a T. |
| 60 | +func GetJSON[T any](ctx context.Context, rawURL string, config HTTPClientConfig) (*T, error) { |
| 61 | + respBody, statusCode, err := DoGET(ctx, rawURL, config) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + |
| 66 | + if statusCode != http.StatusOK { |
| 67 | + return nil, fmt.Errorf("unexpected status %d: %s", statusCode, string(respBody)) |
| 68 | + } |
| 69 | + |
| 70 | + var out T |
| 71 | + if err := json.Unmarshal(respBody, &out); err != nil { |
| 72 | + return nil, fmt.Errorf("failed to parse response: %w", err) |
| 73 | + } |
| 74 | + |
| 75 | + return &out, nil |
| 76 | +} |
| 77 | + |
| 78 | +// FieldHasValue reports whether data has key set to a non-nil, non-empty-string value. |
| 79 | +func FieldHasValue(data map[string]interface{}, key string) bool { |
| 80 | + val, ok := data[key] |
| 81 | + if !ok || val == nil { |
| 82 | + return false |
| 83 | + } |
| 84 | + |
| 85 | + if s, isStr := val.(string); isStr { |
| 86 | + return s != "" |
| 87 | + } |
| 88 | + |
| 89 | + return true |
| 90 | +} |
| 91 | + |
| 92 | +// AllFieldsPresent reports whether every key in expected exists in data. |
| 93 | +// When one is missing it returns that key and false. |
| 94 | +func AllFieldsPresent(data map[string]interface{}, expected []string) (string, bool) { |
| 95 | + for _, key := range expected { |
| 96 | + if _, ok := data[key]; !ok { |
| 97 | + return key, false |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return "", true |
| 102 | +} |
0 commit comments