forked from superfly/flyctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
158 lines (130 loc) · 3.98 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package api
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"regexp"
"strings"
genq "github.com/Khan/genqlient/graphql"
"github.com/superfly/graphql"
)
var (
baseURL string
errorLog bool
)
// SetBaseURL - Sets the base URL for the API
func SetBaseURL(url string) {
baseURL = url
}
// SetErrorLog - Sets whether errors should be loddes
func SetErrorLog(log bool) {
errorLog = log
}
// Client - API client encapsulating the http and GraphQL clients
type Client struct {
httpClient *http.Client
client *graphql.Client
GenqClient genq.Client
accessToken string
userAgent string
trace string
logger Logger
}
// NewClient - creates a new Client, takes an access token
func NewClient(accessToken, name, version string, logger Logger) *Client {
httpClient, _ := NewHTTPClient(logger, http.DefaultTransport)
url := fmt.Sprintf("%s/graphql", baseURL)
client := graphql.NewClient(url, graphql.WithHTTPClient(httpClient))
genqHttpClient, _ := NewHTTPClient(logger, &Transport{UnderlyingTransport: http.DefaultTransport, Token: accessToken, Ctx: context.Background()})
genqClient := genq.NewClient(url, genqHttpClient)
userAgent := fmt.Sprintf("%s/%s", name, version)
return &Client{httpClient, client, genqClient, accessToken, userAgent, os.Getenv("FLY_FORCE_TRACE"), logger}
}
// NewRequest - creates a new GraphQL request
func (*Client) NewRequest(q string) *graphql.Request {
q = compactQueryString(q)
return graphql.NewRequest(q)
}
// Run - Runs a GraphQL request
func (c *Client) Run(req *graphql.Request) (Query, error) {
return c.RunWithContext(context.Background(), req)
}
// RunWithContext - Runs a GraphQL request within a Go context
func (c *Client) RunWithContext(ctx context.Context, req *graphql.Request) (Query, error) {
req.Header.Set("Authorization", AuthorizationHeader(c.accessToken))
req.Header.Set("User-Agent", c.userAgent)
if c.trace != "" {
req.Header.Set("Fly-Force-Trace", c.trace)
}
var resp Query
err := c.client.Run(ctx, req, &resp)
if resp.Errors != nil && errorLog {
fmt.Fprintf(os.Stderr, "Error: %+v\n", resp.Errors)
}
return resp, err
}
var compactPattern = regexp.MustCompile(`\s+`)
func compactQueryString(q string) string {
q = strings.TrimSpace(q)
return compactPattern.ReplaceAllString(q, " ")
}
// GetAccessToken - uses email, password and possible otp to get token
func GetAccessToken(ctx context.Context, email, password, otp string) (token string, err error) {
var postData bytes.Buffer
if err = json.NewEncoder(&postData).Encode(map[string]interface{}{
"data": map[string]interface{}{
"attributes": map[string]string{
"email": email,
"password": password,
"otp": otp,
},
},
}); err != nil {
return
}
url := fmt.Sprintf("%s/api/v1/sessions", baseURL)
var req *http.Request
if req, err = http.NewRequestWithContext(ctx, http.MethodPost, url, &postData); err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
var res *http.Response
if res, err = http.DefaultClient.Do(req); err != nil {
return
}
defer func() {
closeErr := res.Body.Close()
if err == nil {
err = closeErr
}
}()
switch {
case res.StatusCode >= http.StatusInternalServerError:
err = errors.New("An unknown server error occurred, please try again")
case res.StatusCode >= http.StatusBadRequest:
err = errors.New("Incorrect email and password combination")
default:
var result map[string]map[string]map[string]string
if err = json.NewDecoder(res.Body).Decode(&result); err == nil {
token = result["data"]["attributes"]["access_token"]
}
}
return
}
type Transport struct {
UnderlyingTransport http.RoundTripper
Token string
Ctx context.Context
EnableDebugTrace bool
}
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("Authorization", AuthorizationHeader(t.Token))
if t.EnableDebugTrace {
req.Header.Add("Fly-Force-Trace", "true")
}
return t.UnderlyingTransport.RoundTrip(req)
}