From e14417d67e817172f5b9c465201e1b2e4c2839ef Mon Sep 17 00:00:00 2001
From: Moul <moul@moul.re>
Date: Mon, 3 May 2021 19:51:21 +0200
Subject: [PATCH] [lint] Use 'with * as' and remove close()

Handle new pylint v2.8.0 consider-using-with check
https://pylint.pycqa.org/en/latest/whatsnew/changelog.html#what-s-new-in-pylint-2-8-0
---
 duniterpy/api/client.py          | 40 ++++++++++++++++----------------
 examples/save_revoke_document.py |  5 ++--
 2 files changed, 22 insertions(+), 23 deletions(-)

diff --git a/duniterpy/api/client.py b/duniterpy/api/client.py
index 5216596e..709e4c4b 100644
--- a/duniterpy/api/client.py
+++ b/duniterpy/api/client.py
@@ -257,27 +257,27 @@ class API:
                 self.connection_handler.proxy, self.connection_handler.http_scheme
             )
 
-        response = request.urlopen(duniter_request, timeout=15)  # type: HTTPResponse
-
-        if response.status != 200:
+        with request.urlopen(
+            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(
+                            "status code != 200 => %d (%s)" % (response.status, content)
+                        ) from exception
+
+                raise ValueError(
+                    "status code != 200 => %d (%s)" % (response.status, content)
+                )
+
+            # get response content
+            return_response = copy.copy(response)
             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(
-                        "status code != 200 => %d (%s)" % (response.status, content)
-                    ) from exception
-
-            raise ValueError(
-                "status code != 200 => %d (%s)" % (response.status, content)
-            )
-
-        # get response content
-        return_response = copy.copy(response)
-        content = response.read().decode("utf-8")
-        response.close()
 
         # if schema supplied...
         if schema is not None:
diff --git a/examples/save_revoke_document.py b/examples/save_revoke_document.py
index 6bd3b9e8..1496f7d4 100644
--- a/examples/save_revoke_document.py
+++ b/examples/save_revoke_document.py
@@ -158,9 +158,8 @@ def main():
     )
 
     # save revoke document in a file
-    fp = open(REVOCATION_DOCUMENT_FILE_PATH, "w")
-    fp.write(revocation_signed_raw_document)
-    fp.close()
+    with open(REVOCATION_DOCUMENT_FILE_PATH, "w") as fp:
+        fp.write(revocation_signed_raw_document)
 
     # document saved
     print("Revocation document saved in %s" % REVOCATION_DOCUMENT_FILE_PATH)
-- 
GitLab