-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.go
122 lines (98 loc) · 2.62 KB
/
rest.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
// Copyright 2020 Self Group Ltd. All Rights Reserved.
package transport
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"golang.org/x/crypto/ed25519"
)
// RestConfig configuration for connecting to selfs api
type RestConfig struct {
SelfID string
KeyID string
PrivateKey ed25519.PrivateKey
APIURL string
Client *http.Client
}
// Rest client for interacting with self's rest api
type Rest struct {
config RestConfig
}
// NewRest creates a new rest transport
func NewRest(config RestConfig) (*Rest, error) {
return &Rest{config}, nil
}
// Get perform an http get request
func (c *Rest) Get(path string) ([]byte, error) {
return c.request("GET", path, nil, nil)
}
// Post perform an http post request
func (c *Rest) Post(path string, ctype string, data []byte) ([]byte, error) {
headers := map[string]string{"Content-Type": ctype}
return c.request("POST", path, data, headers)
}
// Put perform an http put request
func (c *Rest) Put(path string, ctype string, data []byte) ([]byte, error) {
headers := map[string]string{"Content-Type": ctype}
return c.request("PUT", path, data, headers)
}
// Delete perform an http delete request
func (c *Rest) Delete(path string) ([]byte, error) {
return c.request("DELETE", path, nil, nil)
}
// BuildURL builds a url for the given path.
func (c *Rest) BuildURL(path string) string {
return c.config.APIURL + path
}
// Request make a raw request to the self api
func (c *Rest) request(method, path string, data []byte, headers map[string]string) ([]byte, error) {
u, err := url.Parse(c.config.APIURL + path)
if err != nil {
return nil, err
}
req, err := http.NewRequest(method, u.String(), bytes.NewBuffer(data))
if err != nil {
return nil, err
}
token, err := GenerateToken(c.config.SelfID, c.config.KeyID, c.config.PrivateKey)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
for k, v := range headers {
req.Header.Set(k, v)
}
resp, err := c.config.Client.Do(req)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case http.StatusOK, http.StatusCreated, http.StatusAccepted:
return io.ReadAll(resp.Body)
default:
return nil, errored(resp)
}
}
type apiResponse struct {
Code int `json:"error_code"`
Message string `json:"message"`
}
func errored(resp *http.Response) error {
var e apiResponse
data, err := io.ReadAll(resp.Body)
if err != nil {
return errors.New(resp.Status)
}
err = json.Unmarshal(data, &e)
if err != nil {
return errors.New(resp.Status)
}
if e.Message != "" {
return errors.New(e.Message)
}
return errors.New(resp.Status)
}