diff --git a/src/lib/boavizta/README.md b/src/lib/boavizta/README.md index 238d8a7..9e1e76f 100644 --- a/src/lib/boavizta/README.md +++ b/src/lib/boavizta/README.md @@ -13,7 +13,6 @@ Boavizta exposes a [REST API](https://doc.api.boavizta.org/). If the `boavizta` ### Plugin global config -- `allocation`: manufacturing impacts can be reported with two allocation strategies: `TOTAL` is the total impact without adjusting for usage. `LINEAR` distrbutes the impact linearly over the lifespan of a device. See [Boavizta docs](https://doc.api.boavizta.org/Explanations/manufacture_methodology/#hover-a-specific-duration-allocation-linear) for more info. - `verbose`: determines how much information the API response contains (optional) ### Inputs @@ -23,6 +22,7 @@ Boavizta exposes a [REST API](https://doc.api.boavizta.org/). If the `boavizta` - `cpu/expected-lifespan`: the lifespan of the component, in seconds - `country`: the country used to lookup grid carbon intensity, e.g. "USA" (optional - falls back to Boavizta default) - `cpu/utilization`: percentage CPU utilization for a given observation +- `instance-type`: the name of the specific instance ## Returns diff --git a/src/lib/boavizta/boavizta-api.ts b/src/lib/boavizta/boavizta-api.ts index ce1d2c8..ed6c87a 100644 --- a/src/lib/boavizta/boavizta-api.ts +++ b/src/lib/boavizta/boavizta-api.ts @@ -2,11 +2,18 @@ import axios from 'axios'; import {KeyValuePair} from '../../types/common'; +import {ERRORS} from '../../util/errors'; +import {buildErrorMessage} from '../../util/helpers'; + import {ICountryCodes} from './types'; +const {APIRequestError} = ERRORS; + export const BoaviztaAPI = () => { const BASE_URL = 'https://api.boavizta.org/v1'; + const errorBuilder = buildErrorMessage(BoaviztaAPI.name); + /** * Fetches CPU output data from Boavizta API for a specific component type. */ @@ -27,16 +34,34 @@ export const BoaviztaAPI = () => { * Fetches cloud instance data from Boavizta API. */ const fetchCloudInstanceData = async ( - dataCast: KeyValuePair, + data: KeyValuePair, verbose: boolean ): Promise => { - const updatedDataCast = replaceHyphensWithUnderscores(dataCast); + const updatedDataCast = replaceHyphensWithUnderscores(data); - const response = await axios.post( - `${BASE_URL}/cloud/instance?verbose=${verbose}&duration=${updatedDataCast['usage']['hours_use_time']}`, - dataCast - ); - return response.data; + const response = await axios + .post( + `${BASE_URL}/cloud/instance?verbose=${verbose}&duration=${updatedDataCast['usage']['hours_use_time']}`, + { + provider: data.provider, + instance_type: data['instance-type'], + usage: data.usage, + } + ) + .catch(error => { + throw new APIRequestError( + errorBuilder({ + message: `Error fetching data from Boavizta API. ${JSON.stringify( + (error.response && + error.response.data && + error.response.data.detail) || + error + )}`, + }) + ); + }); + + return response?.data; }; /** diff --git a/src/lib/boavizta/index.ts b/src/lib/boavizta/index.ts index 44f5404..c835e57 100644 --- a/src/lib/boavizta/index.ts +++ b/src/lib/boavizta/index.ts @@ -47,7 +47,7 @@ export const BoaviztaCpuOutput = ( if (!(metricType in input)) { throw new InputValidationError( errorBuilder({ - message: 'Invalid input parameter', + message: `${metricType} is not provided in input`, }) ); } @@ -74,7 +74,7 @@ export const BoaviztaCpuOutput = ( usage: BoaviztaUsageType | undefined ): Promise => { const data = Object.assign({}, input, {usage}); - const verbose = globalConfig.verbose || false; + const verbose = (globalConfig && globalConfig.verbose) || false; const response = await boaviztaAPI.fetchCpuOutputData( data, componentType, @@ -138,7 +138,7 @@ export const BoaviztaCloudOutput = ( if (!(metricType in input)) { throw new InputValidationError( errorBuilder({ - message: 'Invalid input parameter', + message: `${metricType} is not provided in input`, }) ); } @@ -162,7 +162,7 @@ export const BoaviztaCloudOutput = ( usage: BoaviztaUsageType ): Promise => { const data = Object.assign({}, input, {usage}); - const verbose = globalConfig.verbose || false; + const verbose = (globalConfig && globalConfig.verbose) || false; const response = await boaviztaAPI.fetchCloudInstanceData(data, verbose); return baseOutput.formatResponse(response);