-
-
Notifications
You must be signed in to change notification settings - Fork 328
/
api.go
52 lines (44 loc) · 1002 Bytes
/
api.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
package api
import (
"context"
"errors"
"fmt"
"net/http"
"net/netip"
"strings"
"github.com/qdm12/gluetun/internal/models"
)
type API interface {
FetchInfo(ctx context.Context, ip netip.Addr) (
result models.PublicIP, err error)
}
type Provider string
const (
IPInfo Provider = "ipinfo"
IP2Location Provider = "ip2location"
)
func New(provider Provider, client *http.Client, token string) ( //nolint:ireturn
a API, err error) {
switch provider {
case IPInfo:
return newIPInfo(client, token), nil
case IP2Location:
return newIP2Location(client, token), nil
default:
panic("provider not valid: " + provider)
}
}
var (
ErrProviderNotValid = errors.New("API name is not valid")
)
func ParseProvider(s string) (provider Provider, err error) {
switch strings.ToLower(s) {
case "ipinfo":
return IPInfo, nil
case "ip2location":
return IP2Location, nil
default:
return "", fmt.Errorf(`%w: %q can only be "ipinfo" or "ip2location"`,
ErrProviderNotValid, s)
}
}