app_store_connect/
profile_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
// 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::{
    bundle_api::BundleIdResponse, certs_api::CertificatesResponse, AppStoreConnectClient, Result,
};
use serde::{Deserialize, Serialize};

const APPLE_PROFILES_URL: &str = "https://api.appstoreconnect.apple.com/v1/profiles";

impl AppStoreConnectClient {
    pub fn create_profile(
        &self,
        name: &str,
        profile_type: ProfileType,
        bundle_id: &str,
        certificates: &[String],
        devices: Option<&[String]>,
    ) -> Result<ProfileResponse> {
        let token = self.get_token()?;
        let body = ProfileCreateRequest {
            data: ProfileCreateRequestData {
                attributes: ProfileCreateRequestAttributes {
                    name: name.into(),
                    profile_type: profile_type.to_string(),
                },
                relationships: ProfileCreateRequestRelationships {
                    bundle_id: Ref {
                        data: RefData {
                            id: bundle_id.into(),
                            r#type: "bundleIds".into(),
                        },
                    },
                    certificates: Refs {
                        data: certificates
                            .iter()
                            .map(|certificate| RefData {
                                id: certificate.into(),
                                r#type: "certificates".into(),
                            })
                            .collect(),
                    },
                    devices: devices.map(|devices| Refs {
                        data: devices
                            .iter()
                            .map(|device| RefData {
                                id: device.into(),
                                r#type: "devices".into(),
                            })
                            .collect(),
                    }),
                },
                r#type: "profiles".into(),
            },
        };
        let req = self
            .client
            .post(APPLE_PROFILES_URL)
            .bearer_auth(token)
            .header("Accept", "application/json")
            .header("Content-Type", "application/json")
            .json(&body);
        Ok(self.send_request(req)?.json()?)
    }

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

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

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

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

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

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

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

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

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ProfileCreateRequestRelationships {
    pub bundle_id: Ref,
    pub certificates: Refs,
    pub devices: Option<Refs>,
}

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

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Refs {
    pub data: Vec<RefData>,
}

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

#[derive(Clone, Copy, Debug, Eq, PartialEq, clap::ValueEnum)]
pub enum ProfileType {
    IosAppDevelopment,
    MacAppDevelopment,
    IosAppStore,
    MacAppStore,
    MacAppDirect,
}

impl std::fmt::Display for ProfileType {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let s = match self {
            Self::IosAppDevelopment => "IOS_APP_DEVELOPMENT",
            Self::MacAppDevelopment => "MAC_APP_DEVELOPMENT",
            Self::IosAppStore => "IOS_APP_STORE",
            Self::MacAppStore => "MAC_APP_STORE",
            Self::MacAppDirect => "MAC_APP_DIRECT",
        };
        write!(f, "{s}")
    }
}

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

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

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

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProfileAttributes {
    pub name: String,
    pub platform: String,
    pub profile_content: String,
    pub uuid: String,
    pub created_date: Option<String>,
    pub profile_state: String,
    pub profile_type: String,
    pub expiration_date: String,
}