@itwin/insights-client
Version:
Insights client for the iTwin platform
66 lines • 3.49 kB
JavaScript
import { RequiredError } from "../../common/Errors";
import { EntityListIteratorImpl } from "../../common/iterators/EntityListIteratorImpl";
import { getEntityCollectionPage } from "../../common/iterators/IteratorUtil";
import { CARBON_CALCULATION_BASE_PATH, OperationsBase } from "../../common/OperationsBase";
export class EC3ConfigurationsClient extends OperationsBase {
constructor(basePath) {
super(basePath ?? CARBON_CALCULATION_BASE_PATH);
}
async getConfigurations(accessToken, projectId, top) {
const configurations = [];
const configIterator = this.getConfigurationsIterator(accessToken, projectId, top);
for await (const config of configIterator) {
configurations.push(config);
}
return configurations;
}
getConfigurationsIterator(accessToken, projectId, top) {
if (!this.topIsValid(top)) {
throw new RequiredError("top", "Parameter top was outside of the valid range [1-1000].");
}
let url = `${this.basePath}/ec3/configurations?iTwinId=${projectId}`;
url += top ? `&$top=${top}` : "";
const request = this.createRequest("GET", accessToken);
return new EntityListIteratorImpl(async () => getEntityCollectionPage(url, async (nextUrl) => {
const response = await this.fetchJSON(nextUrl, request);
return {
values: response.configurations,
// eslint-disable-next-line @typescript-eslint/naming-convention
_links: response._links,
};
}));
}
async getConfiguration(accessToken, configurationId) {
const url = `${this.basePath}/ec3/configurations/${configurationId}`;
const requestOptions = this.createRequest("GET", accessToken);
return (await this.fetchJSON(url, requestOptions)).configuration;
}
async createConfiguration(accessToken, configuration) {
if (configuration.labels.length === 0) {
throw new RequiredError("configuration", "Required field labels was empty.");
}
if (configuration.labels.some((x) => x.materials.length === 0)) {
throw new RequiredError("configuration", "Required field materials was empty.");
}
const url = `${this.basePath}/ec3/configurations`;
const requestOptions = this.createRequest("POST", accessToken, JSON.stringify(configuration));
return (await this.fetchJSON(url, requestOptions)).configuration;
}
async updateConfiguration(accessToken, configurationId, configuration) {
if (configuration.labels.length === 0) {
throw new RequiredError("configuration", "Required field labels was empty.");
}
if (configuration.labels.some((x) => x.materials.length === 0)) {
throw new RequiredError("configuration", "Required field materials was empty.");
}
const url = `${this.basePath}/ec3/configurations/${configurationId}`;
const requestOptions = this.createRequest("PUT", accessToken, JSON.stringify(configuration));
return (await this.fetchJSON(url, requestOptions)).configuration;
}
async deleteConfiguration(accessToken, configurationId) {
const url = `${this.basePath}/ec3/configurations/${configurationId}`;
const requestOptions = this.createRequest("DELETE", accessToken);
return this.fetchJSON(url, requestOptions);
}
}
//# sourceMappingURL=EC3ConfigurationsClient.js.map