-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
55 lines (46 loc) · 1.14 KB
/
http.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
package client
import (
"crypto/ecdsa"
"net/http"
"github.com/eteissonniere/app-store-connect-cli/helpers"
"github.com/rs/zerolog/log"
)
// A wrapper on `*http.Client` which adds the required headers for the App Store Connect API
// and offer QoL methods for making requests.
type Client struct {
*http.Client
config ClientConfig
}
// Metadata necessary to authenticate with the App Store Connect API.
type ClientConfig struct {
IssuerId string
BundleId string
KeyId string
PrivateKey *ecdsa.PrivateKey
UseSandbox bool
}
type transport struct {
config ClientConfig
rt http.RoundTripper
}
func (t *transport) RoundTrip(r *http.Request) (*http.Response, error) {
jwt, err := helpers.GenerateJWT(t.config.IssuerId, t.config.BundleId, t.config.KeyId, t.config.PrivateKey)
if err != nil {
return nil, err
}
log.Debug().
Str("jwt", jwt).
Msg("Roundtripping... JWT generated")
r.Header.Set("Authorization", "Bearer "+jwt)
return t.rt.RoundTrip(r)
}
func New(config ClientConfig) *Client {
return &Client{
Client: &http.Client{
Transport: &transport{
config: config,
rt: http.DefaultTransport,
},
},
}
}