app_store_connect/
bundle_api.rs

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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use crate::{profile_api::ProfilesResponse, AppStoreConnectClient, Result};
use serde::{Deserialize, Serialize};

const APPLE_BUNDLE_IDS_URL: &str = "https://api.appstoreconnect.apple.com/v1/bundleIds";
const APPLE_BUNDLE_CAPABILITIES_URL: &str =
    "https://api.appstoreconnect.apple.com/v1/bundleIdCapabilities";

impl AppStoreConnectClient {
    pub fn register_bundle_id(&self, identifier: &str, name: &str) -> Result<BundleIdResponse> {
        let token = self.get_token()?;
        let body = BundleIdCreateRequest {
            data: BundleIdCreateRequestData {
                attributes: BundleIdCreateRequestAttributes {
                    identifier: identifier.into(),
                    name: name.into(),
                    platform: "UNIVERSAL".into(),
                },
                r#type: "bundleIds".into(),
            },
        };
        let req = self
            .client
            .post(APPLE_BUNDLE_IDS_URL)
            .bearer_auth(token)
            .header("Accept", "application/json")
            .header("Content-Type", "application/json")
            .json(&body);
        Ok(self.send_request(req)?.json()?)
    }

    pub fn list_bundle_ids(&self) -> Result<BundleIdsResponse> {
        let token = self.get_token()?;
        let req = self
            .client
            .get(APPLE_BUNDLE_IDS_URL)
            .bearer_auth(token)
            .header("Accept", "application/json");
        Ok(self.send_request(req)?.json()?)
    }

    pub fn get_bundle_id(&self, id: &str) -> Result<BundleIdResponse> {
        let token = self.get_token()?;
        let req = self
            .client
            .get(format!("{APPLE_BUNDLE_IDS_URL}/{id}"))
            .bearer_auth(token)
            .header("Accept", "application/json");
        Ok(self.send_request(req)?.json()?)
    }

    pub fn list_bundle_profiles(&self, id: &str) -> Result<ProfilesResponse> {
        let token = self.get_token()?;
        let req = self
            .client
            .get(format!("{APPLE_BUNDLE_IDS_URL}/{id}/profiles"))
            .bearer_auth(token)
            .header("Accept", "application/json");
        Ok(self.send_request(req)?.json()?)
    }

    pub fn list_bundle_capabilities(&self, id: &str) -> Result<BundleCapabilitiesResponse> {
        let token = self.get_token()?;
        let req = self
            .client
            .get(format!("{APPLE_BUNDLE_IDS_URL}/{id}/bundleIdCapabilities"))
            .bearer_auth(token)
            .header("Accept", "application/json");
        Ok(self.send_request(req)?.json()?)
    }

    pub fn enable_bundle_id_capability(
        &self,
        id: &str,
        capability: BundleIdCapabilityCreateRequestDataAttributes,
    ) -> Result<()> {
        let token = self.get_token()?;

        let body = BundleIdCapabilityCreateRequest {
            data: BundleIdCapabilityCreateRequestData {
                attributes: capability,
                relationships: BundleIdCapabilityCreateRequestDataRelationships {
                    bundle_id: BundleIdCapabilityCreateRequestDataRelationshipBundleId {
                        data: BundleIdCapabilityCreateRequestDataRelationshipBundleIdData {
                            id: id.to_string(),
                            r#type: "bundleIds".to_string(),
                        },
                    },
                },
                r#type: "bundleIdCapabilities".to_string(),
            },
        };

        let req = self
            .client
            .post(APPLE_BUNDLE_CAPABILITIES_URL)
            .bearer_auth(token)
            .header("Accept", "application/json")
            .json(&body);
        self.send_request(req)?;
        Ok(())
    }

    pub fn delete_bundle_id(&self, id: &str) -> Result<()> {
        let token = self.get_token()?;
        let req = self
            .client
            .delete(format!("{APPLE_BUNDLE_IDS_URL}/{id}"))
            .bearer_auth(token);
        self.send_request(req)?;
        Ok(())
    }
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BundleIdCreateRequest {
    pub data: BundleIdCreateRequestData,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BundleIdCreateRequestData {
    pub attributes: BundleIdCreateRequestAttributes,
    pub r#type: String,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BundleIdCreateRequestAttributes {
    pub identifier: String,
    pub name: String,
    pub platform: String,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, clap::ValueEnum)]
pub enum BundleIdPlatform {
    Ios,
    MacOs,
}

impl std::fmt::Display for BundleIdPlatform {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let s = match self {
            Self::Ios => "IOS",
            Self::MacOs => "MAC_OS",
        };
        write!(f, "{s}")
    }
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BundleIdResponse {
    pub data: BundleId,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BundleIdsResponse {
    pub data: Vec<BundleId>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BundleId {
    pub attributes: BundleIdAttributes,
    pub id: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BundleIdAttributes {
    pub identifier: String,
    pub name: String,
    pub platform: String,
    pub seed_id: String,
}

#[derive(Debug, Deserialize)]
pub struct BundleCapabilitiesResponse {
    pub data: Vec<BundleCapability>,
}

#[derive(Debug, Deserialize)]
pub struct BundleCapability {
    pub attributes: BundleCapabilityAttributes,
    pub id: String,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BundleCapabilityAttributes {
    pub capability_type: String,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BundleIdCapabilityCreateRequest {
    data: BundleIdCapabilityCreateRequestData,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BundleIdCapabilityCreateRequestData {
    attributes: BundleIdCapabilityCreateRequestDataAttributes,
    relationships: BundleIdCapabilityCreateRequestDataRelationships,
    r#type: String,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BundleIdCapabilityCreateRequestDataAttributes {
    pub capability_type: String,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BundleIdCapabilityCreateRequestDataRelationships {
    bundle_id: BundleIdCapabilityCreateRequestDataRelationshipBundleId,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BundleIdCapabilityCreateRequestDataRelationshipBundleId {
    data: BundleIdCapabilityCreateRequestDataRelationshipBundleIdData,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BundleIdCapabilityCreateRequestDataRelationshipBundleIdData {
    id: String,
    r#type: String,
}