Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update requests_wifi_advanced to 9.0 with Connection Manager #178

Merged
merged 14 commits into from
Apr 28, 2024

Conversation

DJDevon3
Copy link
Contributor

@DJDevon3 DJDevon3 commented Mar 25, 2024

Updating requests_wifi_advanced to 9.0 with Connection Manager
This example is only for setting up a custom user-agent and using response status codes.

Added

  • uses f-strings for formatting
  • pretty serial hierarchy view with emojis (for IDE's that support them)
  • general code cleanup and improvements
  • shouty constants to make Pylint happy.
  • imports sorted
  • uses settings.toml by default
  • using Connection Manager by default

Serial output example:

Connecting to WiFi...
Signal Strength: -59
✅ Wifi!
 | Fetching JSON: https://httpbin.org/get
 | ✅ Response Custom User-Agent Header: blinka/1.0.0
 | ✅ Response HTTP Status Code: 200
 | ✂️ Disconnected from https://httpbin.org/get
Finished!

I think this example could be expanded upon to include the most common status codes and return the appropriate error message with them. I might work on this more.

@DJDevon3
Copy link
Contributor Author

DJDevon3 commented Mar 26, 2024

Added an HTTP status error code tester which works with httpbin.org I expanded it to test every code. I think this is more worthy of the filename as an advanced test. A great example that teaches how to set a custom user-agent and catch error codes with API's to do things with them.

Connecting to WiFi...
Signal Strength: -63
✅ Wifi!
 | GET JSON: https://httpbin.org/get
 | User-Agent: blinka/1.0.0
 | ✅ Status Test: 200 - OK
 | ✂️ Disconnected from https://httpbin.org/get
 | 
 | Status Code Test: https://httpbin.org/status/
 | ✅ Status Test: 100 - Continue
 | ✅ Status Test: 101 - Switching Protocols
 | ✅ Status Test: 102 - Processing
 | ✅ Status Test: 103 - Early Hints
 | ✅ Status Test: 200 - OK
 | ✅ Status Test: 201 - Created
 | ✅ Status Test: 202 - Accepted
 | ✅ Status Test: 203 - Non-Authoritative Information
 | ✅ Status Test: 204 - No Content
 | ✅ Status Test: 205 - Reset Content
 | ✅ Status Test: 206 - Partial Content
 | ✅ Status Test: 207 - Multi-Status
 | ✅ Status Test: 208 - Already Reported
 | ✅ Status Test: 226 - IM Used
 | ❌ Status Test: 300 - Multiple Choices
 | ✅ Status Test: 200 - OK
 | ✅ Status Test: 200 - OK
 | ✅ Status Test: 200 - OK
 | ❌ Status Test: 304 - Not Modified
 | ✅ Status Test: 200 - OK
 | ❌ Status Test: 306 - Unused
 | ✅ Status Test: 200 - OK
 | ❌ Status Test: 308 - Permanent Redirect
 | ❌ Status Test: 400 - Bad Request
 | ❌ Status Test: 401 - Unauthorized
 | ❌ Status Test: 402 - Payment Required
 | ❌ Status Test: 403 - Forbidden
 | ❌ Status Test: 404 - Not Found
 | ❌ Status Test: 405 - Method Not Allowed
 | ❌ Status Test: 406 - Not Acceptable
 | ❌ Status Test: 407 - Proxy Authentication Required
 | ❌ Status Test: 408 - Request Timeout
 | ❌ Status Test: 409 - Conflict
 | ❌ Status Test: 410 - Gone
 | ❌ Status Test: 411 - Length Required
 | ❌ Status Test: 412 - Precondition Failed
 | ❌ Status Test: 413 - Payload Too Large
 | ❌ Status Test: 414 - URI Too Long
 | ❌ Status Test: 415 - Unsupported Media Type
 | ❌ Status Test: 416 - Range Not Satisfiable
 | ❌ Status Test: 417 - Expectation Failed
 | ❌ Status Test: 418 - I'm a teapot
 | ❌ Status Test: 421 - Misdirected Request
 | ❌ Status Test: 422 - Unprocessable Entity
 | ❌ Status Test: 423 - Locked
 | ❌ Status Test: 424 - Failed Dependency
 | ❌ Status Test: 425 - Too Early
 | ❌ Status Test: 426 - Upgrade Required
 | ❌ Status Test: 428 - Precondition Required
 | ❌ Status Test: 429 - Too Many Requests
 | ❌ Status Test: 431 - Request Header Fields Too Large
 | ❌ Status Test: 451 - Unavailable For Legal Reasons
 | ❌ Status Test: 500 - Internal Server Error
 | ❌ Status Test: 501 - Not Implemented
 | ❌ Status Test: 502 - Bad Gateway
 | ❌ Status Test: 503 - Service Unavailable
 | ❌ Status Test: 504 - Gateway Timeout
 | ❌ Status Test: 505 - HTTP Version Not Supported
 | ❌ Status Test: 506 - Variant Also Negotiates
 | ❌ Status Test: 507 - Insufficient Storage
 | ❌ Status Test: 508 - Loop Detected
 | ❌ Status Test: 510 - Not Extended
 | ❌ Status Test: 511 - Network Authentication Required
 | ✂️ Disconnected from https://httpbin.org/get
Finished!

Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove the pylint disable commeent.

examples/wifi/requests_wifi_advanced.py Outdated Show resolved Hide resolved
cleaning up discrepancy between my local pylint and adafruit actions.
Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the complete list of status code checks that were added are a little excessive currently.

I would recommend one of:

  • leave this example as just the single request and make a new "status code list" example for this loop that checks all of these different ones
  • maybe update this example to have a small set of 4-6 or so most commonly used status codes only, and remove the rest, or move them into a separate example.

@DJDevon3
Copy link
Contributor Author

Sounds good will do.

@DJDevon3
Copy link
Contributor Author

DJDevon3 commented Apr 11, 2024

Modified closer to original. Since it's supposed to be called Advanced I put a few more http headers. in there.

Connecting to WiFi...
Signal Strength: -53
✅ Wifi!
 | Fetching JSON: https://httpbin.org/get
 | 🆗 Status Code: 200
 |  | Custom User-Agent Header: blinka/1.0.0
 |  | Content-Type: application/json
 |  | Response Timestamp: Thu, 11 Apr 2024 13:16:34 GMT
 | ✂️ Disconnected from https://httpbin.org/get

Split Status Code into its own example also included in this commit.

added additional http headers, removed most status codes for separate example
Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My results from testing the new status code example seem to show several repititions of some of the status codes instead of printing output for each of them once like the loop seems like it should make it do.

| ✅ Status Test: 202 - Accepted
 | ✅ Status Test: 203 - Non-Authoritative Information
 | ✅ Status Test: 204 - No Content
 | ✅ Status Test: 205 - Reset Content
 | ✅ Status Test: 206 - Partial Content
 | ✅ Status Test: 207 - Multi-Status
 | ✅ Status Test: 208 - Already Reported
 | ✅ Status Test: 226 - IM Used
 | ❌ Status Test: 300 - Multiple Choices
 | ✅ Status Test: 200 - OK
 | ✅ Status Test: 200 - OK
 | ✅ Status Test: 200 - OK
 | ❌ Status Test: 304 - Not Modified
 | ❌ Status Test: 502 - Bad Gateway
 | ❌ Status Test: 306 - Unused
 | ✅ Status Test: 200 - OK
 | ❌ Status Test: 502 - Bad Gateway
 | ❌ Status Test: 400 - Bad Request
 | ❌ Status Test: 401 - Unauthorized
 | ❌ Status Test: 402 - Payment Required
 | ❌ Status Test: 403 - Forbidden
 | ❌ Status Test: 404 - Not Found
 | ❌ Status Test: 405 - Method Not Allowed
 | ❌ Status Test: 406 - Not Acceptable
 | ❌ Status Test: 407 - Proxy Authentication Required
 | ❌ Status Test: 408 - Request Timeout
 | ❌ Status Test: 409 - Conflict
 | ❌ Status Test: 410 - Gone
 | ❌ Status Test: 411 - Length Required
 | ❌ Status Test: 412 - Precondition Failed
 | ❌ Status Test: 413 - Payload Too Large
 | ❌ Status Test: 414 - URI Too Long
 | ❌ Status Test: 415 - Unsupported Media Type
 | ❌ Status Test: 416 - Range Not Satisfiable
 | ❌ Status Test: 417 - Expectation Failed
 | ❌ Status Test: 418 - I'm a teapot
 | ❌ Status Test: 421 - Misdirected Request
 | ❌ Status Test: 422 - Unprocessable Entity
 | ❌ Status Test: 423 - Locked
 | ❌ Status Test: 424 - Failed Dependency
 | ❌ Status Test: 425 - Too Early
 | ❌ Status Test: 426 - Upgrade Required
 | ❌ Status Test: 428 - Precondition Required
 | ❌ Status Test: 429 - Too Many Requests
 | ❌ Status Test: 502 - Bad Gateway
 | ❌ Status Test: 451 - Unavailable For Legal Reasons
 | ❌ Status Test: 500 - Internal Server Error
 | ❌ Status Test: 501 - Not Implemented
 | ❌ Status Test: 502 - Bad Gateway
 | ❌ Status Test: 503 - Service Unavailable
 | ❌ Status Test: 504 - Gateway Timeout
 | ❌ Status Test: 505 - HTTP Version Not Supported
 | ❌ Status Test: 506 - Variant Also Negotiates
 | ❌ Status Test: 507 - Insufficient Storage
 | ❌ Status Test: 508 - Loop Detected
 | ❌ Status Test: 510 - Not Extended
 | ❌ Status Test: 511 - Network Authentication Required
 | ✂️ Disconnected from https://httpbin.org/get
Finished!

200 is repeated 5 times, one of which is in it's proper place in the sequence.

502 is repeated 4 times, one of which is in it's proper place in the sequeence.

I'm guessing that the server replying to these requests can't handle a few of the types we've attempted and returned 502 in those cases.

And for some of the redirects it looks like there may be client code in requests library that is automatically following the redirect which is ultimately resulting in 200 responses instead of the expected 30x ones.

I'm in favor of removing any of the status codes that are unsupported by the server and return 502 instead of the expected result.

For the 300s that end up following the redirect I think there is value in keeping them, but either a code comment or something in the print out should maybe try to clarify why they are different from expected status codes. Maybe they could specify what status was expected and what status was ultimately found? like this:

| ❌ Status Test: expected: 300 actual: 300 - Multiple Choices
 | ✅ Status Test: expected: 301 actual: 200 - OK
 | ✅ Status Test: expected: 302 actual: 200 - OK
 | ✅ Status Test: expected: 303 actual: 200 - OK
 | ❌ Status Test: expected: 304 actual: 304 - Not Modified
 | ✅ Status Test: expected: 305 actual: 200 - OK
 | ❌ Status Test: expected: 306 actual: 306 - Unused
 | ✅ Status Test: expected: 307 actual: 200 - OK

STATUS_DESCRIPTION = http_status_codes.get(STATUS_CODE, "Unknown Status Code")
print_http_status(STATUS_CODE, STATUS_DESCRIPTION)
response.close()
print(f" | ✂️ Disconnected from {JSON_GET_URL}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that this is moved into it's own example that illustrates the whole list of status codes I think I'm infavor of getting rid of this single request that happens up here before the list. I think we could get rid of the JSON_GET_URL entirely from this example and make it just the STATUS_TEST url.

examples/wifi/requests_wifi_status_codes.py Outdated Show resolved Hide resolved
@DJDevon3
Copy link
Contributor Author

Current output example

Connecting to WiFi...
Signal Strength: -60
✅ Wifi!
 | Fetching JSON data from https://httpbin.org/get...
 | 🆗 Status Code: 200
 |  | Custom User-Agent Header: blinka/1.0.0
 |  | Content-Type: application/json
 |  | Response Timestamp: Mon, 15 Apr 2024 16:55:12 GMT

@DJDevon3
Copy link
Contributor Author

changed to

Connecting to WiFi...
Signal Strength: -58
✅ Wifi!
 | Fetching URL https://httpbin.org/get
 | 🆗 Status Code: 200
 |  | Custom User-Agent Header: blinka/1.0.0
 |  | Content-Type: application/json
 |  | Response Timestamp: Mon, 15 Apr 2024 17:20:13 GMT

The variable is actually an http header status test so keeping it as url instead of json_url for that one is good.
@DJDevon3
Copy link
Contributor Author

looks like Justin fixed these in #188 so I basically just repeated the work before seeing his commit. Not sure what to do here. Should I close this PR?

@FoamyGuy
Copy link
Contributor

@DJDevon3 I think it can stay open. This PR contains the new status code example, and #188 contains updates to many other (perhaps all) of the examples.

So there is a little bit of crossover in the one advanced example, but there is also plenty of work that is separate and only in it's own respective PR on both sides.

Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are still a few unresolved changes that I think would be good to have before merging this.

  • remove the JSON_GET_URL and the single request that uses it that comes before the loop with all of the status requests. I think it's best to make the status example just do the status loop and not the single request before it, that looks to be leftover from when this was copied from advanced.
  • Add the expected status codes to the printed output so that its' easy to tell which codes are actually returning different from the expected values (mostly the 300s level redirects, but a few of the others them returned duplicates of 502 for me when I tested the other day as well)

I can make those changes and commit them to this branch if you'd like, let me know.

@DJDevon3
Copy link
Contributor Author

@FoamyGuy Yes please by all mean do. I'm a bit PR'd out at the moment. The ISFL31 typing PR took a lot of wind out of my sail. I would like to work on some 3D design projects and get my mind off code for a while.

# Conflicts:
#	examples/wifi/requests_wifi_advanced.py
…context inside status codes loop. Add expected code to print out.
Copy link
Contributor Author

@DJDevon3 DJDevon3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I like the changes to the status codes example, very nice.

Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latest commits incorporate all of the requested changes.

Thanks for taking a look @DJDevon3.

@FoamyGuy FoamyGuy merged commit 0f1b912 into adafruit:main Apr 28, 2024
1 check passed
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Apr 29, 2024
Updating https://github.com/adafruit/Adafruit_CircuitPython_Requests to 3.2.8 from 3.2.6:
  > Merge pull request adafruit/Adafruit_CircuitPython_Requests#182 from DJDevon3/DJDevon3-QueueTimesAPI
  > Merge pull request adafruit/Adafruit_CircuitPython_Requests#178 from DJDevon3/DJDevon3-WifiAdvanced

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Updated download stats for the libraries
@DJDevon3 DJDevon3 deleted the DJDevon3-WifiAdvanced branch May 1, 2024 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants