forked from algebraic-brain/algorithmia-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
123 lines (102 loc) · 2.78 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
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
123
//Algorithmia API Client (Go)
package algorithmia
import (
"encoding/json"
"net/http"
"net/url"
)
type Client struct {
apiKey string
apiAddress string
}
func NewClient(apiKey, apiAddress string) *Client {
c := &Client{
apiKey: apiKey,
apiAddress: apiAddress,
}
if apiAddress == "" {
c.apiAddress = "https://api.algorithmia.com"
}
return c
}
func (c *Client) Algo(ref string) (*Algorithm, error) {
return NewAlgorithm(c, ref)
}
func (c *Client) File(dataUrl string) *DataFile {
return NewDataFile(c, dataUrl)
}
func (c *Client) Dir(dataUrl string) *DataDirectory {
return NewDataDirectory(c, dataUrl)
}
func (c *Client) postJsonHelper(url string, input interface{}, params url.Values) (*http.Response, error) {
headers := http.Header{}
if c.apiKey != "" {
headers.Add("Authorization", c.apiKey)
}
var (
inputJson []byte
err error
)
if input == nil {
headers.Add("Content-Type", "application/json")
inputJson, err = json.Marshal(input)
if err != nil {
return nil, err
}
}
switch inp := input.(type) {
case string:
headers.Add("Content-Type", "text/plain")
inputJson = []byte(inp)
case []byte:
headers.Add("Content-Type", "application/octet-stream")
inputJson = inp
default:
headers.Add("Content-Type", "application/json")
inputJson, err = json.Marshal(input)
if err != nil {
return nil, err
}
}
return request{Url: c.apiAddress + url, Data: inputJson, Headers: headers, Params: params}.post()
}
func (c *Client) getHelper(url string, params url.Values) (*http.Response, error) {
headers := http.Header{}
if c.apiKey != "" {
headers.Add("Authorization", c.apiKey)
}
return request{Url: c.apiAddress + url, Headers: headers, Params: params}.get()
}
func (c *Client) headHelper(url string) (*http.Response, error) {
headers := http.Header{}
if c.apiKey != "" {
headers.Add("Authorization", c.apiKey)
}
return request{Url: c.apiAddress + url, Headers: headers}.head()
}
func (c *Client) putHelper(url string, data []byte) (*http.Response, error) {
headers := http.Header{}
if c.apiKey != "" {
headers.Add("Authorization", c.apiKey)
}
return request{Url: c.apiAddress + url, Headers: headers, Data: data}.put()
}
func (c *Client) deleteHelper(url string) (*http.Response, error) {
headers := http.Header{}
if c.apiKey != "" {
headers.Add("Authorization", c.apiKey)
}
return request{Url: c.apiAddress + url, Headers: headers}.delete()
}
func (c *Client) patchHelper(url string, params map[string]interface{}) (*http.Response, error) {
headers := http.Header{}
headers.Add("content-type", "application/json")
if c.apiKey != "" {
headers.Add("Authorization", c.apiKey)
}
b, err := json.Marshal(params)
if err != nil {
return nil, err
}
return request{Url: c.apiAddress + url, Headers: headers, Data: b}.patch()
}