-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
91 lines (74 loc) · 1.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
package clarifai2
import (
// "fmt"
"bytes"
"net/http"
"encoding/json"
"io/ioutil"
"errors"
"strings"
"strconv"
)
const (
version = "v2"
rootURL = "https://api.clarifai.com"
)
type Client struct {
APIKey string
APIRoot string
}
func NewClient(apiKey string) *Client {
return &Client{apiKey, rootURL}
}
func (client *Client) commonHTTPRequest(jsonBody interface{}, endpoint, verb string, retry bool) ([]byte, error) {
if jsonBody == nil {
jsonBody = struct{}{}
}
body, err := json.Marshal(jsonBody)
if err != nil {
return nil, err
}
// fmt.Println(client.BuildURL(endpoint))
// s := string(body)
// fmt.Println(s)
req, err := http.NewRequest(verb, client.BuildURL(endpoint), bytes.NewReader(body))
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Key "+client.APIKey)
req.Header.Set("Content-Type", "application/json")
httpClient := &http.Client{}
res, err := httpClient.Do(req)
if err != nil {
return nil, err
}
switch res.StatusCode {
case 200, 201:
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
return body, err
case 401:
if !retry {
return client.commonHTTPRequest(jsonBody, endpoint, verb, true)
}
return nil, errors.New("APIKEY_INVALID")
case 400:
return nil, errors.New("ALL_ERROR")
case 500:
return nil, errors.New("CLARIFAI_ERROR")
default:
return nil, errors.New("UNEXPECTED_STATUS_CODE: "+strconv.Itoa(res.StatusCode))
}
}
// SetAccessToken will set accessToken to a new value
func (client *Client) setAPIKey(apiKey string) {
client.APIKey = apiKey
}
func (client *Client) setAPIRoot(root string) {
client.APIRoot = root
}
// Helper function to build URLs
func (client *Client) BuildURL(endpoint string) string {
parts := []string{client.APIRoot, version, endpoint}
return strings.Join(parts, "/")
}