forked from Deflix-tv/go-debrid
/
http.go
88 lines (76 loc) · 2.81 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
package alldebrid
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"go.uber.org/zap"
)
func (c *Client) get(ctx context.Context, url string) ([]byte, error) {
if strings.Contains(url, "?") {
url += "&agent=go-debrid&apikey=" + c.apiKey
} else {
url += "?agent=go-debrid&apikey=" + c.apiKey
}
req, err := http.NewRequest("GET", url, 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 {
if err, found := errMap[res.StatusCode]; found {
return resBody, err
}
// 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, url string, data url.Values) ([]byte, error) {
url += "?agent=go-debrid&apikey=" + c.apiKey
req, err := http.NewRequest("POST", url, strings.NewReader(data.Encode()))
if err != nil {
return nil, fmt.Errorf("couldn't create POST request: %w", err)
}
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.
// Different RealDebrid API POST endpoints return different status codes.
if res.StatusCode != http.StatusOK {
if err, found := errMap[res.StatusCode]; found {
return resBody, err
}
// 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
}