From 4ac23c006c82cde3b953a3809c57f0d62451b129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20N=C3=A1jera?= Date: Sun, 3 Jan 2021 00:46:54 +0100 Subject: [PATCH] Consolidate rpc Proxy headers in single class property --- bitcoin/rpc.py | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/bitcoin/rpc.py b/bitcoin/rpc.py index cd48420c..e283d3b6 100644 --- a/bitcoin/rpc.py +++ b/bitcoin/rpc.py @@ -224,14 +224,8 @@ def __init__(self, self.__conn = httplib.HTTPConnection(self.__url.hostname, port=port, timeout=timeout) - def _call(self, service_name, *args): - self.__id_count += 1 - - postdata = json.dumps({'version': '1.1', - 'method': service_name, - 'params': args, - 'id': self.__id_count}) - + @property + def _headers(self): headers = { 'Host': self.__url.hostname, 'User-Agent': DEFAULT_USER_AGENT, @@ -241,7 +235,18 @@ def _call(self, service_name, *args): if self.__auth_header is not None: headers['Authorization'] = self.__auth_header - self.__conn.request('POST', self.__url.path, postdata, headers) + return headers + + def _call(self, service_name, *args): + self.__id_count += 1 + + postdata = json.dumps({'version': '1.1', + 'method': service_name, + 'params': args, + 'id': self.__id_count}) + + + self.__conn.request('POST', self.__url.path, postdata, self._headers) response = self._get_response() err = response.get('error') @@ -259,17 +264,7 @@ def _call(self, service_name, *args): def _batch(self, rpc_call_list): postdata = json.dumps(list(rpc_call_list)) - - headers = { - 'Host': self.__url.hostname, - 'User-Agent': DEFAULT_USER_AGENT, - 'Content-type': 'application/json', - } - - if self.__auth_header is not None: - headers['Authorization'] = self.__auth_header - - self.__conn.request('POST', self.__url.path, postdata, headers) + self.__conn.request('POST', self.__url.path, postdata, self._headers) return self._get_response() def _get_response(self):