UNPKG

@itwin/insights-client

Version:

Insights client for the iTwin platform

147 lines 8.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ReportsClient = void 0; const Errors_1 = require("../../common/Errors"); const EntityListIteratorImpl_1 = require("../../common/iterators/EntityListIteratorImpl"); const IteratorUtil_1 = require("../../common/iterators/IteratorUtil"); const OperationsBase_1 = require("../../common/OperationsBase"); class ReportsClient extends OperationsBase_1.OperationsBase { constructor(basePath) { super(basePath ?? OperationsBase_1.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 Errors_1.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_1.EntityListIteratorImpl(async () => (0, IteratorUtil_1.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 Errors_1.RequiredError("displayName", "Required field displayName was null or undefined."); } if (!report.projectId) { throw new Errors_1.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 Errors_1.RequiredError("report", "All fields of report were null or undefined."); } if (report.displayName === "") { throw new Errors_1.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 Errors_1.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_1.EntityListIteratorImpl(async () => (0, IteratorUtil_1.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 Errors_1.RequiredError("imodelId", "Required field imodelId was null or undefined."); } if (!reportMapping.mappingId) { throw new Errors_1.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 Errors_1.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_1.EntityListIteratorImpl(async () => (0, IteratorUtil_1.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 Errors_1.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); } } exports.ReportsClient = ReportsClient; //# sourceMappingURL=ReportsClient.js.map