forked from raphaelm/python-fints
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.py
More file actions
38 lines (28 loc) · 1.23 KB
/
Copy pathconnection.py
File metadata and controls
38 lines (28 loc) · 1.23 KB
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
import base64
import io
import logging
import requests
from fints.utils import Password
from .exceptions import *
from .message import FinTSInstituteMessage, FinTSMessage
logger = logging.getLogger(__name__)
class FinTSHTTPSConnection:
def __init__(self, url):
self.url = url
def send(self, msg: FinTSMessage):
log_out = io.StringIO()
with Password.protect():
msg.print_nested(stream=log_out, prefix="\t")
logger.debug("Sending >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n{}\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n".format(log_out.getvalue()))
log_out.truncate(0)
r = requests.post(
self.url, data=base64.b64encode(msg.render_bytes()),
)
if r.status_code < 200 or r.status_code > 299:
raise FinTSConnectionError('Bad status code {}'.format(r.status_code))
response = base64.b64decode(r.content.decode('iso-8859-1'))
retval = FinTSInstituteMessage(segments=response)
with Password.protect():
retval.print_nested(stream=log_out, prefix="\t")
logger.debug("Received <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n{}\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n".format(log_out.getvalue()))
return retval