-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathprotocols_api.go
60 lines (53 loc) · 1.4 KB
/
protocols_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
package node
import (
"context"
"fmt"
)
// ProtocolsAPI -
type ProtocolsAPI interface {
GetProtocols(ctx context.Context) ([]string, error)
Protocol(ctx context.Context, hash string) (ProtocolInfo, error)
Environment(ctx context.Context, hash string) (int, error)
}
// Protocols -
type Protocols struct {
baseURL string
client *client
}
// NewProtocols -
func NewProtocols(baseURL string) *Protocols {
return &Protocols{
baseURL: baseURL,
client: newClient(),
}
}
// GetProtocols -
func (api *Protocols) GetProtocols(ctx context.Context) ([]string, error) {
req, err := newGetRequest(api.baseURL, "protocols", nil)
if err != nil {
return nil, err
}
var result []string
err = req.doWithJSONResponse(ctx, api.client, &result)
return result, err
}
// Protocol -
func (api *Protocols) Protocol(ctx context.Context, hash string) (ProtocolInfo, error) {
req, err := newGetRequest(api.baseURL, fmt.Sprintf("protocols/%s", hash), nil)
if err != nil {
return ProtocolInfo{}, err
}
var result ProtocolInfo
err = req.doWithJSONResponse(ctx, api.client, &result)
return result, err
}
// Environment -
func (api *Protocols) Environment(ctx context.Context, hash string) (int, error) {
req, err := newGetRequest(api.baseURL, fmt.Sprintf("protocols/%s/environment", hash), nil)
if err != nil {
return 0, err
}
var result int
err = req.doWithJSONResponse(ctx, api.client, &result)
return result, err
}