-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcycle.go
More file actions
52 lines (43 loc) · 1.17 KB
/
cycle.go
File metadata and controls
52 lines (43 loc) · 1.17 KB
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
package cycle
import (
"context"
"net/http"
"github.com/oapi-codegen/oapi-codegen/v2/pkg/securityprovider"
)
// Function to combine multiple interceptors
func combineInterceptors(interceptors ...RequestEditorFn) RequestEditorFn {
return func(ctx context.Context, req *http.Request) error {
for _, interceptor := range interceptors {
if err := interceptor(ctx, req); err != nil {
return err
}
}
return nil
}
}
type ClientConfig struct {
APIKey string
HubID string
BaseURL *string
}
// generate a Cycle API client (with responses) that handles auth based on the provided token and hub
func NewAuthenticatedClient(config ClientConfig) (*ClientWithResponses, error) {
if config.BaseURL == nil {
baseUrl := "https://api.cycle.io"
config.BaseURL = &baseUrl
}
authBearer, err := securityprovider.NewSecurityProviderBearerToken(config.APIKey)
if err != nil {
return nil, err
}
authHub, err := securityprovider.NewSecurityProviderApiKey("header", "X-Hub-Id", config.HubID)
if err != nil {
return nil, err
}
return NewClientWithResponses(*config.BaseURL, WithRequestEditorFn(
combineInterceptors(
authBearer.Intercept,
authHub.Intercept,
),
))
}