-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
62 lines (51 loc) · 1.26 KB
/
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
53
54
55
56
57
58
59
60
61
62
package ctftime
import (
"errors"
"log"
)
type apiClient interface {
GetUrl() (string, error)
GetAPIData() (interface{}, error)
}
type APIContext map[string]interface{}
type apiClientFactory func(ctx APIContext) apiClient
var apiClientFactories = make(map[string]apiClientFactory)
func registerAPIClient(name string, factory apiClientFactory) {
if factory == nil {
log.Panicf("API Client Factory %s does not exist.", name)
}
_, registered := apiClientFactories[name]
if registered {
log.Panicf("API Client Factory %s already registered. Ignoring.", name)
}
apiClientFactories[name] = factory
}
func newAPIClient(name string, ctx APIContext) (apiClient, error) {
clientFactory, ok := apiClientFactories[name]
if !ok {
return nil, errors.New("Invalid API Client name!")
}
return clientFactory(ctx), nil
}
func GetUrl(name string, ctx APIContext) (string, error) {
client, err := newAPIClient(name, ctx)
if err != nil {
return "", err
}
url, err := client.GetUrl()
if err != nil {
return "", err
}
return url, nil
}
func GetAPIData(name string, ctx APIContext) (interface{}, error) {
client, err := newAPIClient(name, ctx)
if err != nil {
return nil, err
}
data, err := client.GetAPIData()
if err != nil {
return nil, err
}
return data, nil
}