/
metadata.go
54 lines (47 loc) · 1.24 KB
/
metadata.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
package metadata
import (
"fmt"
"net/http"
)
type Options struct {
HTTPClient HTTPClient
JWTAuthToken string
MetadataServiceURL string
}
// NewMetadata creates a new Sequence Metadata client instance. Please see
// https://sequence.build to get a `projectAccessKey`.
func NewMetadata(projectAccessKey string, options ...Options) Metadata {
opts := Options{}
if len(options) > 0 {
opts = options[0]
}
client := &httpclient{
client: opts.HTTPClient,
projectAccessKey: projectAccessKey,
}
if opts.HTTPClient == nil {
client.client = http.DefaultClient
}
if opts.JWTAuthToken != "" {
client.jwtAuthHeader = fmt.Sprintf("BEARER %s", opts.JWTAuthToken)
}
metadataServiceURL := "https://metadata.sequence.app"
if opts.MetadataServiceURL != "" {
metadataServiceURL = opts.MetadataServiceURL
}
return NewMetadataClient(metadataServiceURL, client)
}
type httpclient struct {
client HTTPClient
jwtAuthHeader string
projectAccessKey string
}
func (c *httpclient) Do(req *http.Request) (*http.Response, error) {
if c.projectAccessKey != "" {
req.Header.Set("X-Access-Key", c.projectAccessKey)
}
if c.jwtAuthHeader != "" {
req.Header.Set("Authorization", c.jwtAuthHeader)
}
return c.client.Do(req)
}