Skip to content

Commit 0ba8485

Browse files
committed
Add basic Python library
1 parent 2b5609a commit 0ba8485

File tree

6 files changed

+167
-0
lines changed

6 files changed

+167
-0
lines changed

libraries/python/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
# Ignore implementation testing files
2+
main.py
3+
requirements*
4+
5+
#
16
# Sourced from: https://github.com/github/gitignore/blob/main/Python.gitignore
7+
#
28

39
# Byte-compiled / optimized / DLL files
410
__pycache__/

libraries/python/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Python Library for IGDB
2+
3+
A Python library for interacting with IGDB - a database of video game data. This library assists with authentication via Twitch and making requests to IGDB.
4+
5+
## Usage
6+
7+
First you will need to acquire credentials from the Twitch API in order to call IGDB. Once you have a valid API token, you can call IGDB for data.
8+
9+
### Examples
10+
11+
Get an API token from Twitch and prepare to build an IGDB request.
12+
print("Getting secrets from file/env")
13+
14+
```python
15+
# Setup your imports as needed
16+
from igdb_api import igdb
17+
from twitch import twitch_oauth
18+
import json
19+
20+
#...
21+
22+
def test_igdb():
23+
# Retrieve your secrets from a secure environment!
24+
client_id = 'CLIENT_ID'
25+
client_secret = 'CLIENT_SECRET'
26+
27+
# Utilize Twitch's OAuth API to acquire an access token
28+
twitch = twitch_oauth.TwitchOauth()
29+
token_response = twitch.request_oauth_token(client_id, client_secret)
30+
oauth_token = token_response["access_token"]
31+
32+
# Prepare to make a request to IGDB using your client ID and newly-acquired access token
33+
api = igdb.IGDB(client_id, oauth_token)
34+
35+
# Get the name and release date of a single game returned from the API
36+
endpoint = "games"
37+
query = "fields name, release_dates; limit 1;"
38+
39+
# Make the API request -> returns a JSON object
40+
api_response = api.make_request(endpoint, query)
41+
print(api_response)
42+
```

libraries/python/igdb_api/__init__.py

Whitespace-only changes.

libraries/python/igdb_api/igdb.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Note that IGDB provides their own Python implementation of this library,
2+
# so my implementation is largely based on theirs: https://github.com/twitchtv/igdb-api-python/blob/master/src/igdb/wrapper.py
3+
4+
from requests import post
5+
6+
API_ROOT = "https://api.igdb.com"
7+
API_VERSION = "v4"
8+
9+
class IGDB:
10+
def __init__(self, client_id: str, api_token: str):
11+
self.client_id = client_id
12+
self.api_token = api_token
13+
14+
15+
def make_request(self, endpoint: str, query: str) -> bytes:
16+
print("Attempting to make request to IGDB")
17+
endpoint_url = IGDB.build_endpoint_url(endpoint)
18+
request_params = self.build_request_params(query)
19+
20+
print(f"url={endpoint_url} | params={request_params}")
21+
response = post(endpoint_url, **request_params)
22+
response.raise_for_status()
23+
24+
print("Successfully retrieved response from IGDB!")
25+
26+
return response.json()
27+
28+
29+
def build_request_params(self, query: str) -> dict:
30+
request = {}
31+
request['headers'] = self.build_headers_for_request()
32+
request['data'] = query
33+
return request
34+
35+
36+
@staticmethod
37+
def build_endpoint_url(endpoint: str) -> str:
38+
base_url = IGDB.get_base_url()
39+
endpoint_url = "/".join([base_url, endpoint])
40+
return endpoint_url
41+
42+
43+
@staticmethod
44+
def get_base_url() -> str:
45+
base_url = "/".join([API_ROOT, API_VERSION])
46+
return base_url
47+
48+
49+
def build_headers_for_request(self) -> dict:
50+
headers = {
51+
"Client-ID": self.client_id,
52+
"Authorization": f"Bearer {self.api_token}",
53+
"Accept": "application/json",
54+
}
55+
return headers

libraries/python/twitch/__init__.py

Whitespace-only changes.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
from requests import post, get
3+
4+
TWITCH_API_ROOT = "https://id.twitch.tv/"
5+
TWITCH_API_OAUTH_TOKEN = "oauth2/token"
6+
TWITCH_API_OAUTH_VALIDATE = "oauth2/validate"
7+
8+
class TwitchOauth:
9+
10+
def request_oauth_token(self, client_id: str, client_secret: str) -> dict:
11+
print("Attempting to retrieve a new access token")
12+
params = {
13+
"client_id": client_id,
14+
"client_secret": client_secret,
15+
"grant_type": "client_credentials",
16+
}
17+
18+
headers = {
19+
"Content-Type": "application/json; charset=UTF-8",
20+
}
21+
22+
request_params = {
23+
"headers": headers,
24+
"params": params,
25+
}
26+
27+
token_url = TwitchOauth.build_token_url()
28+
response = post(token_url, **request_params)
29+
response.raise_for_status()
30+
31+
print("Successfully retrieved new access token!")
32+
response_data = response.json()
33+
return response_data
34+
35+
36+
def validate_access_token(self, token) -> dict:
37+
print("Attempting to validate access token")
38+
headers = {
39+
"Authorization": f"Oauth {token}",
40+
}
41+
42+
request_params = {
43+
"headers": headers,
44+
}
45+
46+
token_url = TwitchOauth.build_token_url()
47+
response = get(token_url, **request_params)
48+
response.raise_for_status()
49+
50+
print("Validated access token")
51+
response_data = response.json()
52+
return response_data
53+
54+
55+
@staticmethod
56+
def build_token_url() -> str:
57+
oauth_token_url = "".join([TWITCH_API_ROOT, TWITCH_API_OAUTH_TOKEN])
58+
return oauth_token_url
59+
60+
61+
@staticmethod
62+
def build_validate_url() -> str:
63+
validate_url = "".join([TWITCH_API_ROOT, TWITCH_API_OAUTH_VALIDATE])
64+
return validate_url

0 commit comments

Comments
 (0)