-
Notifications
You must be signed in to change notification settings - Fork 1
/
api-main.ts
161 lines (149 loc) · 4.3 KB
/
api-main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import HttpClient from "./http-client";
import Authentication from "./auth";
import { constants } from "../config/";
import { AxiosRequestConfig } from "axios";
import { endpoints } from "../config/constants";
import { Cuenta } from "../types/IolApiCuenta";
import { Titulos } from "../types/IolApiTitulos";
import { Operar } from "../types/IolApiOperar";
import {
Mercado,
IolAuthData,
Country,
GenericResponse,
IolClientInterface,
OperationsFilter,
} from "../types/IolClient";
const { endpoints: api } = constants;
export default class IolClient
extends HttpClient
implements IolClientInterface
{
private auth: Authentication;
public constructor(authData: IolAuthData) {
if (!authData) throw new Error("Missing authenticacion data");
super(authData.url);
this.auth = new Authentication(authData);
this.initializeConnection();
}
// ACCOUNT METHODS
public async getAccountStatus() {
const accountStatus = await this.httpInstance.get<Cuenta.EstadoDeCuenta>(
api.v2.estadocuenta
);
return accountStatus;
}
public async getPortfolio(country: Country) {
const portfolio = await this.httpInstance.get<Cuenta.Portafolio>(
`${api.v2.portafolio}/${country}`
);
return portfolio;
}
public async deleteOperation(operationNumber: number) {
const result = await this.httpInstance.delete<GenericResponse>(
`${api.v2.operaciones}/${operationNumber}`
);
return result;
}
public async getOperation(operationNumber: number) {
const operation = await this.httpInstance.get<Cuenta.OperacionDetalle>(
`${api.v2.operaciones}/${operationNumber}`
);
return operation;
}
public async getOperations(filters: OperationsFilter) {
const params = new URLSearchParams();
Object.entries(filters).forEach((item) => {
params.set(`filtro.${item[0]}`, item[1]);
});
const operations = await this.httpInstance.get<Cuenta.Operaciones>(
api.v2.operaciones,
{ params }
);
return operations;
}
public async buy(data: Operar.Comprar) {
const config: AxiosRequestConfig = {
headers: {
"Content-Type": "application/json",
},
};
const response = await this.httpInstance.post<GenericResponse>(
api.v2.operar.comprar,
data,
config
);
return response;
}
public async sell(data: Operar.Vender) {
const config: AxiosRequestConfig = {
headers: {
"Content-Type": "application/json",
},
};
const response = await this.httpInstance.post<GenericResponse>(
api.v2.operar.vender,
data,
config
);
return response;
}
public async fciRescue(data: Operar.RescateFCI) {
const config: AxiosRequestConfig = {
headers: {
"Content-Type": "application/json",
},
};
const response = await this.httpInstance.post<GenericResponse>(
api.v2.operar.rescateFci,
data,
config
);
return response;
}
public async fciSubscription(data: Operar.SuscripcionFCI) {
const config: AxiosRequestConfig = {
headers: {
"Content-Type": "application/json",
},
};
const response = await this.httpInstance.post<GenericResponse>(
api.v2.operar.suscripcionFci,
data,
config
);
return response;
}
public async getAllFCI() {
const response = await this.httpInstance.get<Titulos.FCI[]>(
endpoints.v2.titulos.fci
);
return response;
}
public async getFCI(symbol: string) {
const response = await this.httpInstance.get<Titulos.FCI>(
`${endpoints.v2.titulos.fci}/${symbol}`
);
return response;
}
public async getPrice(market: Mercado, symbol: string) {
const response = await this.httpInstance.get<Titulos.Cotizacion>(
endpoints.v2.titulos.cotizacion(market, symbol)
);
return response;
}
private initializeConnection() {
this.httpInstance.interceptors.request.use(
async (requestConfig) => await this.useAuth(requestConfig)
);
}
//TODO check the latency introduced by the useAuth requests middleware
private async useAuth(
requestConfig: AxiosRequestConfig
): Promise<AxiosRequestConfig> {
const newToken = await this.auth.getToken();
requestConfig.headers["Authorization"] = `bearer ${newToken}`;
console.log(requestConfig);
return requestConfig;
}
}