-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
65 lines (52 loc) · 1.22 KB
/
Copy pathclient.go
File metadata and controls
65 lines (52 loc) · 1.22 KB
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
package shapeshift
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"github.com/pkg/errors"
)
const (
// BaseURL is an api endpoint
BaseURL = "https://shapeshift.io/%s"
)
// Client is an interface for a client.
type Client interface {
GetAPIClient
PostAPIClient
}
// New creates Client
func New() Client {
return &client{}
}
type client struct{}
func (c *client) do(ctx context.Context, method, path string, body io.Reader) (json.RawMessage, error) {
url := fmt.Sprintf(BaseURL, path)
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
req.WithContext(ctx)
req.Header.Set("Content-Type", "application/json")
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
const format = "failed to HTTP request with status code %d: %s"
msg, err := ioutil.ReadAll(resp.Body)
if err != nil {
msg = []byte("no error message returned from shapeshift server")
}
return nil, errors.Errorf(format, resp.StatusCode, msg)
}
var rawMsg json.RawMessage
if err := json.NewDecoder(resp.Body).Decode(&rawMsg); err != nil {
return nil, err
}
return rawMsg, nil
}