Skip to content

Commit

Permalink
Expose network list endpoint (#292)
Browse files Browse the repository at this point in the history
* Add list network endpoint to sentinels

* Add example project

* change network type

* change param to object param
  • Loading branch information
shahnami authored and collins-w committed Jul 28, 2023
1 parent 12d67c5 commit 196ac18
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
14 changes: 14 additions & 0 deletions examples/list-networks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require('dotenv').config();

const { SentinelClient } = require('@openzeppelin/defender-sentinel-client');

async function main() {
const creds = { apiKey: process.env.ADMIN_API_KEY, apiSecret: process.env.ADMIN_API_SECRET };
const client = new SentinelClient(creds);
const networks = await client.listNetworks({ networkType: 'production' });
console.log(networks);
}

if (require.main === module) {
main().catch(console.error);
}
15 changes: 15 additions & 0 deletions examples/list-networks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "list-networks",
"version": "1.47.0",
"private": true,
"main": "index.js",
"author": "Nami Shah <[email protected]>",
"license": "MIT",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"@openzeppelin/defender-sentinel-client": "1.47.0",
"dotenv": "^8.2.0"
}
}
10 changes: 10 additions & 0 deletions packages/sentinel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,16 @@ await client.pause('8181d9e0-88ce-4db0-802a-2b56e2e6a7b1');
await client.unpause('8181d9e0-88ce-4db0-802a-2b56e2e6a7b1');
```

### List Networks

To list tenant enabled networks, you can call the `listNetworks` function on the client, which returns a `Network[]` object:

```js
await client.listNetworks(); // lists all networks
await client.listNetworks({ networkType: 'production' }); // lists only production networks
await client.listNetworks({ networkType: 'test' }); // lists only test networks
```

### Failed Requests

Failed requests might return the following example response object:
Expand Down
7 changes: 7 additions & 0 deletions packages/sentinel/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
NotificationCategory as NotificationCategoryResponse,
UpdateNotificationCategoryRequest,
} from '../models/category';
import { ListNetworkRequestOptions } from '../models/networks';

export class SentinelClient extends BaseApiClient {
protected getPoolId(): string {
Expand All @@ -42,6 +43,12 @@ export class SentinelClient extends BaseApiClient {
return process.env.DEFENDER_SENTINEL_API_URL || 'https://defender-api.openzeppelin.com/sentinel/';
}

public async listNetworks(opts?: ListNetworkRequestOptions): Promise<Network[]> {
return this.apiCall(async (api) => {
return await api.get(opts && opts.networkType ? `/networks?type=${opts.networkType}` : `/networks`);
});
}

public async list(): Promise<ListSentinelResponse> {
return this.apiCall(async (api) => {
return await api.get(`/subscribers`);
Expand Down
5 changes: 5 additions & 0 deletions packages/sentinel/src/models/networks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type NetworkType = 'production' | 'test';

export interface ListNetworkRequestOptions {
networkType?: NetworkType;
}

0 comments on commit 196ac18

Please sign in to comment.