generic axios

This commit is contained in:
Gabriel Rosa
2022-06-22 09:49:23 -03:00
parent 4df23c917a
commit ff2b8fae6f
2 changed files with 50 additions and 45 deletions
+48 -45
View File
@@ -14,7 +14,7 @@ import {
TestConnectionGetColumnsRequest,
TestConnectionRequest,
} from '@victorradael/protospack';
import axios from 'axios';
import axios, { AxiosRequestConfig } from 'axios';
import { lastValueFrom } from 'rxjs';
import { InputModel } from 'src/modules/inputs/dtos/input.model';
import {
@@ -22,6 +22,7 @@ import {
objectSnakeToCamel,
} from 'src/utils/CaseConverter';
import { mustache } from 'src/utils/mustache';
import { URLSearchParams } from 'url';
import { IIdRequest, UpdateInputRequest } from './interfaces';
export class InputsClientService implements OnModuleInit {
@@ -208,57 +209,59 @@ export class InputsClientService implements OnModuleInit {
return {};
}
async getInputTokens(input: InputModel) {
const { credentials, options } = input;
const credentialsTokens: any = {};
const { credentials } = input;
let credentialsTokens = {};
switch (credentials.connection_type) {
case 'oauth':
const { oauth } = options;
const { get_tokens_url_params, get_tokens_set_response } = oauth;
const secrets = await this.getAuthSecrets(input.plugin);
if (oauth.content_type === 'application/x-www-form-urlencoded') {
const requestParams = new URLSearchParams();
const get_tokens_url_params_string = mustache(get_tokens_url_params, {
...secrets,
...credentials,
});
let get_tokens_url_params_obj: Record<string, any> = {};
try {
get_tokens_url_params_obj = JSON.parse(
get_tokens_url_params_string,
);
} catch (error) {
throw new HttpException(
'Erro transformando get_tokens_url_params',
400,
);
}
const { redirect_uri } = get_tokens_url_params_obj;
for (const key in get_tokens_url_params_obj) {
const value = get_tokens_url_params_obj[key];
requestParams.append(key, value);
}
const { data } = await axios
.post(options.oauth.get_tokens_url, requestParams)
.catch((err) => {
console.log(err.response.data);
throw new HttpException(
err.response.data.message,
err.response.status,
);
});
for (const key in get_tokens_set_response) {
const responseKey = get_tokens_set_response[key];
credentialsTokens[key] = data[responseKey];
}
credentialsTokens.redirect_uri = redirect_uri;
}
credentialsTokens = await this.getOauthTokens(input);
break;
}
return { ...credentials, ...credentialsTokens };
}
// getOauthTokens(input){
async getOauthTokens(input: InputModel) {
const tokens: Record<string, any> = {};
const { options, credentials } = input;
const { oauth } = options;
const { get_tokens_url_params, get_tokens_set_response } = oauth;
const secrets = await this.getAuthSecrets(input.plugin);
const get_tokens_url_params_string = mustache(get_tokens_url_params, {
...secrets,
...credentials,
});
let get_tokens_url_params_obj: Record<string, any> = {};
try {
get_tokens_url_params_obj = JSON.parse(get_tokens_url_params_string);
} catch (error) {
throw new HttpException('Erro transformando get_tokens_url_params', 400);
}
let params = {};
switch (oauth.content_type) {
case 'application/x-www-form-urlencoded':
params = new URLSearchParams(get_tokens_url_params_obj);
params = params.toString();
break;
default:
break;
}
const axiosRequestConfig: AxiosRequestConfig = {
url: oauth.get_tokens_url,
method: oauth.get_tokens_method || 'POST',
data: params,
headers: { 'content-type': oauth.content_type || 'application/json' },
};
// }
const { data } = await axios(axiosRequestConfig).catch((err) => {
console.log(err.response.data);
throw new HttpException(err.response.data.message, err.response.status);
});
for (const key in get_tokens_set_response) {
const responseKey = get_tokens_set_response[key];
tokens[key] = data[responseKey];
}
const { redirect_uri } = get_tokens_url_params_obj;
tokens.redirect_uri = redirect_uri;
return tokens;
}
async getAvailableEntities(data) {
console.log('InputClientService', 'GetAvailableEntities');
+2
View File
@@ -1,4 +1,5 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Method } from 'axios';
export class InputModel {
category: string;
@@ -14,6 +15,7 @@ export class InputModel {
options?: {
oauth?: {
get_tokens_url: string;
get_tokens_method?: Method;
get_tokens_url_params: string;
get_tokens_set_response: Record<string, string>;
content_type: string;