-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
105 lines (97 loc) · 2.83 KB
/
api.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
package gostrawpoll
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
// GetRequest is a structure for creating a GET request.
type GetRequest struct {
ID int
}
// GetResponse is a structure for returning a GET response.
type GetResponse struct {
ID int `json:"id"`
Title string `json:"title"`
Options []string `json:"options"`
Votes []int `json:"votes"`
Multi bool `json:"multi"`
Dupcheck string `json:"dupcheck"`
Captcha bool `json:"captcha"`
}
// PostRequest is a structure for creating a POST request.
type PostRequest struct {
Title string `json:"title"` // REQUIRED
Options []string `json:"options"` // REQUIRED
Multi bool `json:"multi"`
Dupcheck string `json:"dupcheck"`
Captcha bool `json:"captcha"`
}
// PostResponse is the POST response from the server.
type PostResponse struct {
ID int `json:"id"`
Title string `json:"title"`
Options []string `json:"options"`
Multi bool `json:"multi"`
Dupcheck string `json:"dupcheck"`
Captcha bool `json:"captcha"`
}
const endpoint = "https://strawpoll.me/api/v2/polls"
func Get(req *GetRequest) (*GetResponse, error) {
if req == nil {
return nil, errors.New("req must not be nil")
}
resp, err := http.Get(fmt.Sprintf("%s/%d", endpoint, req.ID))
defer resp.Body.Close()
if err != nil {
return nil, fmt.Errorf("failed to send req: err")
} else if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("req failed: %v", resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("could not read response body: %v", err)
}
ret := &GetResponse{}
if err := json.Unmarshal(body, ret); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %v", err)
}
return ret, nil
}
func Post(req *PostRequest) (*PostResponse, error) {
if req == nil {
return nil, errors.New("req must not be nil")
}
if req.Title == "" {
return nil, errors.New("Title must not be empty")
}
if len(req.Options) == 0 {
return nil, errors.New("options must not be empty")
}
b, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("couldn't marshal json req: %v", err)
}
hReq, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(b))
if err != nil {
return nil, fmt.Errorf("failed to create req: %v", err)
}
resp, err := http.DefaultClient.Do(hReq)
defer resp.Body.Close()
if err != nil {
fmt.Errorf("failed to send req: %v", err)
} else if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("req failed: %v", resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("could not read response body: %v", err)
}
ret := &PostResponse{}
if err := json.Unmarshal(body, ret); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %v", err)
}
return ret, nil
}