59 lines
1.6 KiB
TypeScript
59 lines
1.6 KiB
TypeScript
import { Path } from "tsoa";
|
|
import axios from "axios";
|
|
|
|
class CallAPI {
|
|
//Get
|
|
public async GetData(request: any, @Path() path: any) {
|
|
const token = "Bearer " + request.headers.authorization.replace("Bearer ", "");
|
|
const url = process.env.API_URL + path;
|
|
try {
|
|
const response = await axios.get(url, {
|
|
headers: {
|
|
Authorization: `${token}`,
|
|
"Content-Type": "application/json",
|
|
api_key: process.env.API_KEY,
|
|
},
|
|
});
|
|
return response.data.result;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
}
|
|
//Post
|
|
public async PostData(request: any, @Path() path: any, sendData: any) {
|
|
const token = "Bearer " + request.headers.authorization.replace("Bearer ", "");
|
|
const url = process.env.API_URL + path;
|
|
try {
|
|
const response = await axios.post(url, sendData, {
|
|
headers: {
|
|
Authorization: `${token}`,
|
|
"Content-Type": "application/json",
|
|
api_key: process.env.API_KEY,
|
|
},
|
|
});
|
|
return response.data.result;
|
|
} catch (error) {
|
|
console.log(error);
|
|
throw error;
|
|
}
|
|
}
|
|
//Post
|
|
public async PostDataKeycloak(@Path() path: any, sendData: any) {
|
|
// const token = request.headers.authorization;
|
|
const url = process.env.KC_URL + path;
|
|
try {
|
|
const response = await axios.post(url, sendData, {
|
|
headers: {
|
|
// Authorization: `${token}`,
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
api_key: process.env.API_KEY,
|
|
},
|
|
});
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
}
|
|
}
|
|
|
|
export default CallAPI;
|