-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgogocryptohopper.go
74 lines (63 loc) · 1.97 KB
/
gogocryptohopper.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
// Package gogocryptohopper contains the client implementation
package gogocryptohopper
import (
"encoding/json"
"errors"
"github.com/cryptwire/gogocryptohopper/internal"
"github.com/cryptwire/gogocryptohopper/internal/hopper"
"github.com/cryptwire/gogocryptohopper/internal/marketplace"
"github.com/cryptwire/gogocryptohopper/pkg"
)
// GoGoCryptohopper implements the JSON API for Cryptohopper
type GoGoCryptohopper struct {
// Hopper implements hopper related APIs
Hopper *hopper.Hopper
// Marketplace implements marketplace related APIs
Marketplace *marketplace.Marketplace
// client is the HTTP client used to communicate with the API
client *internal.Client
}
// New creates and returns new instance of the Cryptohopper API client
func New(
authenticationEndpoint string,
apiEndpoint string,
accessToken string,
) (*GoGoCryptohopper, error) {
headers := map[string]string{}
client, err := internal.NewClient(authenticationEndpoint, headers)
if err != nil {
return nil, err
}
hopperClient, err := hopper.New(apiEndpoint, accessToken)
if err != nil {
return nil, err
}
marketplaceClient, err := marketplace.New(apiEndpoint, accessToken)
if err != nil {
return nil, err
}
return &GoGoCryptohopper{
Hopper: hopperClient,
Marketplace: marketplaceClient,
client: client,
}, nil
}
// ExchangeGrantToken exchanges a given grant code to an access token
// Cryptohopper documentation: https://www.cryptohopper.com/api-documentation/authentication
func (ch *GoGoCryptohopper) ExchangeGrantToken(
request pkg.TokenExchangeRequest,
) (pkg.TokenExchangeResponse, error) {
var tokenResponse pkg.TokenExchangeResponse
response, err := ch.client.Post("oauth2/token", request)
if err != nil {
return tokenResponse, err
}
if response == nil {
return tokenResponse, errors.New("Empty body received, expected TokenExchangeResponse")
}
err = json.Unmarshal(response, &tokenResponse)
if err != nil {
return tokenResponse, err
}
return tokenResponse, nil
}