Skip to content

Commit 91efc34

Browse files
authored
Merge pull request #101 from muddi900/add/context-manager
Added with statement for requests[Issue #100]
2 parents cdaf615 + 156147d commit 91efc34

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

adafruit_io/adafruit_io.py

+14-15
Original file line numberDiff line numberDiff line change
@@ -537,12 +537,12 @@ def _post(self, path, payload):
537537
:param str path: Formatted Adafruit IO URL from _compose_path
538538
:param json payload: JSON data to send to Adafruit IO
539539
"""
540-
response = self._http.post(
540+
with self._http.post(
541541
path, json=payload, headers=self._create_headers(self._aio_headers[0])
542-
)
543-
self._handle_error(response)
544-
json_data = response.json()
545-
response.close()
542+
) as response:
543+
self._handle_error(response)
544+
json_data = response.json()
545+
546546
return json_data
547547

548548
def _get(self, path):
@@ -551,12 +551,11 @@ def _get(self, path):
551551
552552
:param str path: Formatted Adafruit IO URL from _compose_path
553553
"""
554-
response = self._http.get(
554+
with self._http.get(
555555
path, headers=self._create_headers(self._aio_headers[1])
556-
)
557-
self._handle_error(response)
558-
json_data = response.json()
559-
response.close()
556+
) as response:
557+
self._handle_error(response)
558+
json_data = response.json()
560559
return json_data
561560

562561
def _delete(self, path):
@@ -565,12 +564,12 @@ def _delete(self, path):
565564
566565
:param str path: Formatted Adafruit IO URL from _compose_path
567566
"""
568-
response = self._http.delete(
567+
with self._http.delete(
569568
path, headers=self._create_headers(self._aio_headers[0])
570-
)
571-
self._handle_error(response)
572-
json_data = response.json()
573-
response.close()
569+
) as response:
570+
self._handle_error(response)
571+
json_data = response.json()
572+
574573
return json_data
575574

576575
# Data

0 commit comments

Comments
 (0)