Serge Bazanski | d2271de | 2021-07-11 21:26:37 +0000 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | ) |
| 9 | |
| 10 | const ( |
| 11 | atURL = "https://at.hackerspace.pl/api" |
| 12 | ) |
| 13 | |
| 14 | // atStatus is the result of queruing checkinator/at (Hackerspace presence |
| 15 | // service). |
| 16 | type atStatus struct { |
| 17 | // Users is the list of present and publicly visible users. |
| 18 | Users []atUser `json:"users"` |
| 19 | // ESPs is the number of ESP{8266,32} devices. |
| 20 | ESPs int `json:"esps"` |
| 21 | // Kektops is the number of nettop “Kektop” devices. |
| 22 | Kektops int `json:"kektops"` |
| 23 | // Unknown is the number of unknown devices in the network. |
| 24 | Unknown int `json:"unknown"` |
| 25 | } |
| 26 | |
| 27 | type atUser struct { |
| 28 | Login string `json:"login"` |
| 29 | } |
| 30 | |
| 31 | func getAt(ctx context.Context) (*atStatus, error) { |
| 32 | r, err := http.NewRequestWithContext(ctx, "GET", atURL, nil) |
| 33 | if err != nil { |
| 34 | return nil, fmt.Errorf("NewRequest(%q): %w", atURL, err) |
| 35 | } |
| 36 | res, err := http.DefaultClient.Do(r) |
| 37 | if err != nil { |
| 38 | return nil, fmt.Errorf("GET: %w", err) |
| 39 | } |
| 40 | defer res.Body.Close() |
| 41 | |
| 42 | var status atStatus |
| 43 | if err := json.NewDecoder(res.Body).Decode(&status); err != nil { |
| 44 | return nil, fmt.Errorf("when decoding JSON: %w", err) |
| 45 | } |
| 46 | |
| 47 | return &status, nil |
| 48 | } |