web-humas-fe/services/third-party-service.ts

122 lines
2.9 KiB
TypeScript

import axios from "axios";
const tbnInstance = axios.create({
baseURL: "https://portal.humas.polri.go.id/v1/api",
headers: {
"content-type": "application/json",
},
});
const inpInstance = axios.create({
baseURL: "https://inp.polri.go.id/api",
// baseURL: "https://inp.indoplusmedia.id/wp-json/wp/v2/posts",
headers: {
"content-type": "application/json",
},
});
async function tbnGetNews(pathUrl: any, headers: any) {
const response = await tbnInstance
.get(pathUrl, { headers })
.catch(function (error: any) {
console.log(error);
return error.response;
});
if (response?.status == 200 || response?.status == 201) {
return {
error: false,
message: "success",
data: response?.data,
};
} else {
return {
error: true,
message: response?.data?.message || response?.data || null,
data: null,
};
}
}
async function inpGetNews(pathUrl: any, headers: any) {
const response = await inpInstance
.get(pathUrl, { headers })
.catch(function (error: any) {
console.log(error, "inp");
return error.response;
});
console.log("Response base svc : ", response);
if (response?.status == 200 || response?.status == 201) {
return {
error: false,
message: "success",
data: response?.data,
};
} else {
return {
error: true,
message: response?.data?.message || response?.data || null,
data: null,
};
}
}
export async function inpLogin(headers: any) {
const response = await inpInstance
.post(
"/login",
{
email: "api@inp.humas.polri.go.id",
password: "@Inp2024",
},
{ headers }
)
.catch(function (error: any) {
console.log(error, "inp");
return error.response;
});
console.log("Response base svc : ", response);
if (response?.status == 200 || response?.status == 201) {
return {
error: false,
message: "success",
data: response?.data,
};
} else {
return {
error: true,
message: response?.data?.message || response?.data || null,
data: null,
};
}
}
export async function topNewsTbn() {
const headers = {
"content-type": "application/json",
};
return await tbnGetNews(
`/public/articles?page=1&limit=50&order_by=terkini&source=tbnews`,
headers
);
}
export async function loginInp() {
const headers = {
"content-type": "application/json",
};
// return await inpGetNews(`/media`, headers);
return await inpGetNews(`/login`, headers);
}
export async function topNewsInp(token: any) {
const headers = {
"content-type": "application/json",
Authorization: `Bearer ${token}`,
};
// return await inpGetNews(`/media`, headers);
return await inpGetNews(`/artikel/data?per_page=10&page=1`, headers);
}
export async function getImageInp(id: string) {
const headers = {
"content-type": "application/json",
};
return await inpGetNews(`/media?parent=${id}`, headers);
}