-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
46 lines (39 loc) · 975 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
package nap
import "fmt"
type API struct {
BaseURL string // https://httpbin.org
Resources map[string]*RestResource
DefaultRouter *CBRouter
Client *Client
}
func NewAPI(baseURL string) *API {
return &API{
BaseURL: baseURL,
Resources: make(map[string]*RestResource),
DefaultRouter: NewRouter(),
Client: NewClient(),
}
}
func (api *API) SetAuth(auth Authentication) {
api.Client.SetAuth(auth)
}
func (api *API) AddResource(name string, res *RestResource) {
api.Resources[name] = res
}
func (api *API) Call(name string, params map[string]string) error {
res, ok := api.Resources[name]
if !ok {
return fmt.Errorf("Resource does not exist: %s", name)
}
if err := api.Client.ProcessRequest(api.BaseURL, res, params); err != nil {
return err
}
return nil
}
func (api *API) ResourceNames() []string {
resources := []string{}
for k := range api.Resources {
resources = append(resources, k)
}
return resources
}