api-cg-lib
v1.1.7
Published
Library that gets data from an Api.
Downloads
61
Maintainers
Readme
api-cg-lib
https://github.com/CloudGenUser/api-cg-lib.git
1. Introduction
This code has the objective to get information of an API, no matter if the API is public or requires access permission.
This library could be used as part of a component based on Open Integration Hub (OIH) framework or any Nodejs development that requires API calls on it.
2. Library usage
The library can be installed from npm page with the next:
npm install api-cg-lib
, npm i api-cg-lib
or yarn install api-cg-lib
2.1. Api without security
Arguments:
Required:
- method: This specifies if the method of the request is one of the next: GET, POST, PUT, DELETE, or BATCH.
- api: In this attribute must be specified the path or URL to connect with the API.
Optionals:
- addData: It can be specified an additional parameter or content that could be required by the API as part of the request. The API owner must provide the required information to use the API and obtain the expected result.
- auth: If required by the API this parameter allows to define if it requires a token or credentials to be used to connect to the API. If this parameter is submitted, the parameter "authType" is mandatory.
- authType: This parameter specifies the type of authentication like bearer, basic, oath or other according with the authentication method of the API. If this parameter is set the "auth" parameter is required.
Description: This code creates a request to get information from an API according with the parameter in the call. The correspondig configurations are provided by the API owner and should be set in the corresponding parameters of this library. Once the request is received and processed by the API the respose is sent back with the result. The result is a JSON structure in a string format with the content of the result by the API, it could be the expected result or an error message if the API couldn't process the request.
Sample request:
{"api": "https://pokeapi.co/api/v2/pokemon/ditto", "method": "get"}
Response of the sample request (fragment):
{
"content": {
"abilities": [
{
"ability": {
"name": "limber",
"url": "https://pokeapi.co/api/v2/ability/7/"
}
}
]
}
}
2.2 Api with security
- Arguments:
Required:
- method: This specifies if the method of the request is one of the next: GET, POST, PUT, DELETE, or BATCH.
- api: In this attribute must be specified the path or url to connect with the API.
- auth: In this cases when an authentication is required by the API this parameter allows to define the token or credentials to be used to connect to the API. If this parameter is submited, the parameter "authType" is mandatory.
- authType: This parameter specifies the type of authentication like bearer, basic, oauth or other according with the authentication method of the API. This parameter requires to be configured along with the "auth" parameter.
Optionals:
- addData: It can be specified an additional parameter or content that could be required by the API as part of the request. The API owner must provide the required information to use the API and obtain the expected result.
- Description: This code create a request to get information from an API according with the parameter in the call. Once the request is received and processed by the API the respose is sent back with the result. The result will be in a string format with a JSON structure with the content of the result by the API, it could be the expected result or an error message if the API couldn't process the request.
- Sample of a request:
{"method": "POST","api": "https://elastic.test.com/tempo/","auth": "ZWxhc3RpYzpFGHdGlj","authType":"Basic","addData":{"valorprueba":"prueba"}}
Resultant sample (fragment):
{
"content": {
"abilities": [
{
"ability": {
"name": "limber",
"url": "https://pokeapi.co/api/v2/ability/7/"
}
}
]
}
}