/
http.go
106 lines (93 loc) · 3.16 KB
/
http.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
package premiumize
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"go.uber.org/zap"
)
func (c *Client) get(ctx context.Context, urlString string, data url.Values) ([]byte, error) {
if c.auth.OAuth2 {
urlString += "?access_token=" + c.auth.KeyOrToken
} else {
urlString += "?apikey=" + c.auth.KeyOrToken
}
// map[string][]string
for k, vals := range data {
for _, val := range vals {
urlString += "&" + url.QueryEscape(k) + "=" + url.QueryEscape(val)
}
}
req, err := http.NewRequest("GET", urlString, nil)
if err != nil {
return nil, fmt.Errorf("couldn't create GET request: %w", err)
}
for headerKey, headerVal := range c.opts.ExtraHeaders {
req.Header.Add(headerKey, headerVal)
}
c.logger.Debug("Sending request", zap.String("request", fmt.Sprintf("%+v", req)), zapDebridService)
res, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("couldn't send GET request: %w", err)
}
defer res.Body.Close()
resBody, err := ioutil.ReadAll(res.Body)
c.logger.Debug("Got response", zap.Int("status", res.StatusCode), zap.NamedError("bodyReadError", err), zap.ByteString("response", resBody), zapDebridService)
// Check server response status
if res.StatusCode != http.StatusOK {
// resBody can be nil if above ioutil.ReadAll failed, but in that case we don't care about the related error.
return resBody, fmt.Errorf("bad HTTP response status: %v", res.Status)
}
if err != nil {
return nil, fmt.Errorf("couldn't read response body: %w", err)
}
return resBody, nil
}
func (c *Client) post(ctx context.Context, urlString string, data url.Values, form bool) ([]byte, error) {
if c.auth.OAuth2 {
urlString += "?access_token=" + c.auth.KeyOrToken
} else {
urlString += "?apikey=" + c.auth.KeyOrToken
}
var req *http.Request
var err error
if form {
req, err = http.NewRequest("POST", urlString, strings.NewReader(data.Encode()))
} else {
// map[string][]string
for k, vals := range data {
for _, val := range vals {
urlString += "&" + url.QueryEscape(k) + "=" + url.QueryEscape(val)
}
}
req, err = http.NewRequest("POST", urlString, nil)
}
if err != nil {
return nil, fmt.Errorf("couldn't create POST request: %w", err)
}
if form {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
for headerKey, headerVal := range c.opts.ExtraHeaders {
req.Header.Add(headerKey, headerVal)
}
c.logger.Debug("Sending request", zap.String("request", fmt.Sprintf("%+v", req)), zapDebridService)
res, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("couldn't send POST request: %w", err)
}
defer res.Body.Close()
resBody, err := ioutil.ReadAll(res.Body)
c.logger.Debug("Got response", zap.Int("status", res.StatusCode), zap.NamedError("bodyReadError", err), zap.ByteString("response", resBody), zapDebridService)
// Check server response.
if res.StatusCode != http.StatusOK {
// resBody can be nil if above ioutil.ReadAll failed, but in that case we don't care about the related error.
return resBody, fmt.Errorf("bad HTTP response status: %v", res.Status)
}
if err != nil {
return nil, fmt.Errorf("couldn't read response body: %w", err)
}
return resBody, nil
}