-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Update Agora class and related modules Auth & Projects
- Loading branch information
Showing
5 changed files
with
91 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,62 @@ | ||
const axios = require('axios') | ||
const debug = require('ethernaut-common/src/ui/debug') | ||
const EthernautCliError = require('ethernaut-common/src/error/error') | ||
const Auth = require('./Auth') | ||
const Projects = require('./Projects') | ||
const debug = require('ethernaut-common/src/ui/debug') | ||
|
||
const API_BASE_URL = 'https://vote.optimism.io/api/v1' | ||
const AGORA_API_KEY = process.env.AGORA_API_KEY | ||
|
||
class Agora { | ||
constructor() { | ||
this.auth = new Auth(this) | ||
this.projects = new Projects(this) | ||
this.apiKey = AGORA_API_KEY | ||
this.apiBaseUrl = API_BASE_URL | ||
this.bearerToken = null | ||
} | ||
|
||
async getSpec() { | ||
try { | ||
const response = await axios.get(`${API_BASE_URL}/spec`, { | ||
headers: { | ||
Authorization: `Bearer ${AGORA_API_KEY}`, | ||
}, | ||
}) | ||
// Axios instance setup | ||
createAxiosInstance() { | ||
const headers = { | ||
Authorization: this.bearerToken | ||
? `Bearer ${this.bearerToken}` | ||
: `Bearer ${this.apiKey}`, | ||
} | ||
|
||
debug.log(`Spec: ${response.data}`, 'ethernaut-optigov') | ||
return response.data | ||
} catch (error) { | ||
return axios.create({ | ||
baseURL: this.apiBaseUrl, | ||
headers, | ||
}) | ||
} | ||
|
||
// Handle common API errors | ||
handleError(error) { | ||
if (error.response) { | ||
throw new EthernautCliError( | ||
'ethernaut-optigov', | ||
`Http status error: ${error.response.data}`, | ||
) | ||
} else { | ||
throw new EthernautCliError( | ||
'ethernaut-optigov', | ||
`Http status error: ${error.message}`, | ||
) | ||
} | ||
} | ||
|
||
// common method for getting API spec | ||
async getSpec() { | ||
try { | ||
const axiosInstance = this.createAxiosInstance() | ||
const response = await axiosInstance.get('/spec') | ||
debug.log(`Spec: ${response.data}`, 'ethernaut-optigov') | ||
return response.data | ||
} catch (error) { | ||
this.handleError(error) | ||
} | ||
} | ||
|
||
// Set the bearer token after authentication | ||
setBearerToken(token) { | ||
this.bearerToken = token | ||
} | ||
} | ||
|
||
module.exports = Agora |
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
40 changes: 13 additions & 27 deletions
40
packages/ethernaut-optigov/src/internal/agora/Projects.js
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