-
Notifications
You must be signed in to change notification settings - Fork 17
/
http.go
95 lines (77 loc) · 2.6 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
package starr
import (
"context"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
"reflect"
"strings"
)
/* The methods in this file provided assumption-ridden HTTP calls for Starr apps. */
func (c *Config) req(path, method string, params url.Values, body io.Reader) ([]byte, error) {
if c.Client == nil { // we must have an http client.
return nil, ErrNilClient
}
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout.Duration)
defer cancel()
req, err := http.NewRequestWithContext(ctx, method, c.setPathParams(path, params), body)
if err != nil {
return nil, fmt.Errorf("http.NewRequestWithContext(path): %w", err)
}
c.setHeaders(req)
req.URL.RawQuery = params.Encode()
return c.getBody(req)
}
// setHeaders sets all our request headers.
func (c *Config) setHeaders(req *http.Request) {
// This app allows http auth, in addition to api key (nginx proxy).
if auth := c.HTTPUser + ":" + c.HTTPPass; auth != ":" {
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(auth)))
}
if req.Body != nil {
req.Header.Set("Content-Type", "application/json")
}
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", "go-starr: https://"+reflect.TypeOf(Config{}).PkgPath()) //nolint:exhaustivestruct
req.Header.Set("X-API-Key", c.APIKey)
}
// getBody makes an http request and returns the response body if there are no errors.
func (c *Config) getBody(req *http.Request) ([]byte, error) {
resp, err := c.Client.Do(req)
if err != nil {
return nil, fmt.Errorf("httpClient.Do(req): %w", err)
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("ioutil.ReadAll: %w", err)
}
// fmt.Println(string(b))
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return b, fmt.Errorf("failed: %v (status: %s): %w: %s",
resp.Request.RequestURI, resp.Status, ErrInvalidStatusCode, string(b))
}
return b, nil
}
// setPathParams makes sure the path starts with /api and returns the full URL.
// Also makes sure params is not nil (so it can be encoded later).
// Sets the apikey as a path parameter for use by older radarr/sonarr versions.
func (c *Config) setPathParams(uriPath string, params url.Values) string {
if strings.Contains(uriPath, "api/") {
uriPath = path.Join("/", uriPath)
} else {
uriPath = path.Join("/", "api", uriPath)
}
if params == nil {
params = make(url.Values)
}
if !strings.HasPrefix(uriPath, "/api/v") {
// api paths with /v1 or /v3 in them use a header instead.
params.Add("apikey", c.APIKey)
}
return strings.TrimSuffix(c.URL, "/") + uriPath
}