-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBriefcaseOperations.ts
More file actions
162 lines (152 loc) · 5.63 KB
/
BriefcaseOperations.ts
File metadata and controls
162 lines (152 loc) · 5.63 KB
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import {
BriefcaseResponse,
BriefcasesResponse,
CheckpointResponse,
EntityListIteratorImpl,
OperationsBase,
} from "../../base/internal";
import {
AuthorizationCallback,
Briefcase,
EntityListIterator,
HeaderFactories,
HttpResponse,
MinimalBriefcase,
PreferReturn,
} from "../../base/types";
import { IModelsClient } from "../../IModelsClient";
import { OperationOptions } from "../OperationOptions";
import { assertLink, getUser } from "../SharedFunctions";
import {
GetBriefcaseListParams,
GetSingleBriefcaseParams,
} from "./BriefcaseOperationParams";
export class BriefcaseOperations<
TOptions extends OperationOptions
> extends OperationsBase<TOptions> {
constructor(options: TOptions, private _iModelsClient: IModelsClient) {
super(options);
}
/**
* Gets Briefcases of a specific iModel. This method returns Briefcases in their minimal representation. The returned iterator
* internally queries entities in pages. Wraps the
* {@link https://developer.bentley.com/apis/imodels-v2/operations/get-imodel-briefcases/ Get iModel Briefcases}
* operation from iModels API.
* @param {GetBriefcaseListParams} params parameters for this operation. See {@link GetBriefcaseListParams}.
* @returns {EntityListIterator<MinimalBriefcase>} iterator for Briefcase list. See {@link EntityListIterator},
* {@link MinimalBriefcase}.
*/
public getMinimalList(
params: GetBriefcaseListParams
): EntityListIterator<MinimalBriefcase> {
return new EntityListIteratorImpl(async () =>
this.getEntityCollectionPage<
MinimalBriefcase,
BriefcasesResponse<MinimalBriefcase>
>({
authorization: params.authorization,
url: this._options.urlFormatter.getBriefcaseListUrl({
iModelId: params.iModelId,
urlParams: params.urlParams,
}),
preferReturn: PreferReturn.Minimal,
entityCollectionAccessor: (response) => response.body.briefcases,
headers: params.headers,
})
);
}
/**
* Gets Briefcases of a specific iModel. This method returns Briefcases in their full representation. The returned iterator
* internally queries entities in pages. Wraps the
* {@link https://developer.bentley.com/apis/imodels-v2/operations/get-imodel-briefcases/ Get iModel Briefcases}
* operation from iModels API.
* @param {GetBriefcaseListParams} params parameters for this operation. See {@link GetBriefcaseListParams}.
* @returns {EntityListIterator<Briefcase>} iterator for Briefcase list. See {@link EntityListIterator}, {@link Briefcase}.
*/
public getRepresentationList(
params: GetBriefcaseListParams
): EntityListIterator<Briefcase> {
const entityCollectionAccessor = (
response: HttpResponse<BriefcasesResponse<Briefcase>>
) => {
const briefcases = response.body.briefcases;
const mappedBriefcases = briefcases.map((briefcase) =>
this.appendRelatedEntityCallbacks(
params.authorization,
briefcase,
params.headers
)
);
return mappedBriefcases;
};
return new EntityListIteratorImpl(async () =>
this.getEntityCollectionPage<Briefcase, BriefcasesResponse<Briefcase>>({
authorization: params.authorization,
url: this._options.urlFormatter.getBriefcaseListUrl({
iModelId: params.iModelId,
urlParams: params.urlParams,
}),
preferReturn: PreferReturn.Representation,
entityCollectionAccessor,
headers: params.headers,
})
);
}
/**
* Gets a single Briefcase by its id. This method returns a Briefcase in its full representation. Wraps the
* {@link https://developer.bentley.com/apis/imodels-v2/operations/get-imodel-briefcase-details/ Get iModel Briefcase}
* operation from iModels API.
* @param {GetSingleBriefcaseParams} params parameters for this operation. See {@link GetSingleBriefcaseParams}.
* @returns {Promise<Briefcase>} an Briefcase with specified id. See {@link iModel}.
*/
public async getSingle(params: GetSingleBriefcaseParams): Promise<Briefcase> {
const response = await this.sendGetRequest<BriefcaseResponse>({
authorization: params.authorization,
url: this._options.urlFormatter.getSingleBriefcaseUrl({
iModelId: params.iModelId,
briefcaseId: params.briefcaseId,
}),
headers: params.headers,
});
const result: Briefcase = this.appendRelatedEntityCallbacks(
params.authorization,
response.body.briefcase,
params.headers
);
return result;
}
protected appendRelatedEntityCallbacks(
authorization: AuthorizationCallback,
briefcase: Briefcase,
headers?: HeaderFactories
): Briefcase {
const getOwner = async () =>
getUser(
authorization,
this._iModelsClient.users,
this._options.urlFormatter,
briefcase._links.owner?.href,
headers
);
const checkpointLink = briefcase._links.checkpoint;
assertLink(checkpointLink);
const getCheckpoint = async () => {
const response = await this.sendGetRequest<CheckpointResponse>({
authorization,
url: checkpointLink.href,
headers,
});
return response.body.checkpoint;
};
const result: Briefcase = {
...briefcase,
getOwner,
getCheckpoint,
};
return result;
}
}