forked from ao-space/platform-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetAbility.go
83 lines (67 loc) · 1.7 KB
/
GetAbility.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package platform
import (
"github.com/ao-space/platform-sdk-go/utils"
"github.com/jinzhu/copier"
"net/http"
"net/url"
"strings"
"time"
)
type Api struct {
Method string `json:"method"`
Uri string `json:"uri"`
BriefUri string `json:"briefUri"`
CompatibleVersions []int `json:"compatibleVersions"`
Type string `json:"type"`
Desc string `json:"desc"`
}
type GetAbilityResponse struct {
PlatformApis []Api `json:"platformApis"`
}
func (c *Client) GetAbility() (*GetAbilityResponse, error) {
path := "/platform/ability"
URL := new(url.URL)
copier.Copy(URL, c.BaseURL)
URL = URL.JoinPath(path)
op := new(Operation)
op.SetOperation(http.MethodGet, URL)
response, err := c.Send(op, nil)
if err != nil {
return nil, err
}
output := new(GetAbilityResponse)
err = utils.GetBody(response, output)
if err != nil {
return nil, err
}
ability := make(map[string]map[string]map[int]int)
for _, api := range output.PlatformApis {
if ability[api.Uri] == nil {
ability[api.Uri] = make(map[string]map[int]int)
}
if ability[api.Uri][strings.ToUpper(api.Method)] == nil {
ability[api.Uri][strings.ToUpper(api.Method)] = make(map[int]int)
}
for _, version := range api.CompatibleVersions {
ability[api.Uri][strings.ToUpper(api.Method)][version] = 1
}
}
c.mu.Lock()
c.Ability = ability
c.mu.Unlock()
return output, nil
}
func (c *Client) FlushAbilityWithDuration(duration time.Duration) func() {
return func() {
for {
_, err := c.GetAbility()
if err != nil {
break
}
time.Sleep(duration)
}
}
}
func (c *Client) IsAvailable(uri string, method string) bool {
return c.Ability[uri][method][ApiVersion] == 1
}