-
Notifications
You must be signed in to change notification settings - Fork 126
/
apikey_auth.go
93 lines (70 loc) · 2.98 KB
/
apikey_auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Package service provides an HTTP client with features for logging, metrics, and resilience.It supports various
// functionalities like health checks, circuit-breaker and various authentication.
package service
import (
"context"
"net/http"
)
type APIKeyConfig struct {
APIKey string
}
func (a *APIKeyConfig) AddOption(h HTTP) HTTP {
return &APIKeyAuthProvider{
apiKey: a.APIKey,
HTTP: h,
}
}
type APIKeyAuthProvider struct {
apiKey string
HTTP
}
func (a *APIKeyAuthProvider) Get(ctx context.Context, path string, queryParams map[string]interface{}) (*http.Response, error) {
return a.GetWithHeaders(ctx, path, queryParams, nil)
}
func (a *APIKeyAuthProvider) GetWithHeaders(ctx context.Context, path string, queryParams map[string]interface{},
headers map[string]string) (*http.Response, error) {
headers = setXApiKey(headers, a.apiKey)
return a.HTTP.GetWithHeaders(ctx, path, queryParams, headers)
}
func (a *APIKeyAuthProvider) Post(ctx context.Context, path string, queryParams map[string]interface{},
body []byte) (*http.Response, error) {
return a.PostWithHeaders(ctx, path, queryParams, body, nil)
}
func (a *APIKeyAuthProvider) PostWithHeaders(ctx context.Context, path string, queryParams map[string]interface{}, body []byte,
headers map[string]string) (*http.Response, error) {
headers = setXApiKey(headers, a.apiKey)
return a.HTTP.PostWithHeaders(ctx, path, queryParams, body, headers)
}
func (a *APIKeyAuthProvider) Put(ctx context.Context, api string, queryParams map[string]interface{}, body []byte) (
*http.Response, error) {
return a.PutWithHeaders(ctx, api, queryParams, body, nil)
}
func (a *APIKeyAuthProvider) PutWithHeaders(ctx context.Context, path string, queryParams map[string]interface{}, body []byte,
headers map[string]string) (*http.Response, error) {
headers = setXApiKey(headers, a.apiKey)
return a.HTTP.PutWithHeaders(ctx, path, queryParams, body, headers)
}
func (a *APIKeyAuthProvider) Patch(ctx context.Context, path string, queryParams map[string]interface{}, body []byte) (
*http.Response, error) {
return a.PatchWithHeaders(ctx, path, queryParams, body, nil)
}
func (a *APIKeyAuthProvider) PatchWithHeaders(ctx context.Context, path string, queryParams map[string]interface{}, body []byte,
headers map[string]string) (*http.Response, error) {
headers = setXApiKey(headers, a.apiKey)
return a.HTTP.PatchWithHeaders(ctx, path, queryParams, body, headers)
}
func (a *APIKeyAuthProvider) Delete(ctx context.Context, path string, body []byte) (*http.Response, error) {
return a.DeleteWithHeaders(ctx, path, body, nil)
}
func (a *APIKeyAuthProvider) DeleteWithHeaders(ctx context.Context, path string, body []byte, headers map[string]string) (
*http.Response, error) {
headers = setXApiKey(headers, a.apiKey)
return a.HTTP.DeleteWithHeaders(ctx, path, body, headers)
}
func setXApiKey(headers map[string]string, apiKey string) map[string]string {
if headers == nil {
headers = make(map[string]string)
}
headers["X-API-KEY"] = apiKey
return headers
}