@itwin/insights-client
Version:
Insights client for the iTwin platform
194 lines • 12.4 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";
import { AggregationPropertyType, } from "../interfaces/AggregationProperties";
export class AggregationsClient extends OperationsBase {
constructor(basePath) {
super(basePath ?? REPORTING_BASE_PATH);
}
async getAggregationProperties(accessToken, aggregationTableSetId, aggregationTableId, top) {
const properties = [];
const aggregationPropertyIterator = this.getAggregationPropertiesIterator(accessToken, aggregationTableSetId, aggregationTableId, top);
for await (const aggregationProperty of aggregationPropertyIterator) {
properties.push(aggregationProperty);
}
return properties;
}
getAggregationPropertiesIterator(accessToken, aggregationTableSetId, aggregationTableId, top) {
if (!this.topIsValid(top)) {
throw new RequiredError("top", "Parameter top was outside of the valid range [1-1000].");
}
let url = `${this.basePath}/datasources/aggregations/${encodeURIComponent(aggregationTableSetId)}/tables/${encodeURIComponent(aggregationTableId)}/properties`;
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.aggregationProperties,
// eslint-disable-next-line @typescript-eslint/naming-convention
_links: response._links,
};
}));
}
async getAggregationProperty(accessToken, aggregationTableSetId, aggregationTableId, aggregationPropertyId) {
const url = `${this.basePath}/datasources/aggregations/${encodeURIComponent(aggregationTableSetId)}/tables/${encodeURIComponent(aggregationTableId)}/properties/${encodeURIComponent(aggregationPropertyId)}`;
const requestOptions = this.createRequest("GET", accessToken);
return (await this.fetchJSON(url, requestOptions)).aggregationProperty;
}
async updateAggregationProperty(accessToken, aggregationTableSetId, aggregationTableId, aggregationPropertyId, property) {
if (null == property.propertyName && null == property.sourcePropertyName && null == property.type) {
throw new RequiredError("property", "All properties of property were missing.");
}
if (null != property.propertyName && !this.isSimpleIdentifier(property.propertyName)) {
throw new RequiredError("propertyName", "Field propertyName was invalid.");
}
if (null != property.sourcePropertyName && !this.isSimpleIdentifier(property.sourcePropertyName)) {
throw new RequiredError("sourcePropertyName", "Field sourcePropertyName was invalid.");
}
if (null != property.type && property.type === AggregationPropertyType.Undefined) {
throw new RequiredError("type", "Required field type was null or undefined.");
}
const url = `${this.basePath}/datasources/aggregations/${encodeURIComponent(aggregationTableSetId)}/tables/${encodeURIComponent(aggregationTableId)}/properties/${encodeURIComponent(aggregationPropertyId)}`;
const requestOptions = this.createRequest("PATCH", accessToken, JSON.stringify(property));
return (await this.fetchJSON(url, requestOptions)).aggregationProperty;
}
async createAggregationProperty(accessToken, aggregationTableSetId, aggregationTableId, property) {
if (!this.isSimpleIdentifier(property.propertyName)) {
throw new RequiredError("propertyName", "Field propertyName was invalid.");
}
if (!this.isSimpleIdentifier(property.sourcePropertyName)) {
throw new RequiredError("sourcePropertyName", "Field sourcePropertyName was invalid.");
}
if (property.type === AggregationPropertyType.Undefined) {
throw new RequiredError("type", "Required field type was null or undefined.");
}
const url = `${this.basePath}/datasources/aggregations/${encodeURIComponent(aggregationTableSetId)}/tables/${encodeURIComponent(aggregationTableId)}/properties`;
const requestOptions = this.createRequest("POST", accessToken, JSON.stringify(property));
return (await this.fetchJSON(url, requestOptions)).aggregationProperty;
}
async deleteAggregationProperty(accessToken, aggregationTableSetId, aggregationTableId, aggregationPropertyId) {
const url = `${this.basePath}/datasources/aggregations/${encodeURIComponent(aggregationTableSetId)}/tables/${encodeURIComponent(aggregationTableId)}/properties/${encodeURIComponent(aggregationPropertyId)}`;
const requestOptions = this.createRequest("DELETE", accessToken);
return this.fetchJSON(url, requestOptions);
}
async getAggregationTableSets(accessToken, datasourceId, datasourceType, top) {
const tablesets = [];
const aggregationTableSetsIterator = this.getAggregationTableSetsIterator(accessToken, datasourceId, datasourceType, top);
for await (const aggregationTableSet of aggregationTableSetsIterator) {
tablesets.push(aggregationTableSet);
}
return tablesets;
}
getAggregationTableSetsIterator(accessToken, datasourceId, datasourceType, top) {
if (!this.topIsValid(top)) {
throw new RequiredError("top", "Parameter top was outside of the valid range [1-1000].");
}
let url = `${this.basePath}/datasources/aggregations?datasourceId=${encodeURIComponent(datasourceId)}&datasourceType=${encodeURIComponent(datasourceType)}`;
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.aggregationTableSets,
// eslint-disable-next-line @typescript-eslint/naming-convention
_links: response._links,
};
}));
}
async getAggregationTableSet(accessToken, aggregationTableSetId) {
const url = `${this.basePath}/datasources/aggregations/${encodeURIComponent(aggregationTableSetId)}`;
const requestOptions = this.createRequest("GET", accessToken);
return (await this.fetchJSON(url, requestOptions)).aggregationTableSet;
}
async updateAggregationTableSet(accessToken, aggregationTableSetId, tableset) {
if (null == tableset.tableSetName && null == tableset.description) {
throw new RequiredError("tableset", "All properties of tableset were missing.");
}
if (null != tableset.tableSetName && !this.isSimpleIdentifier(tableset.tableSetName)) {
throw new RequiredError("tableSetName", "Field tableSetName was invalid.");
}
const url = `${this.basePath}/datasources/aggregations/${encodeURIComponent(aggregationTableSetId)}`;
const requestOptions = this.createRequest("PATCH", accessToken, JSON.stringify(tableset));
return (await this.fetchJSON(url, requestOptions)).aggregationTableSet;
}
async createAggregationTableSet(accessToken, tableset) {
if (!this.isSimpleIdentifier(tableset.tableSetName)) {
throw new RequiredError("tableSetName", "Field tableSetName was invalid.");
}
if (!this.isSimpleIdentifier(tableset.datasourceType)) {
throw new RequiredError("datasourceType", "Field datasourceType was invalid.");
}
if (!tableset.datasourceId) {
throw new RequiredError("datasourceId", "Required field datasourceId was null or undefined.");
}
const url = `${this.basePath}/datasources/aggregations`;
const requestOptions = this.createRequest("POST", accessToken, JSON.stringify(tableset));
return (await this.fetchJSON(url, requestOptions)).aggregationTableSet;
}
async deleteAggregationTableSet(accessToken, aggregationTableSetId) {
const url = `${this.basePath}/datasources/aggregations/${encodeURIComponent(aggregationTableSetId)}`;
const requestOptions = this.createRequest("DELETE", accessToken);
return this.fetchJSON(url, requestOptions);
}
async getAggregationTables(accessToken, aggregationTableSetId, top) {
const tables = [];
const aggregationTablesIterator = this.getAggregationTablesIterator(accessToken, aggregationTableSetId, top);
for await (const aggregationTable of aggregationTablesIterator) {
tables.push(aggregationTable);
}
return tables;
}
getAggregationTablesIterator(accessToken, aggregationTableSetId, top) {
if (!this.topIsValid(top)) {
throw new RequiredError("top", "Parameter top was outside of the valid range [1-1000].");
}
let url = `${this.basePath}/datasources/aggregations/${encodeURIComponent(aggregationTableSetId)}/tables`;
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.aggregationTables,
// eslint-disable-next-line @typescript-eslint/naming-convention
_links: response._links,
};
}));
}
async getAggregationTable(accessToken, aggregationTableSetId, aggregationTableId) {
const url = `${this.basePath}/datasources/aggregations/${encodeURIComponent(aggregationTableSetId)}/tables/${encodeURIComponent(aggregationTableId)}`;
const requestOptions = this.createRequest("GET", accessToken);
return (await this.fetchJSON(url, requestOptions)).aggregationTable;
}
async updateAggregationTable(accessToken, aggregationTableSetId, aggregationTableId, table) {
if (null == table.tableName && null == table.description && null == table.sourceTableName) {
throw new RequiredError("table", "All properties of table were missing.");
}
if (null != table.tableName && !this.isSimpleIdentifier(table.tableName)) {
throw new RequiredError("tableName", "Field tableName was invalid.");
}
if (null != table.sourceTableName && !this.isSimpleIdentifier(table.sourceTableName)) {
throw new RequiredError("sourceTableName", "Field sourceTableName was invalid.");
}
const url = `${this.basePath}/datasources/aggregations/${encodeURIComponent(aggregationTableSetId)}/tables/${encodeURIComponent(aggregationTableId)}`;
const requestOptions = this.createRequest("PATCH", accessToken, JSON.stringify(table));
return (await this.fetchJSON(url, requestOptions)).aggregationTable;
}
async createAggregationTable(accessToken, aggregationTableSetId, table) {
if (!this.isSimpleIdentifier(table.tableName)) {
throw new RequiredError("tableName", "Field tableName was invalid.");
}
if (!this.isSimpleIdentifier(table.sourceTableName)) {
throw new RequiredError("sourceTableName", "Field sourceTableName was invalid.");
}
const url = `${this.basePath}/datasources/aggregations/${encodeURIComponent(aggregationTableSetId)}/tables`;
const requestOptions = this.createRequest("POST", accessToken, JSON.stringify(table));
return (await this.fetchJSON(url, requestOptions)).aggregationTable;
}
async deleteAggregationTable(accessToken, aggregationTableSetId, aggregationTableId) {
const url = `${this.basePath}/datasources/aggregations/${encodeURIComponent(aggregationTableSetId)}/tables/${encodeURIComponent(aggregationTableId)}`;
const requestOptions = this.createRequest("DELETE", accessToken);
return this.fetchJSON(url, requestOptions);
}
}
//# sourceMappingURL=AggregationsClient.js.map