-
Notifications
You must be signed in to change notification settings - Fork 0
/
requester.go
145 lines (113 loc) · 2.97 KB
/
requester.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"net/http"
"io"
"bytes"
"encoding/json"
"os"
)
/**
* Post request to the given address with the given parameters
* @param host The host to post to (e.g. https://incommodities.io)
* @param path The path to post to (e.g. /a)
* @param params The parameters to post
* @return The response body
*/
func (r *AreaRequester) post(address string, params map[string]string, data []byte) (result []byte, code int, err error) {
// Declare HTTP Method and Url
req, err := http.NewRequest("POST", address, bytes.NewBuffer(data))
if err != nil {
return []byte{}, 0, err;
}
req.Header.Set("Authorization", "Bearer " + r.auth_key);
if data != nil && 0 < len(data) {
req.Header.Set("Content-Type", "application/json");
}
q := req.URL.Query()
// for each parameter, add it to the query
for key, value := range params {
q.Add(key, value)
}
req.URL.RawQuery = q.Encode()
// Send request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return []byte{}, 0, err;
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return []byte{}, 0, err;
}
return body, resp.StatusCode, nil;
}
type AreaRequester struct {
from_address string
to_address string
auth_key string
areas []string
}
func (r *AreaRequester) GetDataFromArea(area string) (data []byte, statusCode int, err error) {
params := make(map[string]string);
params["area"] = area;
return r.post(r.from_address, params, nil);
}
func (r *AreaRequester) postData(data []byte) (body []byte, statusCode int, err error) {
// empty params
params := make(map[string]string);
return r.post(r.to_address, params, data);
}
func (r *AreaRequester) PostAreaData(payload WeatherData) ([]byte, int, error) {
data, err := json.Marshal(payload)
if err != nil {
return []byte{}, 0, err;
}
return r.postData(data);
}
func GetAreaRequester(areas []string) AreaRequester{
return AreaRequester{
from_address: os.Getenv("API_ADDRESS_FROM"),
to_address: os.Getenv("API_ADDRESS_TO"),
auth_key: os.Getenv("API_KEY"),
areas: areas,
}
}
/**
* Get the area from area requester concurrently
* @return The responses from the different areas
*/
func (r AreaRequester) RequestAreas() []RequestResponse {
c := make(chan RequestResponse, 1)
responses := make([]RequestResponse, len(r.areas))
for _, area := range r.areas {
go r.RequestArea(c, area)
}
for i := 0; i < len(r.areas); i++ {
responses[i] = <-c
}
return responses
}
/**
* Get the area from area requester
* @param area The area to get the data for
* @return The response from the area
*/
func (r AreaRequester) RequestArea(respond chan RequestResponse, area string) {
data, statusCode, err := r.GetDataFromArea(area);
respond <- RequestResponse{
area: area,
data: data,
statusCode: statusCode,
err: err,
}
}
type RequestResponse struct {
area string
data []byte
statusCode int
err error
}
func (r RequestResponse) writeToFile () error {
return writeRawCSVFile(r.data, r.area);
}