-
Notifications
You must be signed in to change notification settings - Fork 1
/
device.go
154 lines (140 loc) · 4.4 KB
/
device.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package appleapi
import (
"encoding/json"
"strconv"
"strings"
)
type DeviceCreateRequest struct {
Data struct {
Attributes struct {
Name string `json:"name,omitempty"`
Platform string `json:"platform,omitempty"`
Udid string `json:"udid,omitempty"`
} `json:"attributes,omitempty"`
Type string `json:"type,omitempty"` // "devices"
} `json:"data,omitempty"`
}
type DeviceUpdateRequest struct {
Data struct {
Attributes struct {
Name string `json:"name,omitempty"`
Status string `json:"status,omitempty"` // Possible values: ENABLED, DISABLED
} `json:"attributes,omitempty"`
Id string `json:"id,omitempty"`
Type string `json:"type,omitempty"` // "devices"
} `json:"data,omitempty"`
}
type Device struct {
Attributes struct {
DeviceClass string `json:"deviceClass,omitempty"` //Possible values: APPLE_WATCH, IPAD, IPHONE, IPOD, APPLE_TV, MAC
Model string `json:"model,omitempty"`
Name string `json:"name,omitempty"`
Platform string `json:"platform,omitempty"` // IOS,MAC_OS
Status string `json:"status,omitempty"` // Possible values: ENABLED, DISABLED
Udid string `json:"udid,omitempty"`
AddedDate string `json:"addedDate,omitempty"`
} `json:"attributes,omitempty"`
Id string `json:"id,omitempty"`
Type string `json:"type,omitempty"` // "devices"
Links ResourceLinks `json:"links,omitempty"`
}
type DeviceResponse struct {
Data Device `json:"data,omitempty"`
Links DocumentLinks `json:"links,omitempty"`
}
type DevicesResponse struct {
Data []Device `json:"data,omitempty"`
Links DocumentLinks `json:"links,omitempty"`
Mate PagingInformation `json:"mate,omitempty"`
}
// https://developer.apple.com/documentation/appstoreconnectapi/list_devices
type ListDevicesQuery struct {
Devices string `json:"devices,omitempty"` //Possible values: addedDate, deviceClass, model, name, platform, status, udid
Id string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Platform string `json:"platform,omitempty"`
Status string `json:"status,omitempty"`
Udid string `json:"udid,omitempty"`
Limit int `json:"limit,omitempty"` //Maximum: 200
Sort string `json:"sort,omitempty"` //Possible values: id, -id, name, -name, platform, -platform, status, -status, udid, -udid
}
func (q *ListDevicesQuery) QueryString() string {
sb := strings.Builder{}
if "" != q.Devices {
sb.WriteString("&fields[devices]=")
sb.WriteString(q.Devices)
}
if "" != q.Id {
sb.WriteString("&filter[id]=")
sb.WriteString(q.Id)
}
if "" != q.Name {
sb.WriteString("&filter[name]=")
sb.WriteString(q.Name)
}
if "" != q.Platform {
sb.WriteString("&filter[platform]=")
sb.WriteString(q.Platform)
}
if "" != q.Status {
sb.WriteString("&filter[status]=")
sb.WriteString(q.Status)
}
if "" != q.Udid {
sb.WriteString("&filter[udid]=")
sb.WriteString(q.Udid)
}
if q.Limit > 0 {
sb.WriteString("&limit=")
sb.WriteString(strconv.Itoa(q.Limit))
}
if "" != q.Sort {
sb.WriteString("&sort=")
sb.WriteString(q.Sort)
}
if sb.Len() > 1 {
return sb.String()[1:sb.Len()]
}
return sb.String()
}
type Devices struct {
*Token
}
// https://developer.apple.com/documentation/appstoreconnectapi/list_devices
func (c *Devices) Query(query *ListDevicesQuery) ([]byte, error) {
url := "https://api.appstoreconnect.apple.com/v1/devices"
if query != nil {
url += "?" + query.QueryString()
}
return c.WebGet(url)
}
// 增加设备
// https://developer.apple.com/documentation/appstoreconnectapi/register_a_new_device
func (c *Devices) DeviceCreate(udid, name string) ([]byte, error) {
req := new(DeviceCreateRequest)
req.Data.Type = "devices"
req.Data.Attributes.Platform = PlatformIos
req.Data.Attributes.Name = name
req.Data.Attributes.Udid = udid
url := "https://api.appstoreconnect.apple.com/v1/devices"
reqJson, err := json.Marshal(req)
if err != nil {
return nil, err
}
return c.WebPost(url, reqJson)
}
// 更新设备
// https://developer.apple.com/documentation/appstoreconnectapi/modify_a_registered_device
func (c *Devices) DeviceUpdate(id, name, status string) ([]byte, error) {
req := new(DeviceUpdateRequest)
req.Data.Type = "devices"
req.Data.Id = id
req.Data.Attributes.Name = name
req.Data.Attributes.Status = status
url := "https://api.appstoreconnect.apple.com/v1/devices/" + id
reqJson, err := json.Marshal(req)
if err != nil {
return nil, err
}
return c.WebPost(url, reqJson)
}