This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests.go
114 lines (93 loc) · 2.82 KB
/
requests.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
package goshikimori
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
)
// NewRequest returns request for Shikimori (add to method full address).
// Ex., ("GET", "whoami", nil) => ("GET", "https://shikimori.org/api/whoami", nil)
func (shiki *Shikimori) NewRequest(httpMethod, shikiMethod string, body io.Reader) (*http.Request, error) {
fullURL := fmt.Sprintf(shiki.URLFormat, shikiMethod)
return http.NewRequest(httpMethod, fullURL, body)
}
// Do http request.
// If returns 404, return as error (for comfy use)
func (shiki *Shikimori) Do(req *http.Request) (*http.Response, error) {
resp, err := shiki.Client.Do(req)
if err != nil {
return resp, err
}
// check HTTP 404
if resp.StatusCode == http.StatusNotFound {
return resp, errors.New("Shikimori API method not found")
}
return resp, err
}
// Get Shikimori method (HTTP GET request)
func (shiki *Shikimori) Get(method string) (*http.Response, error) {
req, err := shiki.NewRequest("GET", method, nil)
if err != nil {
return nil, err
}
return shiki.Do(req)
}
// Post Shikimori method (HTTP POST request)
func (shiki *Shikimori) Post(method string, body io.Reader) (*http.Response, error) {
req, err := shiki.NewRequest("POST", method, body)
if err != nil {
return nil, err
}
return shiki.Do(req)
}
// Delete Shikimori method (HTTP DELETE request)
func (shiki *Shikimori) Delete(method string) (*http.Response, error) {
req, err := shiki.NewRequest("DELETE", method, nil)
if err != nil {
return nil, err
}
return shiki.Do(req)
}
// Put Shikimori method (HTTP PUT request)
func (shiki *Shikimori) Put(method string, body io.Reader) (*http.Response, error) {
req, err := shiki.NewRequest("PUT", method, body)
if err != nil {
return nil, err
}
return shiki.Do(req)
}
// Patch Shikimori method (HTTP PATCH request)
func (shiki *Shikimori) Patch(method string, body io.Reader) (*http.Response, error) {
req, err := shiki.NewRequest("PATCH", method, body)
if err != nil {
return nil, err
}
return shiki.Do(req)
}
// JSONGet Shikimori method (HTTP GET request + decode json into outStruct)
func (shiki *Shikimori) JSONGet(method string, outStruct interface{}) (*http.Response, error) {
resp, err := shiki.Get(method)
if err != nil {
return resp, err
}
err = json.NewDecoder(resp.Body).Decode(outStruct)
return resp, err
}
// JSONPost Shikimori method (HTTP POST request +
// encode json from inStruct as body + decode json into outStruct)
func (shiki *Shikimori) JSONPost(method string, inStruct, outStruct interface{}) (*http.Response, error) {
jsonBytes, err := json.Marshal(inStruct)
if err != nil {
return nil, err
}
jsonReader := bytes.NewReader(jsonBytes)
resp, err := shiki.Post(method, jsonReader)
if err != nil {
return nil, err
}
err = json.NewDecoder(resp.Body).Decode(outStruct)
return resp, err
}
// todo: JSONDelete/Put/Patch