-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.go
More file actions
50 lines (42 loc) · 1.43 KB
/
api.go
File metadata and controls
50 lines (42 loc) · 1.43 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
package aboutmyemail
import (
"context"
"net/http"
"os"
)
const apiEndpoint = "https://api.aboutmy.email/api/v1"
const envMyemailServer = "MYEMAIL_SERVER"
const envMyemailApikey = "MYEMAIL_APIKEY"
//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen@latest --config=api-model.cfg.yaml ameapi.yaml
//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen@latest --config=api-client.cfg.yaml ameapi.yaml
// New creates a new ClientWithResponses for AboutMy.email, with reasonable defaults
func New(opts ...ClientOption) (*ClientWithResponses, error) {
if server := os.Getenv(envMyemailServer); server != "" {
opts = append([]ClientOption{WithServer(server)}, opts...)
}
if apikey := os.Getenv(envMyemailApikey); apikey != "" {
opts = append([]ClientOption{WithApiKey(apikey)}, opts...)
}
return NewClientWithResponses(apiEndpoint, opts...)
}
func doNothing(*Client) error {
return nil
}
// WithApiKey sets the client Authorization header with this key, if key isn't empty
func WithApiKey(key string) ClientOption {
if key == "" {
return doNothing
}
authData := "Bearer " + key
return WithRequestEditorFn(func(ctx context.Context, r *http.Request) error {
r.Header.Set("Authorization", authData)
return nil
})
}
// WithServer sets the client API endpoint, if endpoint isn't empty
func WithServer(endpoint string) ClientOption {
if endpoint == "" {
return doNothing
}
return WithBaseURL(endpoint)
}