From 4e3faf333b1a8769ad2ba9c98e6d7fee02fab85b Mon Sep 17 00:00:00 2001 From: reinerBa Date: Tue, 15 Aug 2017 00:34:15 +0200 Subject: [PATCH 1/7] Add promise support to library and a new example --- examples/zeige_kontoumsaetze_async.js | 89 +++++++++++++++++++++++++++ lib/FinTSClient.js | 79 +++++++++++++++++++++--- 2 files changed, 159 insertions(+), 9 deletions(-) create mode 100644 examples/zeige_kontoumsaetze_async.js diff --git a/examples/zeige_kontoumsaetze_async.js b/examples/zeige_kontoumsaetze_async.js new file mode 100644 index 0000000..bcd90ae --- /dev/null +++ b/examples/zeige_kontoumsaetze_async.js @@ -0,0 +1,89 @@ +/* + * Copyright 2015-2016 Jens Schyma jeschyma@gmail.com + * + * This File is a Part of the source of Open-Fin-TS-JS-Client. + * + * This file is licensed to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * or in the LICENSE File contained in this project. + * + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * See the NOTICE file distributed with this work for additional information + * regarding copyright ownership. + */ +/* + Dieses Beispiel veranschaulicht wie der Client mit Promises und async/await zu verwenden ist um Kontoumsätze anzuzeigen. +*/ +var FinTSClient = require('../') // require("open-fin-ts-js-client"); +var log = null +try { + var logme = false + process.argv.forEach(function (val, index, array) { + if (val == 'log') logme = true + }) + if (logme) { + var bunyan = require('bunyan') + log = bunyan.createLogger({ + name: 'demo_fints_logger', + stream: process.stdout, + level: 'trace' + }) + } +} catch (ee) { + +}; +// 1. Definition der Bankenliste - Echte URLs sind hier http://www.hbci-zka.de/institute/institut_auswahl.htm erhältlich +var bankenliste = { + '12345678': { + 'blz': 12345678, + 'url': 'http://localhost:3000/cgi-bin/hbciservlet' + }, + 'undefined': { + 'url': '' + } +} +// 2. FinTSClient anlegen +// BLZ: 12345678 +// Kunden-ID/Benutzerkennung: test1 +// PIN: 1234 +// Bankenliste siehe oben +var client = new FinTSClient(12345678, 'test1', '1234', bankenliste, log) + +// start +GetKontoUmsaetze() + +async GetKontoUmsaetze(){ + try{ + // 3. Verbindung aufbauen + await client.EstablishConnection() + console.log('Erfolgreich Verbunden') + + // 4. Kontoumsätze für alle Konten nacheinander laden + let daten=[] + for (let konto of client.konten) { + let data = await client.MsgGetKontoUmsaetze(konto.sepa_data, null, null) + daten.push(data) + } + // Alles gut + // 5. Umsätze darstellen + console.log(JSON.stringify(data)) + + // 6. Verbindung beenden + await client.MsgEndDialog() + }catch(exception){ + console.error("Fehler: " + exception) + } + // 7. Secure Daten im Objekt aus dem Ram löschen + client.closeSecure() + console.log('ENDE') +} diff --git a/lib/FinTSClient.js b/lib/FinTSClient.js index 0599d14..efeaab2 100644 --- a/lib/FinTSClient.js +++ b/lib/FinTSClient.js @@ -711,10 +711,24 @@ var FinTSClient = function (in_blz, in_kunden_id, in_pin, in_bankenlist, logger) } } } - me.MsgEndDialog(cb) + MsgEndDialog(cb) } me.MsgEndDialog = function (cb) { + if(cb || typeof Promise !== 'function') + MsgEndDialog(cb) + else + return new Promise(function(resolve, reject) { + MsgEndDialog(function (error, recvMsg) { + if(error) + reject(error) + else + resolve(recvMsg) + }) + }) + } + + MsgEndDialog = function (cb) { var msg = new Nachricht(me.proto_version) if (me.kunden_id != 9999999999) { msg.sign({ @@ -848,13 +862,30 @@ var FinTSClient = function (in_blz, in_kunden_id, in_pin, in_bankenlist, logger) }) } - /* - konto = {iban,bic,konto_nr,unter_konto,ctry_code,blz} - from_date - to_date können null sein - cb + /** + * Lädt die Kontenumsätze für ein bestimmtes Konto + * @constructor + * @param {object} konto - {iban,bic,konto_nr,unter_konto,ctry_code,blz} + * @param {Date} from_date - The date to start from_date + * @param {Date} to_date - The date to stop at + * @param {function} callback - optional if promise is possible + * @return {promise} The optional Promise when no cb is given or Promise is undefined */ me.MsgGetKontoUmsaetze = function (konto, from_date, to_date, cb) { + if(cb || typeof Promise !== 'function') + MsgGetKontoUmsaetze(konto, from_date, to_date, cb) + else + return new Promise(function(resolve, reject) { + MsgGetKontoUmsaetze(konto, from_date, to_date, function (error, rMsg, data) { + if(error) + reject(error) + else + resolve({'rMsg': rMsg, 'data': data}) + }) + }) + } + + function MsgGetKontoUmsaetze(konto, from_date, to_date, cb) { var processed = false var v7 = null var v5 = null @@ -970,11 +1001,27 @@ var FinTSClient = function (in_blz, in_kunden_id, in_pin, in_bankenlist, logger) return result } - /* - konto = {iban,bic,konto_nr,unter_konto,ctry_code,blz} - cb + /** + * Lädt den Saldo eines bestimmten Kontos + * @param {object} konto - {iban,bic,konto_nr,unter_konto,ctry_code,blz} + * @param {function} callback - optional if promise is possible + * @return {promise} The optional Promise when no cb is given or Promise is undefined */ me.MsgGetSaldo = function (konto, cb) { + if(cb || typeof Promise !== 'function') + MsgGetSaldo(konto, cb) + else + return new Promise(function (resolve, reject) { + MsgGetSaldo(konto, function (error, recvMsg, saldo) { + if(error) + reject(error) + else + resolve({'recvMsg': recvMsg, 'saldo': saldo}) + }) + }) + } + + MsgGetSaldo = function (konto, cb) { var req_saldo = new Order(me) var processed = false var v5 = null @@ -1077,6 +1124,20 @@ var FinTSClient = function (in_blz, in_kunden_id, in_pin, in_bankenlist, logger) } me.EstablishConnection = function (cb) { + if(cb || typeof Promise !== 'function') + EstablishConnection(cb) + else + return new Promise(function(resolve, reject) { + EstablishConnection(function (error) { + if(error) + reject(error) + else + resolve() + }) + }) + } + + EstablishConnection = function (cb) { var protocol_switch = false var vers_step = 1 var original_bpd = me.bpd.clone() From b8156ff44f7a40b94a689d7afe8c533e6745d2f6 Mon Sep 17 00:00:00 2001 From: reinerBa Date: Tue, 15 Aug 2017 01:02:38 +0200 Subject: [PATCH 2/7] Library works with async in the new example --- examples/zeige_kontoumsaetze_async.js | 18 ++++++++++++------ lib/FinTSClient.js | 12 ++++++------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/examples/zeige_kontoumsaetze_async.js b/examples/zeige_kontoumsaetze_async.js index bcd90ae..b61cf34 100644 --- a/examples/zeige_kontoumsaetze_async.js +++ b/examples/zeige_kontoumsaetze_async.js @@ -1,6 +1,6 @@ /* * Copyright 2015-2016 Jens Schyma jeschyma@gmail.com - * + * and in case of this file Reiner Bamberger * This File is a Part of the source of Open-Fin-TS-JS-Client. * * This file is licensed to you under the Apache License, Version 2.0 (the @@ -52,17 +52,17 @@ var bankenliste = { 'url': '' } } + // 2. FinTSClient anlegen // BLZ: 12345678 // Kunden-ID/Benutzerkennung: test1 // PIN: 1234 // Bankenliste siehe oben var client = new FinTSClient(12345678, 'test1', '1234', bankenliste, log) - // start GetKontoUmsaetze() -async GetKontoUmsaetze(){ +async function GetKontoUmsaetze () { try{ // 3. Verbindung aufbauen await client.EstablishConnection() @@ -76,14 +76,20 @@ async GetKontoUmsaetze(){ } // Alles gut // 5. Umsätze darstellen - console.log(JSON.stringify(data)) + console.log(JSON.stringify(daten)) + + // 6. Zeige Salden + for (let konto of client.konten) { + let saldo = await client.MsgGetSaldo(konto.sepa_data) + console.log('Saldo von Konto ' + konto.iban + ' ist ' + JSON.stringify(saldo.saldo.saldo)) + } - // 6. Verbindung beenden + // 7. Verbindung beenden await client.MsgEndDialog() }catch(exception){ console.error("Fehler: " + exception) } - // 7. Secure Daten im Objekt aus dem Ram löschen + // 8. Secure Daten im Objekt aus dem Ram löschen client.closeSecure() console.log('ENDE') } diff --git a/lib/FinTSClient.js b/lib/FinTSClient.js index efeaab2..64c2f36 100644 --- a/lib/FinTSClient.js +++ b/lib/FinTSClient.js @@ -728,7 +728,7 @@ var FinTSClient = function (in_blz, in_kunden_id, in_pin, in_bankenlist, logger) }) } - MsgEndDialog = function (cb) { + function MsgEndDialog (cb) { var msg = new Nachricht(me.proto_version) if (me.kunden_id != 9999999999) { msg.sign({ @@ -876,11 +876,11 @@ var FinTSClient = function (in_blz, in_kunden_id, in_pin, in_bankenlist, logger) MsgGetKontoUmsaetze(konto, from_date, to_date, cb) else return new Promise(function(resolve, reject) { - MsgGetKontoUmsaetze(konto, from_date, to_date, function (error, rMsg, data) { + MsgGetKontoUmsaetze(konto, from_date, to_date, function (error, recvMsg, umsaetze) { if(error) reject(error) else - resolve({'rMsg': rMsg, 'data': data}) + resolve({recvMsg, umsaetze}) }) }) } @@ -1016,12 +1016,12 @@ var FinTSClient = function (in_blz, in_kunden_id, in_pin, in_bankenlist, logger) if(error) reject(error) else - resolve({'recvMsg': recvMsg, 'saldo': saldo}) + resolve({recvMsg, saldo}) }) }) } - MsgGetSaldo = function (konto, cb) { + function MsgGetSaldo (konto, cb) { var req_saldo = new Order(me) var processed = false var v5 = null @@ -1137,7 +1137,7 @@ var FinTSClient = function (in_blz, in_kunden_id, in_pin, in_bankenlist, logger) }) } - EstablishConnection = function (cb) { + function EstablishConnection (cb) { var protocol_switch = false var vers_step = 1 var original_bpd = me.bpd.clone() From 938d9916f8e612c3b3cb5082ebebfa2fbee5f9fc Mon Sep 17 00:00:00 2001 From: reinerBa Date: Tue, 15 Aug 2017 20:20:30 +0200 Subject: [PATCH 3/7] More documentation and more functional constructor --- lib/FinTSClient.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/FinTSClient.js b/lib/FinTSClient.js index 64c2f36..dea33d7 100644 --- a/lib/FinTSClient.js +++ b/lib/FinTSClient.js @@ -273,6 +273,16 @@ var FinTSClient = function (in_blz, in_kunden_id, in_pin, in_bankenlist, logger) me.tan = NULL me.debug_mode = false + // Use given blz and forth parameter as url if it's a string + if (typeof in_bankenlist === 'string'){ + in_bankenlist = { + in_blz: { + 'blz': in_blz, + 'url': in_bankenlist + } + } + } + // Technical me.dialog_id = 0 me.next_msg_nr = 1 @@ -714,6 +724,11 @@ var FinTSClient = function (in_blz, in_kunden_id, in_pin, in_bankenlist, logger) MsgEndDialog(cb) } + /** + * Beendet eine Verbindung + * @param {function} callback - optional if promise is possible + * @return {promise} The optional Promise when no cb is given or Promise is undefined + */ me.MsgEndDialog = function (cb) { if(cb || typeof Promise !== 'function') MsgEndDialog(cb) @@ -864,7 +879,6 @@ var FinTSClient = function (in_blz, in_kunden_id, in_pin, in_bankenlist, logger) /** * Lädt die Kontenumsätze für ein bestimmtes Konto - * @constructor * @param {object} konto - {iban,bic,konto_nr,unter_konto,ctry_code,blz} * @param {Date} from_date - The date to start from_date * @param {Date} to_date - The date to stop at @@ -1003,7 +1017,7 @@ var FinTSClient = function (in_blz, in_kunden_id, in_pin, in_bankenlist, logger) /** * Lädt den Saldo eines bestimmten Kontos - * @param {object} konto - {iban,bic,konto_nr,unter_konto,ctry_code,blz} + * @param {object} konto - Das Konto für das der Saldo geladen werden sollen: {iban,bic,konto_nr,unter_konto,ctry_code,blz} * @param {function} callback - optional if promise is possible * @return {promise} The optional Promise when no cb is given or Promise is undefined */ @@ -1122,7 +1136,11 @@ var FinTSClient = function (in_blz, in_kunden_id, in_pin, in_bankenlist, logger) } }) } - + /** + * Vereinfachte Variante um eine Verbindung mit der Bank aufzubauen + * @param {function} callback - optional if promise is possible + * @return {promise} The optional Promise when no cb is given or Promise is undefined + */ me.EstablishConnection = function (cb) { if(cb || typeof Promise !== 'function') EstablishConnection(cb) From 51c0ab93ca3ee1d06a1df06662ad1b03eae00c10 Mon Sep 17 00:00:00 2001 From: reinerBa Date: Tue, 15 Aug 2017 20:35:57 +0200 Subject: [PATCH 4/7] Fix constructor bug --- lib/FinTSClient.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/FinTSClient.js b/lib/FinTSClient.js index dea33d7..d5532c3 100644 --- a/lib/FinTSClient.js +++ b/lib/FinTSClient.js @@ -275,11 +275,11 @@ var FinTSClient = function (in_blz, in_kunden_id, in_pin, in_bankenlist, logger) // Use given blz and forth parameter as url if it's a string if (typeof in_bankenlist === 'string'){ - in_bankenlist = { - in_blz: { - 'blz': in_blz, - 'url': in_bankenlist - } + var bank_url = in_bankenlist + in_bankenlist = {} + in_bankenlist[in_blz] = { + 'blz': in_blz, + 'url': bank_url } } From dd9c85fc32fc485663177fd8a76200ca1e53952b Mon Sep 17 00:00:00 2001 From: reinerBa Date: Thu, 26 Apr 2018 21:13:05 +0200 Subject: [PATCH 5/7] Update Readme.md --- Readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Readme.md b/Readme.md index 48d6167..77c8b76 100644 --- a/Readme.md +++ b/Readme.md @@ -1,6 +1,8 @@ [![NPM Version][npm-image]][npm-url] # Open-Fin-TS-JS-Client + npm i open-fin-ts-js-client-promise + FinTS/HBCI ist eine standardisierte Schnittstelle zur Kommunikation mit Banken von der Deutschen Kreditwirtschaft (DK). Es existieren derzeit drei Versionen der Schnittstelle. * HBCI 2.2 bzw. HBCI+ - Diese API unterstützt diese Version. From fe7b6c03e9a7d0b77954f139605beeb5d4aca8dc Mon Sep 17 00:00:00 2001 From: reinerBa Date: Thu, 26 Apr 2018 21:16:52 +0200 Subject: [PATCH 6/7] Update Readme.md --- Readme.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 77c8b76..3946d6f 100644 --- a/Readme.md +++ b/Readme.md @@ -1,4 +1,5 @@ -[![NPM Version][npm-image]][npm-url] +[![npm](https://img.shields.io/npm/v/open-fin-ts-js-client-promise.svg)](https://www.npmjs.com/package/open-fin-ts-js-client-promise) + # Open-Fin-TS-JS-Client npm i open-fin-ts-js-client-promise From 8c17e1a87193a993ce54d18849f07a8d8f4ab660 Mon Sep 17 00:00:00 2001 From: reinerBa Date: Thu, 26 Apr 2018 23:36:42 +0200 Subject: [PATCH 7/7] Update Readme.md --- Readme.md | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/Readme.md b/Readme.md index 3946d6f..60c1cdc 100644 --- a/Readme.md +++ b/Readme.md @@ -1,5 +1,55 @@ [![npm](https://img.shields.io/npm/v/open-fin-ts-js-client-promise.svg)](https://www.npmjs.com/package/open-fin-ts-js-client-promise) +## Quick-Start Async +Der einfachste Weg ist Open-Fin-TS-JS-Client über NPM durch eine Dependency in der package.json in ein Projekt einzubinden. +Am folgenden Beispiel zum Laden von Kontoumsätzen wird gezeigt wie der Client zu bedienen ist. Bitte beachten: für die keywords async/await und Promise ist die entsprechende Nodejs Version nötig >= 7.6 + +```js +var FinTSClient = require("open-fin-ts-js-client"); +// 1. Definition der Bankenliste - Echte URLs sind hier http://www.hbci-zka.de/institute/institut_auswahl.htm erhältlich. Für mehrere Urls ist ein Object nötig. +var bankenliste = "http://localhost:3000/cgi-bin/hbciservlet" +// 2. FinTSClient anlegen +// BLZ: 12345678 +// Kunden-ID/Benutzerkennung: test1 +// PIN: 1234 +// Bankenliste siehe oben +var client = new FinTSClient(12345678, "test1", "1234", bankenliste); +// start +GetKontoUmsaetze() + +async function GetKontoUmsaetze () { + try{ + // 3. Verbindung aufbauen + await client.EstablishConnection() + console.log('Erfolgreich Verbunden') + + // 4. Kontoumsätze für alle Konten nacheinander laden + let daten=[] + for (let konto of client.konten) { + let data = await client.MsgGetKontoUmsaetze(konto.sepa_data, null, null) + daten.push(data) + } + // Alles gut + // 5. Umsätze darstellen + console.log(JSON.stringify(daten)) + + // 6. Zeige Salden aller Konten + for (let konto of client.konten) { + let saldo = await client.MsgGetSaldo(konto.sepa_data) + console.log('Saldo von Konto ' + konto.iban + ' ist ' + JSON.stringify(saldo.saldo.saldo)) + } + + // 7. Verbindung beenden + await client.MsgEndDialog() + }catch(exception){ + console.error("Fehler: " + exception) + } + // 8. Secure Daten im Objekt aus dem Ram löschen + client.closeSecure() + console.log('ENDE') +} +``` + # Open-Fin-TS-JS-Client npm i open-fin-ts-js-client-promise