Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions consuming-apis-python/README.md
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 3 additions & 3 deletions consuming-apis-python/giphy_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
# 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.
response = requests.get(endpoint, params=params).json()
for gif in response["data"]:
title = gif["title"]
url = gif["url"]
print("%s | %s" % (title, url))
print(f"{title} | {url}")
4 changes: 2 additions & 2 deletions consuming-apis-python/giphy_trending.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")
12 changes: 5 additions & 7 deletions consuming-apis-python/github.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions consuming-apis-python/google_books.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
1 change: 1 addition & 0 deletions consuming-apis-python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests==2.32.3
Loading