-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
79 lines (66 loc) · 1.81 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
package ziggy
import (
"log"
"net/url"
"testing"
"github.com/nickcorin/snorlax"
)
const defaultBaseURL = "https://openapi.investec.com"
// Client defines a stateful REST client wrapper for the Investec Open API.
type Client struct {
ClientID string
ClientSecret string
baseURL string
proxyURL *url.URL
token *AccessToken
transport *snorlax.Client
}
// New returns a Client configured with opts.
func NewClient(clientID, clientSecret string) *Client {
return &Client{
ClientID: clientID,
ClientSecret: clientSecret,
baseURL: defaultBaseURL,
proxyURL: nil,
token: nil,
transport: snorlax.DefaultClient.
AddRequestHook(snorlax.WithBasicAuth(clientID, clientSecret)).
AddRequestHook(snorlax.WithHeader("Accept", "application/json")).
AddRequestHook(snorlax.WithHeader("Content-Type",
"x-www-form-urlencoded")).
SetBaseURL(defaultBaseURL),
}
}
// NewClientForTesting returns a Ziggy client with a custom baseURL.
func NewClientForTesting(_ *testing.T, baseURL string) *Client {
client := NewClient("", "")
return client.setBaseURL(baseURL)
}
func (c *Client) setBaseURL(u string) *Client {
baseURL, err := url.Parse(u)
if err != nil {
// TODO: Add logs to indicate that this failed.
return c
}
c.transport.SetBaseURL(baseURL.String())
c.baseURL = baseURL.String()
return c
}
// SetToken sets a temporary authorization token on the Ziggy client.
func (c *Client) SetToken(token *AccessToken) *Client {
c.token = token
return c
}
// SetProxy sets the proxy URL for the Ziggy client.
func (c *Client) SetProxy(u string) *Client {
proxyURL, err := url.Parse(u)
if err != nil {
log.Println("I DIDN'T SET THE PROXY!")
// TODO: Add logs to indicate that this failed.
return c
}
log.Println("I DID SET THE PROXY!")
c.proxyURL = proxyURL
c.transport.SetProxy(u)
return c
}