-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
42 lines (35 loc) · 904 Bytes
/
client.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
package qweathersdkgo
import (
"encoding/json"
"net/http"
"net/url"
)
var key = "xxx"
type Client struct {
APIKey string
AirQualityBetaURL string
WeatherURL string
GeoURL string
HTTPClient *http.Client
}
func NewClient(apiKey string) *Client {
return &Client{
APIKey: apiKey,
AirQualityBetaURL: "https://devapi.qweather.com/airquality/v1/now",
WeatherURL: "https://devapi.qweather.com/v7",
GeoURL: "https://geoapi.qweather.com/v2",
HTTPClient: &http.Client{},
}
}
func (c *Client) sendRequest(method, endpoint string, params url.Values, v interface{}) error {
req, err := http.NewRequest(method, endpoint+"?"+params.Encode(), nil)
if err != nil {
return err
}
resp, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return json.NewDecoder(resp.Body).Decode(v)
}