-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
66 lines (52 loc) · 1.41 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
const (
ApplicationJsonType = "application/json"
)
type client struct {
BaseURL string
httpClient http.Client
}
type SetPasswordRequest struct {
Password string `json:"password"`
}
type Response struct {
Status int `json:"-"`
Link string `json:"link"`
Ttl int `json:"ttl"`
}
func (r *Response) IsSuccessful() bool {
return r.Status == http.StatusOK
}
func NewClient(baseURL string) *client {
return &client{BaseURL: baseURL, httpClient: http.Client{}}
}
func (c *client) SetPassword(password string) (*Response, error) {
url := fmt.Sprintf("%s/set_password", c.BaseURL)
request := SetPasswordRequest{Password: password}
body, err := json.Marshal(request)
if err != nil {
return nil, fmt.Errorf("error while marshaling set password body > %v", err)
}
resp, err := c.httpClient.Post(url, ApplicationJsonType, bytes.NewReader(body))
if err != nil {
return nil, fmt.Errorf("error making http request to %s > %v", url, err)
}
var response = &Response{Status: resp.StatusCode}
responseBody, _ := io.ReadAll(resp.Body)
defer resp.Body.Close()
if response.Status != http.StatusOK {
return response, fmt.Errorf("error server returned: %s", resp.Status)
}
err = json.Unmarshal(responseBody, &response)
if err != nil {
return response, fmt.Errorf("error unmarshaling json from body: %v", err)
}
return response, nil
}