-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from Menighin/v3
V3
- Loading branch information
Showing
23 changed files
with
1,274 additions
and
2,860 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const typedefs = require("./typedefs"); | ||
const CeiUtils = require('./CeiUtils'); | ||
const AxiosWrapper = require('./AxiosWrapper'); | ||
|
||
const URLS = { | ||
LIST_DATA: 'https://investidor.b3.com.br/api/extrato/v1/movimentacao/:page', | ||
}; | ||
|
||
class AccountStatementCrawler { | ||
|
||
/** | ||
* Crawls the tab "Movimentacao" at CEI | ||
* @param {Date} startDate - The start date of the range | ||
* @param {Date} endDate - The end date of the range | ||
* @param {Number} page - The page of the data | ||
* @param {typedefs.CeiCrawlerOptions} [options] - Options for the crawler | ||
* @returns {Promise<typedefs.CeiListData<typedefs.AccountStatement>} - The account statement | ||
*/ | ||
static async getAccountStatement(startDate, endDate, page, options = {}) { | ||
const lastExecution = options.lastExecutionInfo.generalDate; | ||
const startDateStr = CeiUtils.getDateForQueryParam(startDate || CeiUtils.subtractMonth(lastExecution)); | ||
const endDateStr = CeiUtils.getDateForQueryParam(endDate || lastExecution); | ||
|
||
if (options.debug) | ||
console.log(`[AccountStatementCrawler] Crawling statement for period ${startDateStr} - ${endDateStr}`); | ||
|
||
const response = await AxiosWrapper.request(URLS.LIST_DATA, { | ||
queryParams: { | ||
dataInicio: startDateStr, | ||
dataFim: endDateStr, | ||
}, | ||
pathParams: { | ||
page: page | ||
} | ||
}); | ||
|
||
return response; | ||
} | ||
|
||
} | ||
|
||
module.exports = AccountStatementCrawler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const axios = require('axios').default; | ||
const https = require('https'); | ||
const { CeiCrawlerError, CeiErrorTypes } = require('./CeiCrawlerError'); | ||
|
||
class AxiosWrapper { | ||
|
||
static setup(options) { | ||
const httpsAgent = new https.Agent({ | ||
rejectUnauthorized: false, | ||
}); | ||
|
||
axios.interceptors.request.use(config => { | ||
config.headers = { | ||
'Authorization': `Bearer ${options.auth.token}` | ||
}; | ||
config.params['cache-guid'] = options.auth['cache-guid']; | ||
config.httpsAgent = httpsAgent; | ||
|
||
if (options.debug) | ||
console.log(`[AxiosWrapper] ${config.method.toUpperCase()} ${config.url} ${JSON.stringify(config.params)}`); | ||
|
||
return config; | ||
}); | ||
} | ||
|
||
static async request(url, opts = {}) { | ||
const pathParams = opts.pathParams || {}; | ||
const queryParams = opts.queryParams || {}; | ||
try { | ||
const urlWithParams = Object.keys(pathParams) | ||
.reduce((p, v) => { | ||
return p.replace(`:${v}`, pathParams[v]); | ||
}, url); | ||
|
||
const response = await axios.get(urlWithParams, { | ||
params: { | ||
...queryParams | ||
} | ||
}); | ||
return response.data; | ||
} catch (e) { | ||
const msgStr = e.response.data != null ? (e.response.data.message || e.response.data.trim()) : e.message; | ||
const msg = msgStr === '' ? e.message : msgStr; | ||
|
||
if (e.response.status === 401) | ||
throw new CeiCrawlerError(CeiErrorTypes.UNAUTHORIZED, msg, e.response.status); | ||
|
||
throw new CeiCrawlerError(CeiErrorTypes.BAD_REQUEST, msg, e.response.status); | ||
} | ||
} | ||
} | ||
|
||
module.exports = AxiosWrapper; |
Oops, something went wrong.