diff --git a/consuming-apis-python/README.md b/consuming-apis-python/README.md index a15d453e42..c68da8dc90 100644 --- a/consuming-apis-python/README.md +++ b/consuming-apis-python/README.md @@ -1,3 +1,21 @@ # Python & APIs: A Winning Combo for Reading Public Data Here you can find code examples showing how to read from different public APIs. Check out the Real Python tutorial [Python & APIs: A Winning Combo for Reading Public Data](https://realpython.com/python-api/) for more information. + +## Setup + +Create and activate a virtual environment, then install the dependencies: + +```console +$ python -m pip install -r requirements.txt +``` + +## Usage + +Run any of the scripts with Python: + +```console +$ python google_books.py +``` + +The GIPHY scripts need an API key from [GIPHY](https://developers.giphy.com/), and `github.py` needs the client ID and secret of a GitHub OAuth app. Replace the placeholders at the top of each script before running it. diff --git a/consuming-apis-python/giphy_search.py b/consuming-apis-python/giphy_search.py index 3c4b309903..2da1faa488 100644 --- a/consuming-apis-python/giphy_search.py +++ b/consuming-apis-python/giphy_search.py @@ -12,8 +12,8 @@ # 1. The number of GIFs to return using the `limit` parameter. # 2. The query term to search for using the `q` parameter. # 3. The accepted rating of the GIFs returned using the `rating` parameter -# https://developers.giphy.com/docs/optional-settings#rating -params = {"api_key": API_KEY, "limit": 3, "q": search_term, "rating": "g"} +# https://developers.giphy.com/branch/master/docs/optional-settings/#rating +params = {"api_key": API_KEY, "limit": 1, "q": search_term, "rating": "g"} # The response will contain a list with all the GIFs that match your query. # For each of those, you want to get the `title` and `url` fields. @@ -21,4 +21,4 @@ for gif in response["data"]: title = gif["title"] url = gif["url"] - print("%s | %s" % (title, url)) + print(f"{title} | {url}") diff --git a/consuming-apis-python/giphy_trending.py b/consuming-apis-python/giphy_trending.py index ae5fbac0e0..cf97242afa 100644 --- a/consuming-apis-python/giphy_trending.py +++ b/consuming-apis-python/giphy_trending.py @@ -8,7 +8,7 @@ # In the query parameters you can define: # 1. The number of GIFs to return using the `limit` parameter. # 2. The accepted rating of the GIFs returned using the `rating` parameter -# https://developers.giphy.com/docs/optional-settings#rating +# https://developers.giphy.com/branch/master/docs/optional-settings/#rating params = {"api_key": API_KEY, "limit": 3, "rating": "g"} # The response will contain a list with all the GIFs that match your query. @@ -19,4 +19,4 @@ title = gif["title"] trending_date = gif["trending_datetime"] url = gif["url"] - print("%s | %s | %s" % (title, trending_date, url)) + print(f"{title} | {trending_date}\n{url}\n") diff --git a/consuming-apis-python/github.py b/consuming-apis-python/github.py index e7b83d2cd0..5d2a32a216 100644 --- a/consuming-apis-python/github.py +++ b/consuming-apis-python/github.py @@ -1,7 +1,7 @@ import requests # First, make sure to follow the steps to create your own GitHub application: -# https://docs.github.com/en/free-pro-team@latest/developers/apps/creating-an-oauth-app +# https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/creating-an-oauth-app # REPLACE the following variables with your Client ID and Client Secret @@ -24,7 +24,7 @@ def create_oauth_link(): } # This endpoint is defined in the GitHub documentation: - # https://docs.github.com/en/free-pro-team@latest/developers/apps/authorizing-oauth-apps#1-request-a-users-github-identity + # https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/authorizing-oauth-apps#1-request-a-users-github-identity endpoint = "https://github.com/login/oauth/authorize" response = requests.get(endpoint, params=params) @@ -51,7 +51,7 @@ def exchange_code_for_access_token(code=None): headers = {"Accept": "application/json"} # This endpoint is defined in the GitHub documentation: - # https://docs.github.com/en/free-pro-team@latest/developers/apps/authorizing-oauth-apps#2-users-are-redirected-back-to-your-site-by-github + # https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/authorizing-oauth-apps#2-users-are-redirected-back-to-your-site-by-github endpoint = "https://github.com/login/oauth/access_token" response = requests.post(endpoint, params=params, headers=headers).json() @@ -66,7 +66,7 @@ def print_user_info(access_token=None): headers = {"Authorization": f"token {access_token}"} # This endpoint is defined in the GitHub documentation: - # https://docs.github.com/en/free-pro-team@latest/rest/reference/users#get-the-authenticated-user + # https://docs.github.com/en/rest/users#get-the-authenticated-user endpoint = "https://api.github.com/user" response = requests.get(endpoint, headers=headers).json() @@ -76,9 +76,7 @@ def print_user_info(access_token=None): name = response["name"] username = response["login"] private_repos_count = response["total_private_repos"] - print( - f"{name} ({username}) | number of private repositories: {private_repos_count}" - ) + print(f"{name} ({username}) | private repositories: {private_repos_count}") # So, one last time, step by step: diff --git a/consuming-apis-python/google_books.py b/consuming-apis-python/google_books.py index dc62a554a0..c0f055c1d9 100644 --- a/consuming-apis-python/google_books.py +++ b/consuming-apis-python/google_books.py @@ -19,8 +19,8 @@ # You can fetch these and other attributes from the book using # the specific volume - title = volume["title"] - published = volume["publishedDate"] - description = volume["description"] + title = volume.get("title", "") + published = volume.get("publishedDate", "") + description = volume.get("description", "") - print("%s (%s) | %s" % (title, published, description)) + print(f"{title} ({published})\n{description}\n") diff --git a/consuming-apis-python/requirements.txt b/consuming-apis-python/requirements.txt new file mode 100644 index 0000000000..d80d9fc2a3 --- /dev/null +++ b/consuming-apis-python/requirements.txt @@ -0,0 +1 @@ +requests==2.32.3