Skip to content

Commit d4d03eb

Browse files
committed
Use with in examples, remove close in json()
1 parent 12e6b47 commit d4d03eb

25 files changed

+213
-288
lines changed

adafruit_requests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ def json(self) -> Any:
321321
obj = json_module.load(self._raw)
322322
if not self._cached:
323323
self._cached = obj
324-
self.close()
324+
325325
return obj
326326

327327
def iter_content(self, chunk_size: int = 1, decode_unicode: bool = False) -> bytes:

examples/cpython/requests_cpython_advanced.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,13 @@
1515
headers = {"user-agent": "blinka/1.0.0"}
1616

1717
print("Fetching JSON data from %s..." % JSON_GET_URL)
18-
response = requests.get(JSON_GET_URL, headers=headers)
19-
print("-" * 60)
20-
21-
json_data = response.json()
22-
headers = json_data["headers"]
23-
print("Response's Custom User-Agent Header: {0}".format(headers["User-Agent"]))
24-
print("-" * 60)
25-
26-
# Read Response's HTTP status code
27-
print("Response HTTP Status Code: ", response.status_code)
28-
print("-" * 60)
29-
30-
# Close, delete and collect the response data
31-
response.close()
18+
with requests.get(JSON_GET_URL, headers=headers) as response:
19+
print("-" * 60)
20+
json_data = response.json()
21+
headers = json_data["headers"]
22+
print("Response's Custom User-Agent Header: {0}".format(headers["User-Agent"]))
23+
print("-" * 60)
24+
25+
# Read Response's HTTP status code
26+
print("Response HTTP Status Code: ", response.status_code)
27+
print("-" * 60)

examples/cpython/requests_cpython_simpletest.py

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,31 @@
1414
JSON_POST_URL = "https://httpbin.org/post"
1515

1616
print("Fetching text from %s" % TEXT_URL)
17-
response = requests.get(TEXT_URL)
18-
print("-" * 40)
19-
20-
print("Text Response: ", response.text)
21-
print("-" * 40)
22-
response.close()
17+
with requests.get(TEXT_URL) as response:
18+
print("-" * 40)
19+
print("Text Response: ", response.text)
20+
print("-" * 40)
2321

2422
print("Fetching JSON data from %s" % JSON_GET_URL)
25-
response = requests.get(JSON_GET_URL)
26-
print("-" * 40)
27-
28-
print("JSON Response: ", response.json())
29-
print("-" * 40)
30-
response.close()
23+
with requests.get(JSON_GET_URL) as response:
24+
print("-" * 40)
25+
print("JSON Response: ", response.json())
26+
print("-" * 40)
3127

3228
data = "31F"
3329
print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
34-
response = requests.post(JSON_POST_URL, data=data)
35-
print("-" * 40)
36-
37-
json_resp = response.json()
38-
# Parse out the 'data' key from json_resp dict.
39-
print("Data received from server:", json_resp["data"])
40-
print("-" * 40)
41-
response.close()
30+
with requests.post(JSON_POST_URL, data=data) as response:
31+
print("-" * 40)
32+
json_resp = response.json()
33+
# Parse out the 'data' key from json_resp dict.
34+
print("Data received from server:", json_resp["data"])
35+
print("-" * 40)
4236

4337
json_data = {"Date": "July 25, 2019"}
4438
print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
45-
response = requests.post(JSON_POST_URL, json=json_data)
46-
print("-" * 40)
47-
48-
json_resp = response.json()
49-
# Parse out the 'json' key from json_resp dict.
50-
print("JSON Data received from server:", json_resp["json"])
51-
print("-" * 40)
52-
response.close()
39+
with requests.post(JSON_POST_URL, json=json_data) as response:
40+
print("-" * 40)
41+
json_resp = response.json()
42+
# Parse out the 'json' key from json_resp dict.
43+
print("JSON Data received from server:", json_resp["json"])
44+
print("-" * 40)

examples/esp32spi/requests_esp32spi_advanced.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,14 @@
5353
headers = {"user-agent": "blinka/1.0.0"}
5454

5555
print("Fetching JSON data from %s..." % JSON_GET_URL)
56-
response = requests.get(JSON_GET_URL, headers=headers)
57-
print("-" * 60)
56+
with requests.get(JSON_GET_URL, headers=headers) as response:
57+
print("-" * 60)
5858

59-
json_data = response.json()
60-
headers = json_data["headers"]
61-
print("Response's Custom User-Agent Header: {0}".format(headers["User-Agent"]))
62-
print("-" * 60)
59+
json_data = response.json()
60+
headers = json_data["headers"]
61+
print("Response's Custom User-Agent Header: {0}".format(headers["User-Agent"]))
62+
print("-" * 60)
6363

64-
# Read Response's HTTP status code
65-
print("Response HTTP Status Code: ", response.status_code)
66-
print("-" * 60)
67-
68-
# Close, delete and collect the response data
69-
response.close()
64+
# Read Response's HTTP status code
65+
print("Response HTTP Status Code: ", response.status_code)
66+
print("-" * 60)

examples/esp32spi/requests_esp32spi_simpletest.py

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -52,39 +52,31 @@
5252
JSON_POST_URL = "https://httpbin.org/post"
5353

5454
print("Fetching text from %s" % TEXT_URL)
55-
response = requests.get(TEXT_URL)
56-
print("-" * 40)
57-
58-
print("Text Response: ", response.text)
59-
print("-" * 40)
60-
response.close()
55+
with requests.get(TEXT_URL) as response:
56+
print("-" * 40)
57+
print("Text Response: ", response.text)
58+
print("-" * 40)
6159

6260
print("Fetching JSON data from %s" % JSON_GET_URL)
63-
response = requests.get(JSON_GET_URL)
64-
print("-" * 40)
65-
66-
print("JSON Response: ", response.json())
67-
print("-" * 40)
68-
response.close()
61+
with requests.get(JSON_GET_URL) as response:
62+
print("-" * 40)
63+
print("JSON Response: ", response.json())
64+
print("-" * 40)
6965

7066
data = "31F"
7167
print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
72-
response = requests.post(JSON_POST_URL, data=data)
73-
print("-" * 40)
74-
75-
json_resp = response.json()
76-
# Parse out the 'data' key from json_resp dict.
77-
print("Data received from server:", json_resp["data"])
78-
print("-" * 40)
79-
response.close()
68+
with requests.post(JSON_POST_URL, data=data) as response:
69+
print("-" * 40)
70+
json_resp = response.json()
71+
# Parse out the 'data' key from json_resp dict.
72+
print("Data received from server:", json_resp["data"])
73+
print("-" * 40)
8074

8175
json_data = {"Date": "July 25, 2019"}
8276
print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
83-
response = requests.post(JSON_POST_URL, json=json_data)
84-
print("-" * 40)
85-
86-
json_resp = response.json()
87-
# Parse out the 'json' key from json_resp dict.
88-
print("JSON Data received from server:", json_resp["json"])
89-
print("-" * 40)
90-
response.close()
77+
with requests.post(JSON_POST_URL, json=json_data) as response:
78+
print("-" * 40)
79+
json_resp = response.json()
80+
# Parse out the 'json' key from json_resp dict.
81+
print("JSON Data received from server:", json_resp["json"])
82+
print("-" * 40)

examples/fona/requests_fona_advanced.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,14 @@
5454
headers = {"user-agent": "blinka/1.0.0"}
5555

5656
print("Fetching JSON data from %s..." % JSON_GET_URL)
57-
response = requests.get(JSON_GET_URL, headers=headers)
58-
print("-" * 60)
57+
with requests.get(JSON_GET_URL, headers=headers) as response:
58+
print("-" * 60)
5959

60-
json_data = response.json()
61-
headers = json_data["headers"]
62-
print("Response's Custom User-Agent Header: {0}".format(headers["User-Agent"]))
63-
print("-" * 60)
60+
json_data = response.json()
61+
headers = json_data["headers"]
62+
print("Response's Custom User-Agent Header: {0}".format(headers["User-Agent"]))
63+
print("-" * 60)
6464

65-
# Read Response's HTTP status code
66-
print("Response HTTP Status Code: ", response.status_code)
67-
print("-" * 60)
68-
69-
# Close, delete and collect the response data
70-
response.close()
65+
# Read Response's HTTP status code
66+
print("Response HTTP Status Code: ", response.status_code)
67+
print("-" * 60)

examples/fona/requests_fona_simpletest.py

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,39 +53,31 @@
5353
JSON_POST_URL = "http://httpbin.org/post"
5454

5555
print("Fetching text from %s" % TEXT_URL)
56-
response = requests.get(TEXT_URL)
57-
print("-" * 40)
58-
59-
print("Text Response: ", response.text)
60-
print("-" * 40)
61-
response.close()
56+
with requests.get(TEXT_URL) as response:
57+
print("-" * 40)
58+
print("Text Response: ", response.text)
59+
print("-" * 40)
6260

6361
print("Fetching JSON data from %s" % JSON_GET_URL)
64-
response = requests.get(JSON_GET_URL)
65-
print("-" * 40)
66-
67-
print("JSON Response: ", response.json())
68-
print("-" * 40)
69-
response.close()
62+
with requests.get(JSON_GET_URL) as response:
63+
print("-" * 40)
64+
print("JSON Response: ", response.json())
65+
print("-" * 40)
7066

7167
data = "31F"
7268
print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
73-
response = requests.post(JSON_POST_URL, data=data)
74-
print("-" * 40)
75-
76-
json_resp = response.json()
77-
# Parse out the 'data' key from json_resp dict.
78-
print("Data received from server:", json_resp["data"])
79-
print("-" * 40)
80-
response.close()
69+
with requests.post(JSON_POST_URL, data=data) as response:
70+
print("-" * 40)
71+
json_resp = response.json()
72+
# Parse out the 'data' key from json_resp dict.
73+
print("Data received from server:", json_resp["data"])
74+
print("-" * 40)
8175

8276
json_data = {"Date": "July 25, 2019"}
8377
print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
84-
response = requests.post(JSON_POST_URL, json=json_data)
85-
print("-" * 40)
86-
87-
json_resp = response.json()
88-
# Parse out the 'json' key from json_resp dict.
89-
print("JSON Data received from server:", json_resp["json"])
90-
print("-" * 40)
91-
response.close()
78+
with requests.post(JSON_POST_URL, json=json_data) as response:
79+
print("-" * 40)
80+
json_resp = response.json()
81+
# Parse out the 'json' key from json_resp dict.
82+
print("JSON Data received from server:", json_resp["json"])
83+
print("-" * 40)

examples/wifi/expanded/requests_wifi_adafruit_discord_active_online.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ def time_calc(input_time):
6060
DEBUG_RESPONSE = True
6161

6262
try:
63-
shieldsio_response = requests.get(url=ADA_DISCORD_JSON)
64-
shieldsio_json = shieldsio_response.json()
63+
with requests.get(url=ADA_DISCORD_JSON) as shieldsio_response:
64+
shieldsio_json = shieldsio_response.json()
6565
except ConnectionError as e:
6666
print(f"Connection Error: {e}")
6767
print("Retrying in 10 seconds")

examples/wifi/expanded/requests_wifi_api_discord.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ def time_calc(input_time):
6969
DEBUG_RESPONSE = False
7070

7171
try:
72-
discord_response = requests.get(url=DISCORD_SOURCE, headers=DISCORD_HEADER)
73-
discord_json = discord_response.json()
72+
with requests.get(
73+
url=DISCORD_SOURCE, headers=DISCORD_HEADER
74+
) as discord_response:
75+
discord_json = discord_response.json()
7476
except ConnectionError as e:
7577
print(f"Connection Error: {e}")
7678
print("Retrying in 10 seconds")

examples/wifi/expanded/requests_wifi_api_fitbit.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,16 @@ def time_calc(input_time):
174174
print(f"Current Refresh Token: {Refresh_Token}")
175175
# TOKEN REFRESH POST
176176
try:
177-
fitbit_oauth_refresh_POST = requests.post(
177+
with requests.post(
178178
url=FITBIT_OAUTH_TOKEN,
179179
data=FITBIT_OAUTH_REFRESH_TOKEN,
180180
headers=FITBIT_OAUTH_HEADER,
181-
)
181+
) as fitbit_oauth_refresh_POST:
182+
fitbit_refresh_oauth_json = fitbit_oauth_refresh_POST.json()
182183
except adafruit_requests.OutOfRetries as ex:
183184
print(f"OutOfRetries: {ex}")
184185
break
185186
try:
186-
fitbit_refresh_oauth_json = fitbit_oauth_refresh_POST.json()
187-
188187
fitbit_new_token = fitbit_refresh_oauth_json["access_token"]
189188
if DEBUG:
190189
print("Your Private SHA-256 Token: ", fitbit_new_token)
@@ -252,9 +251,9 @@ def time_calc(input_time):
252251
print(" | Attempting to GET Fitbit JSON!")
253252
FBIS = FITBIT_INTRADAY_SOURCE
254253
FBH = fitbit_header
255-
fitbit_get_response = requests.get(url=FBIS, headers=FBH)
256254
try:
257-
fitbit_json = fitbit_get_response.json()
255+
with requests.get(url=FBIS, headers=FBH) as fitbit_get_response:
256+
fitbit_json = fitbit_get_response.json()
258257
except ConnectionError as e:
259258
print("Connection Error:", e)
260259
print("Retrying in 10 seconds")
@@ -318,9 +317,9 @@ def time_calc(input_time):
318317
print(" | Attempting to GET Device JSON!")
319318
FBDS = FITBIT_DEVICE_SOURCE
320319
FBH = fitbit_header
321-
fitbit_get_device_response = requests.get(url=FBDS, headers=FBH)
322320
try:
323-
fitbit_device_json = fitbit_get_device_response.json()
321+
with requests.get(url=FBDS, headers=FBH) as fitbit_get_device_response:
322+
fitbit_device_json = fitbit_get_device_response.json()
324323
except ConnectionError as e:
325324
print("Connection Error:", e)
326325
print("Retrying in 10 seconds")

examples/wifi/expanded/requests_wifi_api_github.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ def time_calc(input_time):
6161
try:
6262
print(" | Attempting to GET Github JSON!")
6363
try:
64-
github_response = requests.get(url=GITHUB_SOURCE, headers=GITHUB_HEADER)
65-
github_json = github_response.json()
64+
with requests.get(
65+
url=GITHUB_SOURCE, headers=GITHUB_HEADER
66+
) as github_response:
67+
github_json = github_response.json()
6668
except ConnectionError as e:
6769
print("Connection Error:", e)
6870
print("Retrying in 10 seconds")
@@ -92,9 +94,6 @@ def time_calc(input_time):
9294
print("Full API GET URL: ", GITHUB_SOURCE)
9395
print(github_json)
9496

95-
github_response.close()
96-
print("✂️ Disconnected from Github API")
97-
9897
print("\nFinished!")
9998
print(f"Board Uptime: {time_calc(time.monotonic())}")
10099
print(f"Next Update: {time_calc(SLEEP_TIME)}")

examples/wifi/expanded/requests_wifi_api_mastodon.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ def time_calc(input_time):
7575
DEBUG_RESPONSE = False
7676

7777
try:
78-
mastodon_response = requests.get(url=MAST_SOURCE)
79-
mastodon_json = mastodon_response.json()
78+
with requests.get(url=MAST_SOURCE) as mastodon_response:
79+
mastodon_json = mastodon_response.json()
8080
except ConnectionError as e:
8181
print(f"Connection Error: {e}")
8282
print("Retrying in 10 seconds")

examples/wifi/expanded/requests_wifi_api_openskynetwork_private.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ def _format_datetime(datetime):
9494
print(" | Attempting to GET OpenSky-Network Single Private Flight JSON!")
9595
print(" | Website Credentials Required! Allows more daily calls than Public.")
9696
try:
97-
opensky_response = requests.get(url=OPENSKY_SOURCE, headers=OPENSKY_HEADER)
98-
opensky_json = opensky_response.json()
97+
with requests.get(
98+
url=OPENSKY_SOURCE, headers=OPENSKY_HEADER
99+
) as opensky_response:
100+
opensky_json = opensky_response.json()
99101
except ConnectionError as e:
100102
print("Connection Error:", e)
101103
print("Retrying in 10 seconds")
@@ -178,9 +180,6 @@ def _format_datetime(datetime):
178180
else:
179181
print(" | | ❌ Flight has no active data or you're polling too fast.")
180182

181-
opensky_response.close()
182-
print("✂️ Disconnected from OpenSky-Network API")
183-
184183
print("\nFinished!")
185184
print(f"Board Uptime: {time_calc(time.monotonic())}")
186185
print(f"Next Update: {time_calc(SLEEP_TIME)}")

0 commit comments

Comments
 (0)