Files
gluetun/internal/provider/privateinternetaccess/updater/api.go
T
Quentin McGaw 4a78989d9d chore: do not use sentinel errors when unneeded
- 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
2026-05-02 03:29:46 +00:00

73 lines
1.4 KiB
Go

package updater
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/netip"
)
type apiData struct {
Regions []regionData `json:"regions"`
}
type regionData struct {
Name string `json:"name"`
DNS string `json:"dns"`
PortForward bool `json:"port_forward"`
Offline bool `json:"offline"`
Servers struct {
UDP []serverData `json:"ovpnudp"`
TCP []serverData `json:"ovpntcp"`
} `json:"servers"`
}
type serverData struct {
IP netip.Addr `json:"ip"`
CN string `json:"cn"`
}
func fetchAPI(ctx context.Context, client *http.Client) (
data apiData, err error,
) {
const url = "https://serverlist.piaservers.net/vpninfo/servers/v7"
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return data, err
}
response, err := client.Do(request)
if err != nil {
return data, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return data, fmt.Errorf("HTTP status code not OK: %d %s",
response.StatusCode, response.Status)
}
b, err := io.ReadAll(response.Body)
if err != nil {
return data, err
}
if err := response.Body.Close(); err != nil {
return data, err
}
// remove key/signature at the bottom
i := bytes.IndexRune(b, '\n')
b = b[:i]
if err := json.Unmarshal(b, &data); err != nil {
return data, err
}
return data, nil
}