financial-assistant
116 строк · 2.9 Кб
1import axios from "../../../node_modules/axios/index";
2import { Categories, ResponseType, TableData } from "../contracts/interfaces";
3
4class Api {
5private serverUrl: string;
6
7constructor() {
8this.serverUrl = "http://127.0.0.1:8000/";
9}
10
11public async fetchBanks(): Promise<ResponseType[] | []> {
12try {
13const response = await axios.get(`${this.serverUrl}banks/`);
14return response.data as ResponseType[];
15} catch (error) {
16console.error("Error fetching banks:", error);
17return [];
18}
19}
20
21public async fetchProducts(): Promise<ResponseType[] | []> {
22try {
23const response = await axios.get(`${this.serverUrl}financial-products/`);
24return response.data as ResponseType[];
25} catch (error) {
26console.error("Error fetching financial products:", error);
27return [];
28}
29}
30
31public async fetchProductsCategories(
32bankId: number,
33productId: number
34): Promise<Categories[] | []> {
35try {
36const response = await axios.get(
37`${this.serverUrl}products-categories/`,
38{
39params: {
40bank_id: bankId,
41product_id: productId,
42},
43}
44);
45return response.data;
46} catch (error) {
47console.error("Error fetching credit categories:", error);
48return [];
49}
50}
51
52public async fetchCategorySubsections(
53categoryId: number
54): Promise<ResponseType[] | []> {
55try {
56const response = await axios.get(`${this.serverUrl}loan-subsections/`, {
57params: {
58category_id: categoryId,
59},
60});
61return response.data;
62} catch (error) {
63console.error("Error fetching category's subsections:", error);
64return [];
65}
66}
67
68public async fetchSubsectionDetails(
69subsectionId: number
70): Promise<ResponseType[] | []> {
71try {
72const response = await axios.get(`${this.serverUrl}subsection-details/`, {
73params: {
74subsection_id: subsectionId,
75},
76});
77return response.data;
78} catch (error) {
79console.error("Error fetching subsection details:", error);
80return [];
81}
82}
83
84public async fetchDetailedData(
85loanDetailId: number
86): Promise<TableData[] | []> {
87try {
88const response = await axios.get(
89`${this.serverUrl}detailed-description/`,
90{
91params: {
92loanDetail_id: loanDetailId,
93},
94}
95);
96return response.data;
97} catch (error) {
98console.error("Error fetching detailed information:", error);
99return [];
100}
101}
102
103public async assistant(query: string): Promise<ResponseType | null> {
104try {
105const response = await axios.get(
106`${this.serverUrl}assistant/?q=${encodeURIComponent(query)}`
107);
108return response.data;
109} catch (error) {
110console.error("Error getting loan rate:", error);
111return null;
112}
113}
114}
115
116export default Api;
117