-
Notifications
You must be signed in to change notification settings - Fork 7
/
targets.go
125 lines (107 loc) · 3.03 KB
/
targets.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
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"fmt"
"io"
"net/http"
"net/url"
"reflect"
"strings"
)
// nzb file target structure
type Target struct {
name string
getCategories func() (Categories, error)
push func(string, string) error
}
// nzb file targets map
type Targets map[string]Target
// global nzb files targets map
var targets = Targets{
"NZBGET": Target{
name: "NZBGet",
getCategories: nzbget_getCategories,
push: nzbget_push,
},
"SABNZBD": Target{
name: "SABnzbd",
getCategories: sabnzbd_getCategories,
push: sabnzbd_push,
},
"SYNOLOGYDLS": Target{
name: "Synology DownloadStation",
push: synologyds_push,
},
"EXECUTE": Target{
name: "Download folder",
push: execute_push,
},
}
// http request function for the targets
func request(conf interface{}, httpMethod string, path string, headers map[string]string, queryParameters url.Values, body io.Reader, contentType string) ([]byte, error) {
values := reflect.ValueOf(conf)
// generate URL
var scheme string
if values.FieldByName("Ssl").Bool() == true {
scheme = "https://"
} else {
scheme = "http://"
}
fullUrl := fmt.Sprintf("%s%s", scheme, values.FieldByName("Host").String())
if values.FieldByName("Port").Int() > 0 {
fullUrl += fmt.Sprintf(":%d", values.FieldByName("Port").Int())
}
if strings.Trim(values.FieldByName("Basepath").String(), " /") != "" {
fullUrl += fmt.Sprintf("/%s", strings.Trim(values.FieldByName("Basepath").String(), " /"))
}
if path != "" {
fullUrl += fmt.Sprintf("/%s", strings.Trim(path, " /"))
}
// set up client
client := http.Client{}
u, err := url.Parse(fullUrl)
if err != nil {
return nil, err
}
// append the query parameters.
q := u.Query()
for k, v := range queryParameters {
q.Set(k, strings.Join(v, ","))
}
// set the query to the encoded parameters
u.RawQuery = q.Encode()
// regardless of GET or POST, we can safely add the body
req, err := http.NewRequest(httpMethod, u.String(), body)
if err != nil {
return nil, err
}
// for each header passed, add the header value to the request
for k, v := range headers {
req.Header.Set(k, v)
}
// if content type is provided, add to header
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
// if basic auth username and password are set add auth header
if values.FieldByName("Basicauth_username").String() != "" && values.FieldByName("Basicauth_password").String() != "" {
req.SetBasicAuth(values.FieldByName("Basicauth_username").String(), values.FieldByName("Basicauth_password").String())
}
// finally, do the request
res, err := client.Do(req)
if err != nil {
return nil, err
}
if res == nil {
return nil, fmt.Errorf("calling %s returned empty response", u.String())
}
defer res.Body.Close()
// read the response data
responseData, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error calling %s:\nstatus: %s\nresponseData: %s", u.String(), res.Status, responseData)
}
return responseData, nil
}