Last active
October 6, 2021 10:04
-
-
Save adilkhash/c62454d939bb943549f938ee003270a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from http import HTTPStatus | |
import pprint | |
class HttpError: | |
def __init__(self, status_code: int): | |
self.status_code = status_code | |
class ServerError: | |
def __init__(self, status_code: int): | |
self.status_code = status_code | |
def get(url: str): | |
response = requests.get(url=url) | |
match response.status_code: | |
case ok if HTTPStatus.OK <= ok < HTTPStatus.BAD_REQUEST: | |
return response.json() | |
case http_error if HTTPStatus.BAD_REQUEST <= http_error < HTTPStatus.INTERNAL_SERVER_ERROR: | |
return HttpError(response.status_code) | |
case server_error if server_error >= HTTPStatus.INTERNAL_SERVER_ERROR: | |
return ServerError(response.status_code) | |
case _: | |
pass | |
if __name__ == '__main__': | |
match get('https://httpbin.org/status/500'): | |
case HttpError() as val: | |
print(f'http error, {val.status_code=}') | |
case ServerError() as val: | |
print(f'server error, {val.status_code=}') | |
case {**payload}: | |
pprint.pprint(payload) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment