|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | +) |
| 7 | + |
| 8 | +type APIKeyConfig struct { |
| 9 | + APIKey string |
| 10 | +} |
| 11 | + |
| 12 | +func (a *APIKeyConfig) addOption(h HTTP) HTTP { |
| 13 | + return &APIKeyAuthProvider{ |
| 14 | + apiKey: a.APIKey, |
| 15 | + HTTP: h, |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +type APIKeyAuthProvider struct { |
| 20 | + apiKey string |
| 21 | + |
| 22 | + HTTP |
| 23 | +} |
| 24 | + |
| 25 | +func (a *APIKeyAuthProvider) Get(ctx context.Context, path string, queryParams map[string]interface{}) (*http.Response, error) { |
| 26 | + return a.GetWithHeaders(ctx, path, queryParams, nil) |
| 27 | +} |
| 28 | + |
| 29 | +func (a *APIKeyAuthProvider) GetWithHeaders(ctx context.Context, path string, queryParams map[string]interface{}, |
| 30 | + headers map[string]string) (*http.Response, error) { |
| 31 | + setXApiKey(headers, a.apiKey) |
| 32 | + |
| 33 | + return a.HTTP.GetWithHeaders(ctx, path, queryParams, headers) |
| 34 | +} |
| 35 | + |
| 36 | +func (a *APIKeyAuthProvider) Post(ctx context.Context, path string, queryParams map[string]interface{}, |
| 37 | + body []byte) (*http.Response, error) { |
| 38 | + return a.PostWithHeaders(ctx, path, queryParams, body, nil) |
| 39 | +} |
| 40 | + |
| 41 | +func (a *APIKeyAuthProvider) PostWithHeaders(ctx context.Context, path string, queryParams map[string]interface{}, body []byte, |
| 42 | + headers map[string]string) (*http.Response, error) { |
| 43 | + setXApiKey(headers, a.apiKey) |
| 44 | + |
| 45 | + return a.HTTP.PostWithHeaders(ctx, path, queryParams, body, headers) |
| 46 | +} |
| 47 | + |
| 48 | +func (a *APIKeyAuthProvider) Put(ctx context.Context, api string, queryParams map[string]interface{}, body []byte) ( |
| 49 | + *http.Response, error) { |
| 50 | + return a.PutWithHeaders(ctx, api, queryParams, body, nil) |
| 51 | +} |
| 52 | + |
| 53 | +func (a *APIKeyAuthProvider) PutWithHeaders(ctx context.Context, path string, queryParams map[string]interface{}, body []byte, |
| 54 | + headers map[string]string) (*http.Response, error) { |
| 55 | + setXApiKey(headers, a.apiKey) |
| 56 | + |
| 57 | + return a.HTTP.PutWithHeaders(ctx, path, queryParams, body, headers) |
| 58 | +} |
| 59 | + |
| 60 | +func (a *APIKeyAuthProvider) Patch(ctx context.Context, path string, queryParams map[string]interface{}, body []byte) ( |
| 61 | + *http.Response, error) { |
| 62 | + return a.PatchWithHeaders(ctx, path, queryParams, body, nil) |
| 63 | +} |
| 64 | + |
| 65 | +func (a *APIKeyAuthProvider) PatchWithHeaders(ctx context.Context, path string, queryParams map[string]interface{}, body []byte, |
| 66 | + headers map[string]string) (*http.Response, error) { |
| 67 | + setXApiKey(headers, a.apiKey) |
| 68 | + |
| 69 | + return a.HTTP.PatchWithHeaders(ctx, path, queryParams, body, headers) |
| 70 | +} |
| 71 | + |
| 72 | +func (a *APIKeyAuthProvider) Delete(ctx context.Context, path string, body []byte) (*http.Response, error) { |
| 73 | + return a.DeleteWithHeaders(ctx, path, body, nil) |
| 74 | +} |
| 75 | + |
| 76 | +func (a *APIKeyAuthProvider) DeleteWithHeaders(ctx context.Context, path string, body []byte, headers map[string]string) ( |
| 77 | + *http.Response, error) { |
| 78 | + setXApiKey(headers, a.apiKey) |
| 79 | + |
| 80 | + return a.HTTP.DeleteWithHeaders(ctx, path, body, headers) |
| 81 | +} |
| 82 | + |
| 83 | +func setXApiKey(headers map[string]string, apiKey string) { |
| 84 | + if headers == nil { |
| 85 | + headers = make(map[string]string) |
| 86 | + } |
| 87 | + |
| 88 | + headers["X-API-KEY"] = apiKey |
| 89 | +} |
0 commit comments