-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
client.go
46 lines (36 loc) · 915 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
43
44
45
46
package client
import (
"context"
"net/url"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/x/client"
)
// The BaseURL of our API client.
const BaseURL = "https://api.weatherapi.com/v1"
type (
Options struct {
APIKey string `json:"api_key" yaml:"APIKey" toml:"APIKey"`
}
Client struct {
*client.Client
}
)
func NewClient(opts Options) *Client {
apiKeyParameterSetter := client.RequestParam("key", opts.APIKey)
c := client.New(
client.Debug,
client.BaseURL(BaseURL),
client.PersistentRequestOptions(apiKeyParameterSetter),
)
return &Client{c}
}
func (c *Client) GetCurrentByCity(ctx context.Context, city string) (resp Response, err error) {
urlpath := "/current.json"
// ?q=Athens&aqi=no
params := client.RequestQuery(url.Values{
"q": []string{city},
"aqi": []string{"no"},
})
err = c.Client.ReadJSON(ctx, &resp, iris.MethodGet, urlpath, nil, params)
return
}