-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
48 lines (36 loc) · 1.06 KB
/
auth.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
package dogehousego
import (
"bytes"
"encoding/json"
"errors"
"net/http"
)
type AuthResponse struct {
Username string `json:"username"`
AccessToken string `json:"accessToken"`
RefreshToken string `json:"refreshToken"`
Error string `json:"error"`
}
type authPost struct {
ApiKey string `json:"apiKey"`
}
func Auth(apiKey string) (*AuthResponse, error) {
postData := authPost{ApiKey: apiKey};
dataString, err := json.Marshal(postData);
if err != nil {
return nil, errors.New("Failed to marshal apiKey. Error: " + err.Error());
}
resp, err := http.Post(HttpBaseUrl+ "/bot/auth", "application/json", bytes.NewBuffer(dataString));
if err != nil {
return nil, errors.New("Failed to send auth request. Error: " + err.Error());
}
var retVal *AuthResponse;
err = json.NewDecoder(resp.Body).Decode(&retVal);
if err != nil {
return nil, errors.New("Failed to unmarshal response auth data. Error: " + err.Error());
}
if retVal.Error != "" {
return nil, errors.New("Failed to get tokens from api-key. Reason: " + retVal.Error);
}
return retVal, nil;
}