-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
207 lines (172 loc) · 5.78 KB
/
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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package weatherkit
import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"github.com/golang-jwt/jwt"
"github.com/hashicorp/go-hclog"
"github.com/turbot/steampipe-plugin-sdk/v4/plugin"
"io"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
)
type Client struct {
httpClient *http.Client
config *weatherKitConfig
logger hclog.Logger
}
func NewClient(ctx context.Context, httpClient *http.Client, config *weatherKitConfig) *Client {
return &Client{
httpClient: httpClient,
config: config,
logger: plugin.Logger(ctx),
}
}
func (c *Client) Get(ctx context.Context, url string, v interface{}) error {
req, _ := c.NewRequest(ctx, http.MethodGet, url)
err := c.DoRequest(req, v)
if err != nil {
return err
}
return nil
}
func (c *Client) NewRequest(ctx context.Context, method, url string) (*http.Request, error) {
req, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create HTTP request: %w", err)
}
var token string
if c.config.Token != nil {
token = *c.config.Token
} else {
token = c.createJwt()
}
req.Header.Add("Authorization", "Bearer "+token)
req = req.WithContext(ctx)
return req, nil
}
func (c *Client) checkResponseStatus(r *http.Response) {
status := r.Status
switch status {
case "400 Bad Request":
c.logger.Error("DoRequest", "status", status, "message", "The server is unable to process the request due to an invalid parameter value.")
panic("\nthe server is unable to process the request due to an invalid parameter value.\nPlease file an issue at https://github.com/ellisvalentiner/steampipe-plugin-weatherkit")
case "401 Unauthorized":
c.logger.Error("DoRequest", "status", status, "message", "The request isn’t authorized or doesn’t include the correct authentication information.")
panic("\nthe request isn’t authorized or doesn’t include the correct authentication information.\nHint: check the credentials in ~/.steampipe/config/weatherkit.spc")
default:
c.logger.Info("DoRequest", "status", status)
}
}
func (c *Client) DoRequest(r *http.Request, v interface{}) error {
if v == nil {
return nil
}
resp, err := c.httpClient.Do(r)
if resp == nil || err != nil {
c.logger.Error("DoRequest", "message", "received empty response")
return errors.New("an error occurred while doing the request")
}
c.checkResponseStatus(resp)
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
return
}
}(resp.Body)
var buf bytes.Buffer
dec := json.NewDecoder(io.TeeReader(resp.Body, &buf))
if err := dec.Decode(v); err != nil {
c.logger.Error("DoRequest", "message", "could not parse response body")
return fmt.Errorf("could not parse response body: %w [%s:%s] %s", err, r.Method, r.URL.String(), buf.String())
}
return nil
}
func (c *Client) loadPrivateKey() *ecdsa.PrivateKey {
// Read, decode, and parse the private key
fileBytes, _ := os.ReadFile(*c.config.PrivateKeyPath)
x509Encoded, _ := pem.Decode(fileBytes)
parsedKey, _ := x509.ParsePKCS8PrivateKey(x509Encoded.Bytes)
ecdsaPrivateKey, _ := parsedKey.(*ecdsa.PrivateKey)
return ecdsaPrivateKey
}
func (c *Client) createJwt() string {
// Define standard claims
claims := jwt.StandardClaims{
Issuer: *c.config.TeamId,
IssuedAt: time.Now().UTC().Unix(),
ExpiresAt: time.Now().Add(time.Minute * 5).UTC().Unix(),
Subject: *c.config.ServiceId,
}
// Create the JWT
token := jwt.NewWithClaims(jwt.SigningMethodES256, claims)
// Add header information
token.Header = map[string]interface{}{
"alg": "ES256",
"kid": c.config.KeyId,
"id": claims.Issuer + "." + claims.Subject,
}
// Sign and get the complete encoded token as a string using the secret
ecdsaPrivateKey := c.loadPrivateKey()
tokenString, _ := token.SignedString(ecdsaPrivateKey)
return tokenString
}
func (c *Client) Availability(ctx context.Context, latitude string, longitude string) ([]string, error) {
requestUrl := url.URL{
Scheme: "https",
Host: baseUrl,
Path: strings.Join([]string{"api", "v1", "availability", latitude, longitude}, "/"),
}
u := requestUrl.Query()
u.Set("country", "US")
requestUrl.RawQuery = u.Encode()
//Response object
var dataSet []string
err := c.Get(ctx, requestUrl.String(), &dataSet)
if err != nil {
log.Fatalf("Request failed: [%s]", err)
}
return dataSet, nil
}
func (c *Client) Weather(ctx context.Context, latitude string, longitude string, datasets []string) (Weather, error) {
requestUrl := url.URL{
Scheme: "https",
Host: baseUrl,
Path: strings.Join([]string{"api", "v1", "weather", language, latitude, longitude}, "/"),
}
u := requestUrl.Query()
u.Set("country", "US")
u.Set("dataSets", strings.Join(datasets, ","))
requestUrl.RawQuery = u.Encode()
//Response object
var weather Weather
err := c.Get(ctx, requestUrl.String(), &weather)
if err != nil {
log.Fatalf("Request failed: [%s]", err)
}
return weather, nil
}
func (c *Client) CurrentWeather(ctx context.Context, latitude string, longitude string) (Weather, error) {
return c.Weather(ctx, latitude, longitude, []string{"currentWeather"})
}
func (c *Client) DailyForecast(ctx context.Context, latitude string, longitude string) (Weather, error) {
return c.Weather(ctx, latitude, longitude, []string{"forecastDaily"})
}
func (c *Client) HourlyForecast(ctx context.Context, latitude string, longitude string) (Weather, error) {
return c.Weather(ctx, latitude, longitude, []string{"forecastHourly"})
}
func (c *Client) NextHourForecast(ctx context.Context, latitude string, longitude string) (Weather, error) {
return c.Weather(ctx, latitude, longitude, []string{"forecastNextHour"})
}
func (c *Client) WeatherAlerts(ctx context.Context, latitude string, longitude string) (Weather, error) {
return c.Weather(ctx, latitude, longitude, []string{"weatherAlerts"})
}