|
1 | 1 | package pkg |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
4 | 6 | "fmt" |
5 | 7 | "io" |
6 | 8 | "log" |
@@ -292,6 +294,67 @@ func (c *Client) GetLogs(patStr, owner, host, id string, age time.Duration, staf |
292 | 294 | return string(body), res.StatusCode, nil |
293 | 295 | } |
294 | 296 |
|
| 297 | +func (c *Client) GetMetering(patStr, owner, host, id string, staff bool) (string, int, error) { |
| 298 | + |
| 299 | + u, _ := url.Parse(c.baseURL) |
| 300 | + u.Path = "/api/v1/metering" |
| 301 | + |
| 302 | + q := u.Query() |
| 303 | + q.Set("owner", owner) |
| 304 | + q.Set("host", host) |
| 305 | + |
| 306 | + if len(id) > 0 { |
| 307 | + q.Set("id", id) |
| 308 | + } else { |
| 309 | + return "", http.StatusBadRequest, fmt.Errorf("id is required") |
| 310 | + } |
| 311 | + |
| 312 | + if staff { |
| 313 | + q.Set("staff", "1") |
| 314 | + } |
| 315 | + |
| 316 | + u.RawQuery = q.Encode() |
| 317 | + |
| 318 | + req, err := http.NewRequest(http.MethodGet, u.String(), nil) |
| 319 | + if err != nil { |
| 320 | + return "", http.StatusBadRequest, err |
| 321 | + } |
| 322 | + |
| 323 | + req.Header.Set("Authorization", "Bearer "+patStr) |
| 324 | + |
| 325 | + if os.Getenv("DEBUG") == "1" { |
| 326 | + sanitised := http.Header{} |
| 327 | + for k, v := range req.Header { |
| 328 | + |
| 329 | + if k == "Authorization" { |
| 330 | + v = []string{"redacted"} |
| 331 | + } |
| 332 | + sanitised[k] = v |
| 333 | + } |
| 334 | + |
| 335 | + fmt.Printf("URL %s\nHeaders: %v\n", u.String(), sanitised) |
| 336 | + } |
| 337 | + |
| 338 | + res, err := c.httpClient.Do(req) |
| 339 | + if err != nil { |
| 340 | + return "", http.StatusBadRequest, err |
| 341 | + } |
| 342 | + |
| 343 | + var body []byte |
| 344 | + if res.Body != nil { |
| 345 | + defer res.Body.Close() |
| 346 | + body, _ = io.ReadAll(res.Body) |
| 347 | + } |
| 348 | + |
| 349 | + var prettyJSON bytes.Buffer |
| 350 | + |
| 351 | + if err = json.Indent(&prettyJSON, []byte(body), "", " "); err != nil { |
| 352 | + return "", http.StatusBadRequest, err |
| 353 | + } |
| 354 | + |
| 355 | + return prettyJSON.String(), res.StatusCode, nil |
| 356 | +} |
| 357 | + |
295 | 358 | func (c *Client) GetAgentLogs(patStr, owner, host string, age time.Duration, staff bool) (string, int, error) { |
296 | 359 |
|
297 | 360 | mins := int(age.Minutes()) |
|
0 commit comments