-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhttp.go
More file actions
130 lines (112 loc) · 2.92 KB
/
http.go
File metadata and controls
130 lines (112 loc) · 2.92 KB
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
package verniy
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"strings"
)
type queryRequest struct {
Query string `json:"query"`
Variables queryVariable `json:"variables"`
}
type queryVariable map[string]interface{}
type errorResponse struct {
Errors []struct {
Message string `json:"message"`
Status int `json:"status"`
} `json:"errors"`
}
func (c *Client) handleError(body []byte) error {
var e errorResponse
if err := json.Unmarshal(body, &e); err != nil {
return err
}
errMsgs := make([]string, len(e.Errors))
for i, b := range e.Errors {
errMsgs[i] = b.Message
}
return errors.New(strings.Join(errMsgs, " | "))
}
func (c *Client) post(ctx context.Context, query string, v map[string]interface{}, model interface{}) error {
d, err := json.Marshal(queryRequest{
Query: query,
Variables: v,
})
if err != nil {
return err
}
body, code, err := c.MakeRequest(ctx, d)
if err != nil {
return err
}
if code != http.StatusOK {
return c.handleError(body)
}
if err := json.Unmarshal(body, &model); err != nil {
return err
}
return nil
}
// MakeRequest to make direct HTTP request without any wrapper.
// Prepare your body request and handle the response by yourself.
//
// Use this if you want to make custom request. Also, need to read the
// docs of how to prepare the body request and read the response
// (https://studio.apollographql.com/sandbox/explorer?endpoint=https%3A%2F%2Fgraphql.anilist.co).
//
// Params requestBody is your data in JSON format. So, marshal your data
// first before passing it to this function. And will return response body,
// response code, and error.
//
// Example:
//
// query := verniy.FieldObject("query", verniy.QueryParam{
// "$id": "Int",
// "$type": "MediaType",
// }, verniy.FieldObject("Media", verniy.QueryParam{
// "id": "$id",
// "type": "$type",
// }, "id"))
//
// body := map[string]interface{}{
// "query": query,
// "variables": map[string]interface{}{
// "id": 1,
// "type": "ANIME",
// },
// }
//
// jsonBody, _ := json.Marshal(body)
//
// data, code, err := c.MakeRequest(jsonBody)
// if err != nil {
// panic(err)
// }
//
// fmt.Println(code)
// fmt.Println(string(data))
func (c *Client) MakeRequest(ctx context.Context, requestBody []byte) ([]byte, int, error) {
c.Limiter.Take()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.Host, bytes.NewBuffer(requestBody))
if err != nil {
return nil, http.StatusInternalServerError, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
if c.AccessToken != "" {
req.Header.Add("Authorization", "Bearer "+c.AccessToken)
}
resp, err := c.Http.Do(req)
if err != nil {
return nil, http.StatusInternalServerError, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, http.StatusInternalServerError, err
}
return body, resp.StatusCode, nil
}