chainlib/cic_tools/eth/connection.py

32 lines
737 B
Python
Raw Normal View History

2021-02-08 10:39:42 +01:00
import logging
import json
from urllib.request import (
Request,
urlopen,
)
from .error import DefaultErrorParser
from .method import jsonrpc_result
error_parser = DefaultErrorParser()
logg = logging.getLogger(__name__)
class HTTPConnection:
def __init__(self, url):
self.url = url
def do(self, o, error_parser=error_parser):
req = Request(
self.url,
method='POST',
)
req.add_header('Content-Type', 'application/json')
data = json.dumps(o)
logg.debug('(HTTP) send {}'.format(data))
res = urlopen(req, data=data.encode('utf-8'))
o = json.load(res)
return jsonrpc_result(o, error_parser)