@itwin/insights-client
Version:
Insights client for the iTwin platform
143 lines • 7.64 kB
JavaScript
import { RequiredError } from "../../common/Errors";
import { EntityListIteratorImpl } from "../../common/iterators/EntityListIteratorImpl";
import { getEntityCollectionPage } from "../../common/iterators/IteratorUtil";
import { OperationsBase, REPORTING_BASE_PATH } from "../../common/OperationsBase";
export class ReportsClient extends OperationsBase {
constructor(basePath) {
super(basePath ?? REPORTING_BASE_PATH);
}
async getReports(accessToken, projectId, top, deleted = false) {
const reports = [];
const reportIterator = this.getReportsIterator(accessToken, projectId, top, deleted);
for await (const report of reportIterator) {
reports.push(report);
}
return reports;
}
getReportsIterator(accessToken, projectId, top, deleted = false) {
if (!this.topIsValid(top)) {
throw new RequiredError("top", "Parameter top was outside of the valid range [1-1000].");
}
let url = `${this.basePath}/reports?projectId=${encodeURIComponent(projectId)}&deleted=${encodeURIComponent(deleted)}`;
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.reports,
// eslint-disable-next-line @typescript-eslint/naming-convention
_links: response._links,
};
}));
}
async getReport(accessToken, reportId) {
const url = `${this.basePath}/reports/${encodeURIComponent(reportId)}`;
const requestOptions = this.createRequest("GET", accessToken);
return (await this.fetchJSON(url, requestOptions)).report;
}
async createReport(accessToken, report) {
if (!report.displayName) {
throw new RequiredError("displayName", "Required field displayName was null or undefined.");
}
if (!report.projectId) {
throw new RequiredError("projectId", "Required field projectId was null or undefined.");
}
const url = `${this.basePath}/reports/`;
const requestOptions = this.createRequest("POST", accessToken, JSON.stringify(report));
return (await this.fetchJSON(url, requestOptions)).report;
}
async updateReport(accessToken, reportId, report) {
if (report.deleted == null && report.description == null && report.displayName == null) {
throw new RequiredError("report", "All fields of report were null or undefined.");
}
if (report.displayName === "") {
throw new RequiredError("displayName", "Field displayName was empty.");
}
const url = `${this.basePath}/reports/${encodeURIComponent(reportId)}`;
const requestOptions = this.createRequest("PATCH", accessToken, JSON.stringify(report));
return (await this.fetchJSON(url, requestOptions)).report;
}
async deleteReport(accessToken, reportId) {
const url = `${this.basePath}/reports/${encodeURIComponent(reportId)}`;
const requestOptions = this.createRequest("DELETE", accessToken);
return this.fetchJSON(url, requestOptions);
}
async getReportMappings(accessToken, reportId, top) {
const reportMappings = [];
const reportMappingIterator = this.getReportMappingsIterator(accessToken, reportId, top);
for await (const reportMapping of reportMappingIterator) {
reportMappings.push(reportMapping);
}
return reportMappings;
}
getReportMappingsIterator(accessToken, reportId, top) {
if (!this.topIsValid(top)) {
throw new RequiredError("top", "Parameter top was outside of the valid range [1-1000].");
}
let url = `${this.basePath}/reports/${encodeURIComponent(reportId)}/datasources/imodelMappings`;
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.mappings,
// eslint-disable-next-line @typescript-eslint/naming-convention
_links: response._links,
};
}));
}
async createReportMapping(accessToken, reportId, reportMapping) {
if (!reportMapping.imodelId) {
throw new RequiredError("imodelId", "Required field imodelId was null or undefined.");
}
if (!reportMapping.mappingId) {
throw new RequiredError("mappingId", "Required field mappingId was null or undefined.");
}
const url = `${this.basePath}/reports/${encodeURIComponent(reportId)}/datasources/imodelMappings`;
const requestOptions = this.createRequest("POST", accessToken, JSON.stringify(reportMapping));
return (await this.fetchJSON(url, requestOptions)).mapping;
}
async deleteReportMapping(accessToken, reportId, reportMappingId) {
const url = `${this.basePath}/reports/${encodeURIComponent(reportId)}/datasources/imodelMappings/${encodeURIComponent(reportMappingId)}`;
const requestOptions = this.createRequest("DELETE", accessToken);
return this.fetchJSON(url, requestOptions);
}
async getReportAggregations(accessToken, reportId, top) {
const aggregations = [];
const reportAggregationIterator = this.getReportAggregationsIterator(accessToken, reportId, top);
for await (const reportAggregation of reportAggregationIterator) {
aggregations.push(reportAggregation);
}
return aggregations;
}
getReportAggregationsIterator(accessToken, reportId, top) {
if (!this.topIsValid(top)) {
throw new RequiredError("top", "Parameter top was outside of the valid range [1-1000].");
}
let url = `${this.basePath}/reports/${encodeURIComponent(reportId)}/datasources/aggregations`;
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.aggregations,
// eslint-disable-next-line @typescript-eslint/naming-convention
_links: response._links,
};
}));
}
async createReportAggregation(accessToken, reportId, aggregation) {
if (!aggregation.aggregationTableSetId) {
throw new RequiredError("aggregationTableSetId", "Required field aggregationTableSetId was null or undefined.");
}
const url = `${this.basePath}/reports/${encodeURIComponent(reportId)}/datasources/aggregations`;
const requestOptions = this.createRequest("POST", accessToken, JSON.stringify(aggregation));
return (await this.fetchJSON(url, requestOptions)).aggregation;
}
async deleteReportAggregation(accessToken, reportId, aggregationTableSetId) {
const url = `${this.basePath}/reports/${encodeURIComponent(reportId)}/datasources/aggregations/${encodeURIComponent(aggregationTableSetId)}`;
const requestOptions = this.createRequest("DELETE", accessToken);
return this.fetchJSON(url, requestOptions);
}
}
//# sourceMappingURL=ReportsClient.js.map