All files / utils CogniteClient3dExtensions.ts

78.13% Statements 25/32
70.59% Branches 12/17
81.82% Functions 9/11
78.13% Lines 25/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                          2x 2x 2x 2x 2x     2x         7x 7x                         5x 15x   5x   6x                                             2x       4x                               1x 1x             6x 6x       6x 4x 4x 3x             6x 4x   2x    
/*!
 * Copyright 2020 Cognite AS
 */
 
import { CogniteClient, CogniteInternalId, CogniteExternalId } from '@cognite/sdk';
 
export type Model3dOutput = {
  readonly format: CogniteWellknown3dFormat | string;
  readonly version: number;
  readonly blobId: CogniteInternalId;
};
 
export type CogniteUniformId = CogniteInternalId | CogniteExternalId;
export enum CogniteWellknown3dFormat {
  EptPointCloud = 'ept-pointcloud',
  RevealCadModel = 'reveal-json',
  RevealLegacyI3DFModel = 'reveal-i3df',
  ReevalLegacyProtobufModel = 'reveal-pb'
}
 
export class Model3dOutputList {
  public readonly model: CogniteModel3dIdentifier;
  public readonly outputs: Model3dOutput[];
 
  constructor(model: CogniteModel3dIdentifier, outputs: Model3dOutput[]) {
    this.model = model;
    this.outputs = outputs;
  }
 
  /**
   * Finds an output with a given format of the most recent version.
   *
   * @param outputFormat        Format to find output for, either a well known format a custom format.
   * @param supportedVersions   Optional list of supported version. If not provided all versions are considered.
   */
  public findMostRecentOutput(
    outputFormat: CogniteWellknown3dFormat | string,
    supportedVersions?: number[]
  ): Model3dOutput | undefined {
    const candidates = this.outputs.filter(
      x => x.format === outputFormat && (!supportedVersions || supportedVersions.indexOf(x.version) !== -1)
    );
    return candidates.length > 0
      ? candidates.reduce((left, right) => {
          return right.version > left.version ? right : left;
        })
      : undefined;
  }
}
 
type CogniteModel3dIdentifier = { id: CogniteInternalId } | { externalId: CogniteExternalId };
 
interface OutputsRequest {
  models: CogniteModel3dIdentifier[];
  formats?: (string | CogniteWellknown3dFormat)[];
}
 
interface OutputsResponse {
  readonly items: {
    readonly model: CogniteModel3dIdentifier;
    readonly outputs: Model3dOutput[];
  }[];
}
 
/**
 * Provides 3D V2 specific extensions for the standard CogniteClient used by Reveal.
 */
export class CogniteClient3dExtensions {
  private readonly client: CogniteClient;
 
  constructor(client: CogniteClient) {
    this.client = client;
  }
 
  public async retrieveJsonBlob<T>(blobId: number, path?: string): Promise<T> {
    const url = this.buildBlobBaseUrl(blobId) + (path ? `/${path}` : '');
    const response = await this.client.get<T>(url);
    return response.data;
  }
 
  public async retrieveBinaryBlob(blobId: number, path?: string): Promise<ArrayBuffer> {
    const url = this.buildBlobBaseUrl(blobId) + (path ? `/${path}` : '');
    const response = await this.client.get<ArrayBuffer>(url);
    return response.data;
  }
 
  public buildBlobBaseUrl(blobId: number): string {
    const url = `/api/playground/projects/${this.client.project}/3d/v2/blobs/${blobId}`;
    return url;
  }
 
  public async getOutputs(
    modelRevisionId: CogniteUniformId,
    formats?: (CogniteWellknown3dFormat | string)[]
  ): Promise<Model3dOutputList> {
    const url = `/api/playground/projects/${this.client.project}/3d/v2/outputs`;
    const request: OutputsRequest = {
      models: [createModelIdentifier(modelRevisionId)],
      formats
    };
    const response = await this.client.post<OutputsResponse>(url, { data: request });
    Eif (response.status === 200) {
      const item = response.data.items[0];
      return new Model3dOutputList(item.model, item.outputs);
    }
    throw new Error(`Unexpected response ${response.status} (payload: '${response.data})`);
  }
}
 
function createModelIdentifier(id: CogniteUniformId): CogniteModel3dIdentifier {
  if (typeof id === 'number') {
    return { id };
  }
  return { externalId: id };
}