mirror of
https://github.com/qdm12/gluetun.git
synced 2026-05-09 20:29:23 +02:00
4a78989d9d
- main reason being it's a burden to always define sentinel errors at global scope, wrap them with `%w` instead of using a string directly - only use sentinel errors when it has to be checked using `errors.Is` - replace all usage of these sentinel errors in `fmt.Errorf` with direct strings that were in the sentinel error - exclude the sentinel error definition requirement from .golangci.yml - update unit tests to use ContainersError instead of ErrorIs so it stays as a "not a change detector test" without requiring a sentinel error
52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package updater
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
type serverData struct {
|
|
Hostname string `json:"hostname"`
|
|
Country string `json:"country_name"`
|
|
City string `json:"city_name"`
|
|
Active bool `json:"active"`
|
|
Owned bool `json:"owned"`
|
|
Provider string `json:"provider"`
|
|
IPv4 string `json:"ipv4_addr_in"`
|
|
IPv6 string `json:"ipv6_addr_in"`
|
|
Type string `json:"type"`
|
|
PubKey string `json:"pubkey"` // Wireguard public key
|
|
}
|
|
|
|
func fetchAPI(ctx context.Context, client *http.Client) (data []serverData, err error) {
|
|
const url = "https://api.mullvad.net/www/relays/all/"
|
|
|
|
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
response, err := client.Do(request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer response.Body.Close()
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("HTTP status code not OK: %d %s", response.StatusCode, response.Status)
|
|
}
|
|
|
|
decoder := json.NewDecoder(response.Body)
|
|
if err := decoder.Decode(&data); err != nil {
|
|
return nil, fmt.Errorf("failed decoding response body: %s", err)
|
|
}
|
|
|
|
if err := response.Body.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return data, nil
|
|
}
|