-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PLAT-4879] Add controller for fetching PMI annotations (#631)
* Use latest protos * Add controller for accessing PMI annotations
- Loading branch information
1 parent
0aeba56
commit ae3af3a
Showing
12 changed files
with
206 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
jest.mock( | ||
'@vertexvis/scene-view-protos/sceneview/protos/scene_view_api_pb_service' | ||
); | ||
|
||
import { SceneViewAPIClient } from '@vertexvis/scene-view-protos/sceneview/protos/scene_view_api_pb_service'; | ||
|
||
import { mockGrpcUnaryResult, random } from '../../../testing'; | ||
import { makeListPmiAnnotationsResponse } from '../../../testing/pmi'; | ||
import { PmiController } from '../controller'; | ||
import { mapListPmiAnnotationsResponseOrThrow } from '../mapper'; | ||
|
||
describe(PmiController, () => { | ||
const jwt = random.string(); | ||
const deviceId = random.string(); | ||
|
||
describe(PmiController.prototype.listAnnotations, () => { | ||
it('fetches page of annotations', async () => { | ||
const { controller, client } = makePmiController(jwt, deviceId); | ||
const expected = makeListPmiAnnotationsResponse(); | ||
|
||
(client.listPmiAnnotations as jest.Mock).mockImplementationOnce( | ||
mockGrpcUnaryResult(expected) | ||
); | ||
|
||
const res = await controller.listAnnotations(); | ||
expect(res).toEqual( | ||
mapListPmiAnnotationsResponseOrThrow(expected.toObject()) | ||
); | ||
}); | ||
}); | ||
|
||
function makePmiController( | ||
jwt: string, | ||
deviceId: string | ||
): { | ||
controller: PmiController; | ||
client: SceneViewAPIClient; | ||
} { | ||
const client = new SceneViewAPIClient('https://example.com'); | ||
return { | ||
client, | ||
controller: new PmiController( | ||
client, | ||
() => jwt, | ||
() => deviceId | ||
), | ||
}; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Pager } from '@vertexvis/scene-view-protos/core/protos/paging_pb'; | ||
import { Uuid2l } from '@vertexvis/scene-view-protos/core/protos/uuid_pb'; | ||
import { | ||
ListPmiAnnotationsRequest, | ||
ListPmiAnnotationsResponse, | ||
} from '@vertexvis/scene-view-protos/sceneview/protos/scene_view_api_pb'; | ||
import { SceneViewAPIClient } from '@vertexvis/scene-view-protos/sceneview/protos/scene_view_api_pb_service'; | ||
import { UUID } from '@vertexvis/utils'; | ||
|
||
import { createMetadata, JwtProvider, requestUnary } from '../grpc'; | ||
import { mapListPmiAnnotationsResponseOrThrow } from './mapper'; | ||
import { PmiAnnotationListResponse } from './types'; | ||
|
||
export interface ListAnnotationsOptions { | ||
modelViewId?: UUID.UUID; | ||
cursor?: string; | ||
size?: number; | ||
} | ||
|
||
export class PmiController { | ||
public constructor( | ||
private client: SceneViewAPIClient, | ||
private jwtProvider: JwtProvider, | ||
private deviceIdProvider: () => string | undefined | ||
) {} | ||
|
||
public async listAnnotations({ | ||
modelViewId, | ||
cursor, | ||
size = 50, | ||
}: ListAnnotationsOptions = {}): Promise<PmiAnnotationListResponse> { | ||
const res: ListPmiAnnotationsResponse = await requestUnary( | ||
async (handler) => { | ||
const deviceId = this.deviceIdProvider(); | ||
const meta = await createMetadata(this.jwtProvider, deviceId); | ||
const req = new ListPmiAnnotationsRequest(); | ||
|
||
if (modelViewId != null) { | ||
const { msb, lsb } = UUID.toMsbLsb(modelViewId); | ||
const id = new Uuid2l(); | ||
id.setMsb(msb); | ||
id.setLsb(lsb); | ||
req.setModelViewId(id); | ||
} | ||
|
||
const page = new Pager(); | ||
page.setLimit(size); | ||
if (cursor != null) { | ||
page.setCursor(cursor); | ||
} | ||
req.setPage(page); | ||
|
||
this.client.listPmiAnnotations(req, meta, handler); | ||
} | ||
); | ||
|
||
return mapListPmiAnnotationsResponseOrThrow(res.toObject()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './controller'; | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { PmiAnnotation as PBPmiAnnotation } from '@vertexvis/scene-view-protos/core/protos/model_views_pb'; | ||
import { ListPmiAnnotationsResponse } from '@vertexvis/scene-view-protos/sceneview/protos/scene_view_api_pb'; | ||
import { Mapper as M } from '@vertexvis/utils'; | ||
|
||
import { fromPbUuid2l, mapCursor } from '../mappers'; | ||
import { PmiAnnotation, PmiAnnotationListResponse } from './types'; | ||
|
||
const mapModelView: M.Func<PBPmiAnnotation.AsObject, PmiAnnotation> = | ||
M.defineMapper( | ||
M.read(M.mapRequiredProp('id', fromPbUuid2l), M.getProp('displayName')), | ||
([id, displayName]) => ({ id, displayName }) | ||
); | ||
|
||
const mapListPmiAnnotationsResponse: M.Func< | ||
ListPmiAnnotationsResponse.AsObject, | ||
PmiAnnotationListResponse | ||
> = M.defineMapper( | ||
M.read( | ||
M.mapProp('annotationsList', M.mapArray(mapModelView)), | ||
M.mapProp('nextPageCursor', mapCursor) | ||
), | ||
([annotations, next]) => ({ annotations, paging: { next } }) | ||
); | ||
|
||
export const mapListPmiAnnotationsResponseOrThrow = M.ifInvalidThrow( | ||
mapListPmiAnnotationsResponse | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { UUID } from '@vertexvis/utils'; | ||
|
||
import { PagingLinks } from '../types/pagination'; | ||
|
||
export interface PmiAnnotation { | ||
id: UUID.UUID; | ||
displayName: string; | ||
} | ||
|
||
export interface PmiAnnotationListResponse { | ||
annotations: PmiAnnotation[]; | ||
paging: PagingLinks; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { PmiAnnotation } from '@vertexvis/scene-view-protos/core/protos/model_views_pb'; | ||
import { ListPmiAnnotationsResponse } from '@vertexvis/scene-view-protos/sceneview/protos/scene_view_api_pb'; | ||
import { UUID } from '@vertexvis/utils'; | ||
|
||
import { random } from './random'; | ||
import { makeUuid2l } from './sceneView'; | ||
|
||
export function makeListPmiAnnotationsResponse( | ||
annotations: PmiAnnotation[] = [makeAnnotation(), makeAnnotation()] | ||
): ListPmiAnnotationsResponse { | ||
const res = new ListPmiAnnotationsResponse(); | ||
res.setAnnotationsList(annotations); | ||
return res; | ||
} | ||
|
||
export function makeAnnotation( | ||
id: UUID.UUID = UUID.create(), | ||
displayName: string = random.string() | ||
): PmiAnnotation { | ||
const annotation = new PmiAnnotation(); | ||
annotation.setId(makeUuid2l(id)); | ||
annotation.setDisplayName(displayName); | ||
return annotation; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters