-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Account client and support to retrieve quotas usage (#404)
- Loading branch information
Showing
11 changed files
with
179 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require('dotenv').config(); | ||
|
||
const { AccountClient } = require('@openzeppelin/defender-account-client'); | ||
|
||
async function main() { | ||
const creds = { apiKey: process.env.ADMIN_API_KEY, apiSecret: process.env.ADMIN_API_SECRET }; | ||
|
||
const client = new AccountClient(creds); | ||
|
||
// List Account Usage | ||
const usage = await client.getUsage(); | ||
|
||
console.log(usage); | ||
} | ||
|
||
if (require.main === module) { | ||
main().catch(console.error); | ||
} |
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,15 @@ | ||
{ | ||
"name": "get-usage", | ||
"version": "1.49.0", | ||
"private": true, | ||
"main": "index.js", | ||
"author": "Zeljko Markovic <[email protected]>", | ||
"license": "MIT", | ||
"scripts": { | ||
"start": "node index.js" | ||
}, | ||
"dependencies": { | ||
"@openzeppelin/defender-base-client": "1.49.0", | ||
"dotenv": "^8.2.0" | ||
} | ||
} |
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,52 @@ | ||
# Defender Account Client | ||
|
||
Defender Account acts as an interface to manage your account. | ||
|
||
## Install | ||
|
||
```bash | ||
npm install @openzeppelin/defender-account-client | ||
``` | ||
|
||
```bash | ||
yarn add @openzeppelin/defender-account-client | ||
``` | ||
|
||
## Usage | ||
|
||
Start by creating a new _Team API Key_ in Defender, and granting it the capability to create new proposals. Use the newly created API key to initialize an instance of the Account client. | ||
|
||
```js | ||
const { AccountClient } = require('@openzeppelin/defender-account-client'); | ||
const client = new AccountClient({ apiKey: API_KEY, apiSecret: API_SECRET }); | ||
``` | ||
|
||
### Account Usage | ||
|
||
To get account usages `getUsage` method can be used: | ||
|
||
```js | ||
await client.getUsage(); | ||
``` | ||
|
||
You can optionally set date to get usage for past period. When date is set only subset of quotas connected to the monthly usage is returned. | ||
|
||
```js | ||
await client.getUsage({ | ||
date: '2023-10-01' | ||
}); | ||
``` | ||
|
||
You can also optionally set quotas list to get usage only for desired quotas. | ||
|
||
```js | ||
await client.getUsage({ | ||
quotas: ['relayers', 'relayerTxPerHour'] | ||
}); | ||
``` | ||
|
||
## FAQ | ||
|
||
**Can I use this package in a browser?** | ||
|
||
This package is not designed to be used in a browser environment. Using this package requires sensitive API KEYS that should not be exposed publicly. |
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 @@ | ||
module.exports = require('../../jest.config'); |
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,31 @@ | ||
{ | ||
"name": "@openzeppelin/defender-account-client", | ||
"version": "1.50.0-rc.0", | ||
"description": "", | ||
"main": "./lib/index.js", | ||
"types": "./lib/index.d.ts", | ||
"scripts": { | ||
"build": "rm -rf lib && tsc", | ||
"test": "yarn test:unit", | ||
"test:unit": "jest --verbose --passWithNoTests", | ||
"watch": "tsc -w", | ||
"prepare": "yarn build" | ||
}, | ||
"files": [ | ||
"lib", | ||
"!*.test.js", | ||
"!*.test.js.map", | ||
"!*.test.d.ts", | ||
"!*__mocks__" | ||
], | ||
"author": "Zeljko Markovic <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@openzeppelin/defender-base-client": "1.50.0-rc.0", | ||
"axios": "^1.4.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"gitHead": "a7c4808dd11e708df42d110de230c264061c72c3" | ||
} |
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,29 @@ | ||
import { BaseApiClient, ApiVersion } from '@openzeppelin/defender-base-client'; | ||
|
||
import { AccountUsageResponse } from './models/account'; | ||
|
||
export class AccountClient extends BaseApiClient { | ||
protected getPoolId(): string { | ||
return process.env.DEFENDER_ADMIN_POOL_ID ?? 'us-west-2_94f3puJWv'; | ||
} | ||
|
||
protected getPoolClientId(): string { | ||
return process.env.DEFENDER_ADMIN_POOL_CLIENT_ID ?? '40e58hbc7pktmnp9i26hh5nsav'; | ||
} | ||
|
||
protected getApiUrl(v: ApiVersion = 'v1'): string { | ||
if (v === 'v2') { | ||
return process.env.DEFENDER_API_V2_URL ?? 'https://defender-api.openzeppelin.com/v2/'; | ||
} | ||
return process.env.DEFENDER_ADMIN_API_URL ?? 'https://defender-api.openzeppelin.com/admin/'; | ||
} | ||
|
||
public async getUsage(params?: { date?: string | Date; quotas: string[] }): Promise<AccountUsageResponse> { | ||
const searchParams = new URLSearchParams({ | ||
...(params?.quotas && { quotas: params.quotas.join(',') }), | ||
...(params?.date && { date: new Date(params.date).toISOString() }), | ||
}); | ||
|
||
return this.apiCall(async (api) => api.get(`/account/usage?${searchParams.toString()}`)); | ||
} | ||
} |
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,5 @@ | ||
export { AccountClient } from './api'; | ||
export { AccountUsageResponse } from './models/account'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
export const VERSION = require('../package.json').version; |
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,16 @@ | ||
type AccountUsage = | ||
| { | ||
name: string; | ||
description: string; | ||
used: number; | ||
limit: number; | ||
overage?: number; | ||
remaining: number; | ||
period: 'hour' | 'month' | 'total'; | ||
} | ||
| { | ||
name: string; | ||
error: string; | ||
}; | ||
|
||
export type AccountUsageResponse = Record<string, AccountUsage>; |
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,10 @@ | ||
{ | ||
"extends": "code-style/tsconfig.json", | ||
"compilerOptions": { | ||
"declaration": true, | ||
"outDir": "./lib", | ||
"skipLibCheck": true, | ||
"sourceMap": false | ||
}, | ||
"include": ["./src"] | ||
} |
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