Last active
February 29, 2020 12:38
-
-
Save dylanjcastillo/a42dcdb60155930ca0a4c8650e9e2815 to your computer and use it in GitHub Desktop.
Code for post "Fast & Asynchronous: Accelerate Your Requests Using Python's asyncio"
This file contains hidden or 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 aiohttp | |
import asyncio | |
import os | |
from aiohttp import ClientSession | |
GOOGLE_BOOKS_URL = "https://www.googleapis.com/books/v1/volumes?q=isbn:" | |
LIST_ISBN = [ | |
'9780002005883', | |
'9780002238304', | |
'9780002261982', | |
'9780006163831', | |
'9780006178736', | |
'9780006280897', | |
'9780006280934', | |
'9780006353287', | |
'9780006380832', | |
'9780006470229', | |
] | |
def extract_fields_from_response(response): | |
"""Extract fields from API's response""" | |
item = response.get("items", [{}])[0] | |
volume_info = item.get("volumeInfo", {}) | |
title = volume_info.get("title", None) | |
subtitle = volume_info.get("subtitle", None) | |
description = volume_info.get("description", None) | |
published_date = volume_info.get("publishedDate", None) | |
return ( | |
title, | |
subtitle, | |
description, | |
published_date, | |
) | |
async def get_book_details_async(isbn, session): | |
"""Get book details using Google Books API (asynchronously)""" | |
url = GOOGLE_BOOKS_URL + isbn | |
try: | |
response = await session.request(method='GET', url=url) | |
response.raise_for_status() | |
print(f"Response status ({url}): {response.status}") | |
except HTTPError as http_err: | |
raise Exception(f"HTTP error occurred: {http_err}") | |
except Exception as e: | |
raise Exception(f"An error ocurred: {e}") | |
response_json = await response.json() | |
return response_json | |
async def run_program(isbn, session): | |
"""Wrapper for running program in an asynchronous manner""" | |
try: | |
response = await get_book_details_async(isbn, session) | |
parsed_response = extract_fields_from_response(response) | |
print(f"Response: {json.dumps(parsed_response, indent=2)}") | |
except Exception as err: | |
print(f"Exception occured: {err}") | |
pass | |
async with ClientSession() as session: | |
await asyncio.gather(*[run_program(isbn, session) for isbn in LIST_ISBN]) |
This file contains hidden or 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
def get_book_details_seq(isbn, session): | |
"""Get book details using Google Books API (sequentially)""" | |
url = GOOGLE_BOOKS_URL + isbn | |
response = None | |
try: | |
response = session.get(url) | |
response.raise_for_status() | |
print(f"Response status ({url}): {response.status_code}") | |
except HTTPError as http_err: | |
raise Exception(f"HTTP error occurred: {http_err}") | |
except Exception as e: | |
raise Exception(f"An error ocurred: {e}") | |
response_json = response.json() | |
items = response_json.get("items", [{}])[0] | |
return items | |
with requests.Session() as session: | |
for isbn in LIST_ISBN: | |
try: | |
response = get_book_details_seq(isbn, session) | |
parsed_response = extract_fields_from_response(response) | |
print(f"Response: {json.dumps(parsed_response, indent=2)}") | |
except Exception as err: | |
print(f"Exception occured: {err}") | |
pass |
This file contains hidden or 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
def extract_fields_from_response(response): | |
"""Extract fields from API's response""" | |
item = response.get("items", [{}])[0] | |
volume_info = item.get("volumeInfo", {}) | |
title = volume_info.get("title", None) | |
subtitle = volume_info.get("subtitle", None) | |
description = volume_info.get("description", None) | |
published_date = volume_info.get("publishedDate", None) | |
return ( | |
title, | |
subtitle, | |
description, | |
published_date, | |
) |
This file contains hidden or 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
{ | |
"kind": "books#volumes", | |
"totalItems": 1, | |
"items": [ | |
{ | |
"kind": "books#volume", | |
"id": "3Mx4QgAACAAJ", | |
"etag": "FWJF/JY16xg", | |
"selfLink": "https://www.googleapis.com/books/v1/volumes/3Mx4QgAACAAJ", | |
"volumeInfo": { | |
"title": "Mapping the Big Picture", | |
"subtitle": "Integrating Curriculum and Assessment, K-12", | |
... |
This file contains hidden or 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 os | |
import requests | |
from requests.exceptions import HTTPError | |
GOOGLE_BOOKS_URL = "https://www.googleapis.com/books/v1/volumes?q=isbn:" | |
LIST_ISBN = [ | |
'9780002005883', | |
'9780002238304', | |
'9780002261982', | |
'9780006163831', | |
'9780006178736', | |
'9780006280897', | |
'9780006280934', | |
'9780006353287', | |
'9780006380832', | |
'9780006470229', | |
] |
This file contains hidden or 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 os | |
import requests | |
from requests.exceptions import HTTPError | |
GOOGLE_BOOKS_URL = "https://www.googleapis.com/books/v1/volumes?q=isbn:" | |
LIST_ISBN = [ | |
'9780002005883', | |
'9780002238304', | |
'9780002261982', | |
'9780006163831', | |
'9780006178736', | |
'9780006280897', | |
'9780006280934', | |
'9780006353287', | |
'9780006380832', | |
'9780006470229', | |
] | |
def extract_fields_from_response(item): | |
"""Extract fields from API's response""" | |
volume_info = item.get("volumeInfo", {}) | |
title = volume_info.get("title", None) | |
subtitle = volume_info.get("subtitle", None) | |
description = volume_info.get("description", None) | |
published_date = volume_info.get("publishedDate", None) | |
return ( | |
title, | |
subtitle, | |
description, | |
published_date, | |
) | |
def get_book_details_seq(isbn, session): | |
"""Get book details using Google Books API (sequentially)""" | |
url = GOOGLE_BOOKS_URL + isbn | |
response = None | |
try: | |
response = session.get(url) | |
response.raise_for_status() | |
print(f"Response status ({url}): {response.status_code}") | |
except HTTPError as http_err: | |
raise Exception(f"HTTP error occurred: {http_err}") | |
except Exception as e: | |
raise Exception(f"An error ocurred: {e}") | |
response_json = response.json() | |
items = response_json.get("items", [{}])[0] | |
return items | |
with requests.Session() as session: | |
for isbn in LIST_ISBN: | |
try: | |
response = get_book_details_seq(isbn, session) | |
parsed_response = extract_fields_from_response(response) | |
print(f"Response: {json.dumps(parsed_response, indent=2)}") | |
except Exception as err: | |
print(f"Exception occured: {err}") | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment