-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from OpenZeppelin/feature/account-usage
Add support for retrieving account usage via sdk
- Loading branch information
Showing
12 changed files
with
158 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,17 @@ | ||
require('dotenv').config(); | ||
|
||
const { Defender } = require('@openzeppelin/defender-sdk'); | ||
|
||
async function main() { | ||
const creds = { apiKey: process.env.API_KEY, apiSecret: process.env.API_SECRET }; | ||
const client = new Defender(creds); | ||
|
||
// List Account Usage | ||
const usage = await client.account.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": "@openzeppelin/defender-sdk-example-get-usage", | ||
"version": "1.3.0", | ||
"private": true, | ||
"main": "index.js", | ||
"author": "Zeljko Markovic <[email protected]>", | ||
"license": "MIT", | ||
"scripts": { | ||
"start": "node index.js" | ||
}, | ||
"dependencies": { | ||
"@openzeppelin/defender-sdk": "1.3.0", | ||
"dotenv": "^16.3.1" | ||
} | ||
} |
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 @@ | ||
# Defender SDK Account Client | ||
|
||
The OpenZeppelin Defender provides a security operations (SecOps) platform for Ethereum with built-in best practices. Development teams implement Defender to ship faster and minimize security risks. | ||
|
||
This library provides methods related to networks. See Examples for usage. |
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,30 @@ | ||
{ | ||
"name": "@openzeppelin/defender-sdk-account-client", | ||
"version": "1.3.0", | ||
"description": "", | ||
"main": "./lib/index.js", | ||
"types": "./lib/index.d.ts", | ||
"scripts": { | ||
"build": "rm -rf lib && tsc", | ||
"test": "npm run test:unit", | ||
"test:unit": "jest --verbose --passWithNoTests --forceExit", | ||
"watch": "tsc -w" | ||
}, | ||
"files": [ | ||
"lib", | ||
"!*.test.js", | ||
"!*.test.js.map", | ||
"!*.test.d.ts", | ||
"!*__mocks__" | ||
], | ||
"author": "OpenZeppelin Defender <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@openzeppelin/defender-sdk-base-client": "^1.3.0", | ||
"axios": "^1.4.0", | ||
"lodash": "^4.17.21" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
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 { BaseApiClient } from '@openzeppelin/defender-sdk-base-client'; | ||
import { AccountUsageResponse } from '../models/account'; | ||
|
||
const PATH = '/account'; | ||
|
||
export class AccountClient extends BaseApiClient { | ||
protected getPoolId(): string { | ||
return process.env.DEFENDER_POOL_ID ?? 'us-west-2_94f3puJWv'; | ||
} | ||
|
||
protected getPoolClientId(): string { | ||
return process.env.DEFENDER_POOL_CLIENT_ID ?? '40e58hbc7pktmnp9i26hh5nsav'; | ||
} | ||
|
||
protected getApiUrl(): string { | ||
return process.env.DEFENDER_API_URL ?? 'https://defender-api.openzeppelin.com/v2/'; | ||
} | ||
|
||
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(`${PATH}/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,11 @@ | ||
{ | ||
"extends": "code-style/tsconfig.json", | ||
"compilerOptions": { | ||
"declaration": true, | ||
"outDir": "./lib", | ||
"skipLibCheck": true, | ||
"sourceMap": false | ||
}, | ||
"include": ["./src"], | ||
"exclude": ["**/*.test.ts", "**/__mocks__/*"] | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.