blob: b8f924c6314315791ee0353f5d1328dd91d3307b (
plain)
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
|
package coinbase
import (
"errors"
"net/http"
"time"
)
// ClientOAuthAuthentication Struct implements the Authentication interface
// and takes care of authenticating OAuth RPC requests on behalf of a client
// (i.e GetBalance())
type clientOAuthAuthentication struct {
Tokens *oauthTokens
BaseUrl string
Client http.Client
}
// ClientOAuth instantiates ClientOAuthAuthentication with the client OAuth tokens
func clientOAuth(tokens *oauthTokens) *clientOAuthAuthentication {
return clientOAuthWithEnv(tokens, false)
}
// ClientOAuthWithEnv instantiates ClientOAuthAuthentication with the client OAuth tokens and the specified environment
func clientOAuthWithEnv(tokens *oauthTokens, sandbox bool) *clientOAuthAuthentication {
baseUrl := "https://api.coinbase.com/v1/" // Live Url
// Check if should use sandbox
if sandbox {
baseUrl = "https://api.sandbox.coinbase.com/v1/" // Sandbox Url
}
a := clientOAuthAuthentication{
Tokens: tokens,
BaseUrl: baseUrl,
Client: http.Client{
Transport: &http.Transport{
Dial: dialTimeout,
},
},
}
return &a
}
// Client OAuth authentication requires us to attach an unexpired OAuth token to
// the request header
func (a clientOAuthAuthentication) authenticate(req *http.Request, endpoint string, params []byte) error {
// Ensure tokens havent expired
if time.Now().UTC().Unix() > a.Tokens.ExpireTime {
return errors.New("The OAuth tokens are expired. Use refreshTokens to refresh them")
}
req.Header.Set("Authorization", "Bearer "+a.Tokens.AccessToken)
return nil
}
func (a clientOAuthAuthentication) getBaseUrl() string {
return a.BaseUrl
}
func (a clientOAuthAuthentication) getClient() *http.Client {
return &a.Client
}
|