-
Notifications
You must be signed in to change notification settings - Fork 362
/
client.go
127 lines (101 loc) · 3.04 KB
/
client.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package stripeauth
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
log "github.com/sirupsen/logrus"
"github.com/stripe/stripe-cli/pkg/stripe"
)
const stripeCLISessionPath = "/v1/stripecli/sessions"
//
// Public types
//
// Config contains the optional configuration parameters of a Client.
type Config struct {
Log *log.Logger
}
// Client is the client used to initiate new CLI sessions with Stripe.
type Client struct {
client stripe.RequestPerformer
// Optional configuration parameters
cfg *Config
}
// DeviceURLMap is a mapping of the urls that the device is listening
// for forwarded events on.
type DeviceURLMap struct {
ForwardURL string
ForwardConnectURL string
}
// CreateSessionRequest defines the API input parameters for client.Authorize.
type CreateSessionRequest struct {
DeviceName string
WebSocketFeatures []string
Filters *string
DeviceURLMap *DeviceURLMap
}
// Authorize sends a request to Stripe to initiate a new CLI session.
func (c *Client) Authorize(ctx context.Context, req CreateSessionRequest) (*StripeCLISession, error) {
c.cfg.Log.WithFields(log.Fields{
"prefix": "stripeauth.client.Authorize",
}).Debug("Authenticating with Stripe...")
form := url.Values{}
form.Add("device_name", req.DeviceName)
for _, feature := range req.WebSocketFeatures {
form.Add("websocket_features[]", feature)
}
if req.Filters != nil {
form.Add("filters", *req.Filters)
}
if devURLMap := req.DeviceURLMap; devURLMap != nil {
if len(devURLMap.ForwardURL) > 0 {
form.Add("forward_to_url", devURLMap.ForwardURL)
}
if len(devURLMap.ForwardConnectURL) > 0 {
form.Add("forward_connect_to_url", devURLMap.ForwardConnectURL)
}
}
resp, err := c.client.PerformRequest(ctx, http.MethodPost, stripeCLISessionPath, form.Encode(), nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
err := fmt.Errorf("Authorization failed, status=%d, body=%s", resp.StatusCode, body)
return nil, err
}
var session *StripeCLISession
if err := json.Unmarshal(body, &session); err != nil {
return nil, err
}
c.cfg.Log.WithFields(log.Fields{
"prefix": "stripeauth.Client.Authorize",
"websocket_url": session.WebSocketURL,
"websocket_id": session.WebSocketID,
"websocket_authorized_feature": session.WebSocketAuthorizedFeature,
"reconnect_delay": session.ReconnectDelay,
"display_connect_filter_warning": session.DisplayConnectFilterWarning,
"default_version": session.DefaultVersion,
"latest_version": session.LatestVersion,
}).Debug("Got successful response from Stripe")
return session, nil
}
// NewClient returns a new Client.
func NewClient(client stripe.RequestPerformer, cfg *Config) *Client {
if cfg == nil {
cfg = &Config{}
}
if cfg.Log == nil {
cfg.Log = &log.Logger{Out: io.Discard}
}
return &Client{
client: client,
cfg: cfg,
}
}