-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RSDK-9621: Add Discover Service and GetModelsFromModules to Typescript (
#436) Co-authored-by: John <[email protected]>
- Loading branch information
1 parent
3a8f661
commit 6bac906
Showing
6 changed files
with
159 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { DiscoveryClient } from './discovery/client'; | ||
export type { Discovery } from './discovery/discovery'; |
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,51 @@ | ||
// @vitest-environment happy-dom | ||
|
||
import { | ||
createPromiseClient, | ||
createRouterTransport, | ||
} from '@connectrpc/connect'; | ||
import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import { DiscoveryService } from '../../gen/service/discovery/v1/discovery_connect'; | ||
import { DiscoverResourcesResponse } from '../../gen/service/discovery/v1/discovery_pb'; | ||
import { RobotClient } from '../../robot'; | ||
import { DiscoveryClient } from './client'; | ||
import { ComponentConfig } from '../../gen/app/v1/robot_pb'; | ||
vi.mock('../../robot'); | ||
vi.mock('../../gen/service/discovery/v1/discovery_pb_service'); | ||
|
||
const discoveryClientName = 'test-discovery'; | ||
|
||
let discovery: DiscoveryClient; | ||
|
||
const discoveries: ComponentConfig = new ComponentConfig(); | ||
|
||
describe('DiscoveryClient Tests', () => { | ||
beforeEach(() => { | ||
const mockTransport = createRouterTransport(({ service }) => { | ||
service(DiscoveryService, { | ||
discoverResources: () => | ||
new DiscoverResourcesResponse({ discoveries: [discoveries] }), | ||
}); | ||
}); | ||
|
||
RobotClient.prototype.createServiceClient = vi | ||
.fn() | ||
.mockImplementation(() => | ||
createPromiseClient(DiscoveryService, mockTransport) | ||
); | ||
discovery = new DiscoveryClient( | ||
new RobotClient('host'), | ||
discoveryClientName | ||
); | ||
}); | ||
|
||
describe('Discovery Resources Tests', () => { | ||
it('returns resources from a machine', async () => { | ||
const expected = [discoveries]; | ||
|
||
await expect(discovery.discoverResources()).resolves.toStrictEqual( | ||
expected | ||
); | ||
}); | ||
}); | ||
}); |
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,51 @@ | ||
import { Struct, type JsonValue } from '@bufbuild/protobuf'; | ||
import type { CallOptions, PromiseClient } from '@connectrpc/connect'; | ||
import { DiscoveryService } from '../../gen/service/discovery/v1/discovery_connect'; | ||
import { DiscoverResourcesRequest } from '../../gen/service/discovery/v1/discovery_pb'; | ||
import type { RobotClient } from '../../robot'; | ||
import { doCommandFromClient } from '../../utils'; | ||
import type { Options } from '../../types'; | ||
import type { Discovery } from './discovery'; | ||
|
||
/** | ||
* A gRPC-web client for a Vision service. | ||
* | ||
* @group Clients | ||
*/ | ||
export class DiscoveryClient implements Discovery { | ||
private client: PromiseClient<typeof DiscoveryService>; | ||
private readonly name: string; | ||
private readonly options: Options; | ||
public callOptions: CallOptions = { headers: {} as Record<string, string> }; | ||
|
||
constructor(client: RobotClient, name: string, options: Options = {}) { | ||
this.client = client.createServiceClient(DiscoveryService); | ||
this.name = name; | ||
this.options = options; | ||
} | ||
|
||
async discoverResources(extra = {}, callOptions = this.callOptions) { | ||
const request = new DiscoverResourcesRequest({ | ||
name: this.name, | ||
extra: Struct.fromJson(extra), | ||
}); | ||
|
||
this.options.requestLogger?.(request); | ||
|
||
const resp = await this.client.discoverResources(request, callOptions); | ||
return resp.discoveries; | ||
} | ||
|
||
async doCommand( | ||
command: Struct, | ||
callOptions = this.callOptions | ||
): Promise<JsonValue> { | ||
return doCommandFromClient( | ||
this.client.doCommand, | ||
this.name, | ||
command, | ||
this.options, | ||
callOptions | ||
); | ||
} | ||
} |
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,14 @@ | ||
import type { Struct } from '@bufbuild/protobuf'; | ||
import type { Resource } from '../../types'; | ||
import type { ComponentConfig } from '../../gen/app/v1/robot_pb'; | ||
|
||
/** A service that enables various computer vision algorithms */ | ||
export interface Discovery extends Resource { | ||
/** | ||
* Get a list of component configs of all discovered components. | ||
* | ||
* @param discoveryName - The name of the discovery service. | ||
* @returns - The list of ComponentConfigs. | ||
*/ | ||
discoverResources: (extra?: Struct) => Promise<ComponentConfig[]>; | ||
} |