degiro-api
Advanced tools
Comparing version 0.4.0 to 0.4.1
{ | ||
"name": "degiro-api", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"public": true, | ||
@@ -5,0 +5,0 @@ "description": "Unofficial DeGiro API for Javascript. Buy and sell in the stock market. See your portfolio and much more", |
@@ -40,3 +40,3 @@ # DeGiro Trading Broker API | ||
degiro.login() | ||
.then(() => console.log('Log in success')) | ||
.then((accountData) => console.log('Log in success\n', accountData)) | ||
.catch(console.error) | ||
@@ -47,3 +47,3 @@ | ||
const degiro = DeGiro.create({ username: '*****', pwd: '*****' }) | ||
await degiro.login() | ||
const accountData = await degiro.login() | ||
@@ -53,3 +53,3 @@ // or create with env credentials | ||
const degiro = new DeGiro() // <-- Use DEGIRO_USER & DEGIRO_PWD | ||
await degiro.login() | ||
const accountData = await degiro.login() | ||
``` | ||
@@ -100,17 +100,59 @@ | ||
DeGiro login process | ||
✓ should successfully log in with environment credentials (641ms) | ||
✓ should return a valid account config from server (635ms) | ||
✓ should return a valid account data from server (700ms) | ||
✓ should successfully log in with environment credentials (619ms) | ||
✓ should return a valid account config from server (738ms) | ||
✓ should return a valid account data from server (727ms) | ||
✓ getJSESSIONID should return a valid jsessionId | ||
✓ should login with previous jsessionId | ||
DeGiro logout process | ||
✓ should successfully log out after sign in (724ms) | ||
✓ should successfully log out after sign in (685ms) | ||
8 passing (3s) | ||
10 passing (3s) | ||
✨ Done in 4.69s. | ||
✨ Done in 5.21s. | ||
``` | ||
### Get account details | ||
### Get JSESSIONID and reuse sessions | ||
The JSessionId is the session browser cookie that DeGiro use to authenticate requests. You could prevent masive login/logout requests reusing a valid jsessionid from previous DeGiro instance. The way to do that is: | ||
`getJSESSIONID(): string` | ||
```js | ||
import DeGiro from 'degiro-api' | ||
(async () => { | ||
// Create an instance from a previous session | ||
const degiro = new DeGiro({ | ||
username: '<your_username_here>', | ||
pwd: '*******', | ||
jsessionId: previousJSESSIONID | ||
}) | ||
// Hydrate | ||
// Re-use sessions need to re-hydrate the account config data and could use as a session expiration checker | ||
await degiro.login() // Login also returns accountData | ||
// Do your stuff here... | ||
})() | ||
``` | ||
```js | ||
import DeGiro from 'degiro-api' | ||
(async () => { | ||
const degiro = new DeGiro({}) // <-- Using ENV variables | ||
await degiro.login() // Login also returns accountData | ||
// Get the jsessionId (LOOK, is not a promise) | ||
const jsessionId = degiro.getJSESSIONID() | ||
})() | ||
``` | ||
### Get account details explicitly | ||
Get account info using `await`: | ||
@@ -127,3 +169,3 @@ | ||
await degiro.login() | ||
await degiro.login() // Login also returns accountData | ||
@@ -130,0 +172,0 @@ const accountData = await degiro.getAccountData() |
@@ -43,3 +43,3 @@ // Import modules | ||
private readonly pwd: string | ||
private jsessionId: string | ||
private jsessionId: string | undefined | ||
private accountConfig: AccountConfigType | undefined | ||
@@ -49,7 +49,7 @@ private accountData: AccountDataType | undefined | ||
constructor(params: DeGiroSettupType = {}) { | ||
let { username, pwd } = params | ||
const { jsessionId = '' } = params | ||
let { username, pwd, jsessionId } = params | ||
username = username || process.env['DEGIRO_USER'] | ||
pwd = pwd || process.env['DEGIRO_PWD'] | ||
jsessionId = jsessionId || process.env['DEGIRO_JSESSIONID'] | ||
@@ -70,2 +70,3 @@ if (!username) throw new Error('DeGiro api needs an username to access') | ||
login(): Promise<AccountDataType> { | ||
if (this.jsessionId) return this.loginWithJSESSIONID(this.jsessionId) | ||
return new Promise((resolve, reject) => { | ||
@@ -83,2 +84,14 @@ loginRequest({ username: this.username, pwd: this.pwd }) | ||
private loginWithJSESSIONID(jsessionId: string): Promise<AccountDataType> { | ||
return new Promise((resolve, reject) => { | ||
this.getAccountConfig(jsessionId) | ||
.then(() => this.getAccountData()) | ||
.then((accountData) => { | ||
this.jsessionId = undefined // Remove the jsessionId to prevent reuse | ||
resolve(accountData) | ||
}) | ||
.catch(reject) | ||
}) | ||
} | ||
logout(): Promise<void> { | ||
@@ -101,2 +114,4 @@ return new Promise((resolve, reject) => { | ||
getJSESSIONID = () => this.hasSessionId() ? (<AccountConfigType>this.accountConfig).data.sessionId : undefined | ||
getAccountConfig(sessionId?: string): Promise<AccountConfigType> { | ||
@@ -103,0 +118,0 @@ return new Promise((resolve, reject) => { |
@@ -23,2 +23,4 @@ import { | ||
getJSESSIONID(): string | undefined | ||
getAccountConfig(sessionId: string): Promise<AccountConfigType> | ||
@@ -25,0 +27,0 @@ |
@@ -196,2 +196,61 @@ // Importamos código ha probar | ||
it('getJSESSIONID should return a valid jsessionId', async () => { | ||
// Creamos la instancia del objecto y comprobamos que se ha creado bien | ||
const degiro = new DeGiro() | ||
expect(degiro).to.exist | ||
expect(degiro).to.be.a('object') | ||
expect(degiro).to.be.instanceOf(DeGiro) | ||
// Hacemos login y esperamos con que no falle | ||
const loginPromise = degiro.login() | ||
expect(loginPromise).not.be.rejected | ||
loginPromise.then(() => { | ||
const jsessionId = degiro.getJSESSIONID() | ||
expect(jsessionId).to.exist | ||
expect(jsessionId).not.to.be.null | ||
expect(jsessionId).to.be.a('string') | ||
expect(jsessionId).to.have.length | ||
degiro.logout() // <-- Cerramos sesión pero nos "da igual que falle o no" | ||
}) | ||
}) | ||
it('should login with previous jsessionId', async () => { | ||
// Creamos la instancia del objecto y comprobamos que se ha creado bien | ||
const degiroAux = new DeGiro() | ||
expect(degiroAux).to.exist | ||
expect(degiroAux).to.be.a('object') | ||
expect(degiroAux).to.be.instanceOf(DeGiro) | ||
// Hacemos login y esperamos con que no falle para obtener un jsessionId | ||
const loginPromiseAux = degiroAux.login() | ||
expect(loginPromiseAux).not.be.rejected | ||
loginPromiseAux.then(() => { | ||
// Obtenemos el jsessionID | ||
const jsessionId = degiroAux.getJSESSIONID() | ||
expect(jsessionId).exist | ||
expect(jsessionId).not.to.be.null | ||
expect(jsessionId).to.be.a('string') | ||
expect(jsessionId).to.have.length | ||
// Creamos el degiro a testear y le pasamos el jsession valido del anterior objeto | ||
const degiro = new DeGiro({ jsessionId }) | ||
const loginPromise = degiro.login() | ||
expect(loginPromise).not.be.rejected | ||
loginPromise | ||
.then((accountData) => { | ||
expect(accountData).exist | ||
}) | ||
.catch((error) => { | ||
console.error(error) | ||
expect(error).not.exist | ||
}) | ||
.finally(() => { | ||
// Cuando hemos acabado todo cerramos sesion | ||
degiroAux.logout() // <-- Cerramos sesión pero nos "da igual que falle o no" | ||
}) | ||
}) | ||
}) | ||
}) |
904756
4944
498