-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathapple-portal-application-service.ts
54 lines (45 loc) · 2.1 KB
/
apple-portal-application-service.ts
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
export class ApplePortalApplicationService implements IApplePortalApplicationService {
constructor(
private $applePortalSessionService: IApplePortalSessionService,
private $errors: IErrors,
private $httpClient: Server.IHttpClient
) { }
public async getApplications(credentials: ICredentials): Promise<IApplePortalApplicationSummary[]> {
let result: IApplePortalApplicationSummary[] = [];
const user = await this.$applePortalSessionService.createUserSession(credentials);
for (const account of user.associatedAccounts) {
const contentProviderId = account.contentProvider.contentProviderId;
const dsId = user.sessionToken.dsId;
const applications = await this.getApplicationsByProvider(contentProviderId, dsId);
result = result.concat(applications.summaries);
}
return result;
}
public async getApplicationsByProvider(contentProviderId: number, dsId: string): Promise<IApplePortalApplication> {
const webSessionCookie = await this.$applePortalSessionService.createWebSession(contentProviderId, dsId);
const response = await this.$httpClient.httpRequest({
url: "https://appstoreconnect.apple.com/WebObjects/iTunesConnect.woa/ra/apps/manageyourapps/summary/v2",
method: "GET",
body: JSON.stringify({
contentProviderId
}),
headers: {
'Content-Type': 'application/json',
'Cookie': webSessionCookie
}
});
return JSON.parse(response.body).data;
}
public async getApplicationByBundleId(credentials: ICredentials, bundleId: string): Promise<IApplePortalApplicationSummary> {
const applications = await this.getApplications(credentials);
if (!applications || !applications.length) {
this.$errors.failWithoutHelp(`Cannot find any registered applications for Apple ID ${credentials.username} in iTunes Connect.`);
}
const application = _.find(applications, app => app.bundleId === bundleId);
if (!application) {
this.$errors.failWithoutHelp(`Cannot find registered applications that match the specified identifier ${bundleId} in iTunes Connect.`);
}
return application;
}
}
$injector.register("applePortalApplicationService", ApplePortalApplicationService);