diff --git a/duniterpy/api/client.py b/duniterpy/api/client.py index ac590693d16f7472e0863e2cd8e5950f6b191dad..fb478330e0ad838dac6df24bc9c9db302ce0c9a1 100644 --- a/duniterpy/api/client.py +++ b/duniterpy/api/client.py @@ -13,10 +13,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -import copy import json import logging -from http.client import HTTPResponse from typing import Any, Callable, Dict, Optional, Union from urllib import parse, request @@ -252,39 +250,31 @@ class API: self.connection_handler.proxy, self.connection_handler.http_scheme ) - with request.urlopen( + response = request.urlopen( # pylint: disable=consider-using-with duniter_request, timeout=15 - ) as response: # type: HTTPResponse - if response.status != 200: - content = response.read().decode("utf-8") - if bma_errors: - try: - error_data = parse_error(content) - raise DuniterError(error_data) - except (TypeError, jsonschema.ValidationError) as exception: - raise ValueError( - f"status code != 200 => {response.status} ({content})" - ) from exception - - raise ValueError(f"status code != 200 => {response.status} ({content})") - - # get response content - return_response = copy.copy(response) - content = response.read().decode("utf-8") - - # if schema supplied... - if schema is not None: + ) + content = response.read().decode("utf-8") + if response.status != 200: + if bma_errors: + try: + error_data = parse_error(content) + raise DuniterError(error_data) + except (TypeError, jsonschema.ValidationError) as exception: + raise ValueError( + f"status code != 200 => {response.status} ({content})" + ) from exception + + raise ValueError(f"status code != 200 => {response.status} ({content})") + + if rtype == RESPONSE_HTTP: + return response + # if schema supplied… + if schema: # validate response parse_response(content, schema) - - # return the chosen type - result = return_response # type: Any if rtype == RESPONSE_TEXT: - result = content - elif rtype == RESPONSE_JSON: - result = json.loads(content) - - return result + return content + return json.loads(content) def connect_ws(self, path: str) -> WSConnection: """