From 156147d522144ee41266b3388fae1f809bb9e86f Mon Sep 17 00:00:00 2001 From: Mudassir Chapra <37051110+muddi900@users.noreply.github.com> Date: Sun, 7 May 2023 04:10:21 +0000 Subject: [PATCH] Added with statement for requests[Issue #100] --- adafruit_io/adafruit_io.py | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/adafruit_io/adafruit_io.py b/adafruit_io/adafruit_io.py index 03adf86..c40b018 100755 --- a/adafruit_io/adafruit_io.py +++ b/adafruit_io/adafruit_io.py @@ -537,12 +537,12 @@ def _post(self, path, payload): :param str path: Formatted Adafruit IO URL from _compose_path :param json payload: JSON data to send to Adafruit IO """ - response = self._http.post( + with self._http.post( path, json=payload, headers=self._create_headers(self._aio_headers[0]) - ) - self._handle_error(response) - json_data = response.json() - response.close() + ) as response: + self._handle_error(response) + json_data = response.json() + return json_data def _get(self, path): @@ -551,12 +551,11 @@ def _get(self, path): :param str path: Formatted Adafruit IO URL from _compose_path """ - response = self._http.get( + with self._http.get( path, headers=self._create_headers(self._aio_headers[1]) - ) - self._handle_error(response) - json_data = response.json() - response.close() + ) as response: + self._handle_error(response) + json_data = response.json() return json_data def _delete(self, path): @@ -565,12 +564,12 @@ def _delete(self, path): :param str path: Formatted Adafruit IO URL from _compose_path """ - response = self._http.delete( + with self._http.delete( path, headers=self._create_headers(self._aio_headers[0]) - ) - self._handle_error(response) - json_data = response.json() - response.close() + ) as response: + self._handle_error(response) + json_data = response.json() + return json_data # Data