forked from NativeScript/nativescript-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapple-portal-application-service.ts
86 lines (76 loc) · 2.29 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
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
import { IErrors, Server } from "../../common/declarations";
import { injector } from "../../common/yok";
import * as _ from "lodash";
import {
IApplePortalUserDetail,
IApplePortalSessionService,
IApplePortalApplicationService,
IApplePortalApplicationSummary,
IApplePortalApplication,
} from "./definitions";
export class ApplePortalApplicationService
implements IApplePortalApplicationService {
constructor(
private $applePortalSessionService: IApplePortalSessionService,
private $errors: IErrors,
private $httpClient: Server.IHttpClient
) {}
public async getApplications(
user: IApplePortalUserDetail
): Promise<IApplePortalApplicationSummary[]> {
let result: IApplePortalApplicationSummary[] = [];
for (const account of user.associatedAccounts) {
const contentProviderId = account.contentProvider.contentProviderPublicId;
const applications = await this.getApplicationsByProvider(
contentProviderId
);
result = result.concat(applications.summaries);
}
return result;
}
public async getApplicationsByProvider(
contentProviderId: string
): Promise<IApplePortalApplication> {
const webSessionCookie = await this.$applePortalSessionService.createWebSession(
contentProviderId
);
const response = await this.$httpClient.httpRequest({
url:
"https://appstoreconnect.apple.com/WebObjects/iTunesConnect.woa/ra/apps/manageyourapps/summary/v2",
method: "GET",
body: {
contentProviderId,
},
headers: {
"Content-Type": "application/json",
Cookie: webSessionCookie,
},
});
return JSON.parse(response.body).data;
}
public async getApplicationByBundleId(
user: IApplePortalUserDetail,
bundleId: string
): Promise<IApplePortalApplicationSummary> {
const applications = await this.getApplications(user);
if (!applications || !applications.length) {
this.$errors.fail(
`Cannot find any registered applications for Apple ID ${user.userName} in iTunes Connect.`
);
}
const application = _.find(
applications,
(app) => app.bundleId === bundleId
);
if (!application) {
this.$errors.fail(
`Cannot find registered applications that match the specified identifier ${bundleId} in iTunes Connect.`
);
}
return application;
}
}
injector.register(
"applePortalApplicationService",
ApplePortalApplicationService
);