This repository has been archived by the owner on Nov 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rtm_reflection.go
72 lines (60 loc) · 1.61 KB
/
rtm_reflection.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
package rtm
import (
"context"
"encoding/json"
"sort"
)
type ReflectionService struct {
client *Client
}
type MethodInfo struct {
Name string
NeedsLogin bool
}
// https://www.rememberthemilk.com/services/api/methods/rtm.reflection.getMethodInfo.rtm
func (r *ReflectionService) GetMethodInfo(ctx context.Context, method string) (*MethodInfo, error) {
b, err := r.client.Call(ctx, "rtm.reflection.getMethodInfo", Args{"method_name": method})
if err != nil {
return nil, err
}
return r.getMethodInfoUnmarshal(b)
}
func (r *ReflectionService) getMethodInfoUnmarshal(b []byte) (*MethodInfo, error) {
var resp struct {
Rsp struct {
Method struct {
Name string `json:"name"`
NeedsLogin rtmBool `json:"needslogin"`
} `json:"method"`
} `json:"rsp"`
}
if err := json.Unmarshal(b, &resp); err != nil {
return nil, err
}
return &MethodInfo{
Name: resp.Rsp.Method.Name,
NeedsLogin: bool(resp.Rsp.Method.NeedsLogin),
}, nil
}
// https://www.rememberthemilk.com/services/api/methods/rtm.reflection.getMethods.rtm
func (r *ReflectionService) GetMethods(ctx context.Context) ([]string, error) {
b, err := r.client.Call(ctx, "rtm.reflection.getMethods", nil)
if err != nil {
return nil, err
}
return r.getMethodsUnmarshal(b)
}
func (r *ReflectionService) getMethodsUnmarshal(b []byte) ([]string, error) {
var resp struct {
Rsp struct {
Methods struct {
Method []string `json:"method"`
} `json:"methods"`
} `json:"rsp"`
}
if err := json.Unmarshal(b, &resp); err != nil {
return nil, err
}
sort.Strings(resp.Rsp.Methods.Method)
return resp.Rsp.Methods.Method, nil
}