-
Notifications
You must be signed in to change notification settings - Fork 1
/
acrolinx.go
169 lines (135 loc) · 3.55 KB
/
acrolinx.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package acrolinx
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
)
const (
userAgent = "go-acrolinx"
headerSignature = "X-Acrolinx-Client"
headerToken = "X-Acrolinx-Auth"
headerLocale = "X-Acrolinx-Client-Locale"
)
type Client struct {
// Signature identifies this client
signature string
platformURL *url.URL
accessToken string
client *http.Client
// Services for different parts of the API
Checking *CheckingService
}
func NewClient(signature string, urlStr string, options ...ClientOptionFunc) (*Client, error) {
platformURL, err := makePlatformURL(urlStr)
if err != nil {
return nil, fmt.Errorf("Error creating new client: %w", err)
}
// build HTTP NewClient
client := &Client{
signature: signature,
platformURL: platformURL,
client: &http.Client{
Timeout: 30 * time.Second,
},
}
for _, fn := range options {
if fn == nil {
continue
}
if err := fn(client); err != nil {
return nil, err
}
}
client.Checking = &CheckingService{client}
return client, nil
}
func (c *Client) SignIn(username string, password string) error {
creds := Credentials{username, password}
path := "dashboard/api/signin/authenticate"
req, err := c.newRequest(http.MethodPost, path, creds)
if err != nil {
return fmt.Errorf("Error signing in, could not prepare request: %w", err)
}
var token accessToken
err = c.do(req, &token)
if err != nil {
return fmt.Errorf("Error signing in, could not prepare request: %w", err)
}
c.accessToken = token.AccessToken
return nil
}
func (c *Client) newRequest(method, path string, creds interface{}) (*http.Request, error) {
u := *c.platformURL
u.Path = c.platformURL.Path + path
jsonBody, err := json.Marshal(creds)
if err != nil {
return nil, fmt.Errorf("Error encoding JSON: %w", err)
}
body := bytes.NewReader(jsonBody)
req, err := http.NewRequest(method, u.String(), body)
if err != nil {
return nil, fmt.Errorf("Error creating new request: %w", err)
}
req.Header.Set(headerSignature, c.signature)
if c.accessToken != "" {
req.Header.Set(headerToken, c.accessToken)
}
req.Header.Set("Content-Type", "application/json")
return req, nil
}
func (c *Client) do(req *http.Request, v interface{}) error {
res, err := c.client.Do(req)
if err != nil {
return fmt.Errorf("Error submitting request: %w", err)
}
err = json.NewDecoder(res.Body).Decode(&v)
if err != nil {
return fmt.Errorf("Error decoding JSON response: %w", err)
}
return nil
}
func makePlatformURL(urlStr string) (*url.URL, error) {
if !strings.HasSuffix(urlStr, "/") {
urlStr += "/"
}
platformURL, err := url.ParseRequestURI(urlStr)
if err != nil {
return nil, fmt.Errorf("Error parsing platform URL: %w", err)
}
return platformURL, nil
}
func (c *Client) setToken(token string) {
c.accessToken = token
}
type Links = map[string]string
type Response struct {
Data interface{} `json:"data,omitempty"`
Links Links `json:"links,omitempty"`
Progress *Progress `json:"progress,omitempty"`
Error *RequestError `json:"error,omitempty"`
}
type Progress struct {
Percent int `json:"percent"`
Message string `json:"message"`
RetryAfter int `json:"retryAfter"`
}
type RequestError struct {
Type string `json:"type"`
Title string `json:"title"`
Detail string `json:"detail"`
Status int `json:"status"`
}
func (r *RequestError) Error() string {
return r.Detail
}
type Credentials struct {
Username string `json:"username"`
Password string `json:"password"`
}
type accessToken struct {
AccessToken string `json:"accessToken"`
}