* 'develop' of github.com:Frappet/bma-ehr-probation: add body edit permission # Conflicts: # src/interfaces/call-api.ts # src/interfaces/permission.ts
86 lines
2.1 KiB
TypeScript
86 lines
2.1 KiB
TypeScript
import { Path } from "tsoa"
|
|
import axios from "axios"
|
|
import { addLogSequence } from "./utils"
|
|
|
|
class CallAPI {
|
|
//Get
|
|
public async GetData(request: any, @Path() path: any, log = true) {
|
|
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,
|
|
},
|
|
})
|
|
if (log)
|
|
addLogSequence(request, {
|
|
action: "request",
|
|
status: "success",
|
|
description: "connected",
|
|
request: {
|
|
method: "GET",
|
|
url: url,
|
|
response: JSON.stringify(response.data.result),
|
|
},
|
|
})
|
|
return response.data.result
|
|
} catch (error) {
|
|
if (log)
|
|
addLogSequence(request, {
|
|
action: "request",
|
|
status: "error",
|
|
description: "unconnected",
|
|
request: {
|
|
method: "GET",
|
|
url: url,
|
|
response: JSON.stringify(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,
|
|
},
|
|
})
|
|
addLogSequence(request, {
|
|
action: "request",
|
|
status: "success",
|
|
description: "connected",
|
|
request: {
|
|
method: "POST",
|
|
url: url,
|
|
payload: JSON.stringify(sendData),
|
|
response: JSON.stringify(response.data.result),
|
|
},
|
|
})
|
|
return response.data.result
|
|
} catch (error) {
|
|
addLogSequence(request, {
|
|
action: "request",
|
|
status: "error",
|
|
description: "unconnected",
|
|
request: {
|
|
method: "POST",
|
|
url: url,
|
|
payload: JSON.stringify(sendData),
|
|
response: JSON.stringify(error),
|
|
},
|
|
})
|
|
throw error
|
|
}
|
|
}
|
|
}
|
|
|
|
export default CallAPI
|