Skip to main content

A Python SDK for interacting with PokeAPI

Project description

PokeAPI SDK for Python

The PokeAPI SDK for Python provides a scoped demo of using Python to create an SDK for a REST api.

Preamble

This is a small demo of a Pokeapi SDK written in python which covers the Pokemon, Nature and Stat endpoints of PokeAPI only.

The main packages/tools used are:

  • requests - to ensure code readability and maintainability when interacting with the PokeAPI
  • pytest - for testing
  • GitHub Actions - for automated testing and deployment
  • Pypi - for hosting the built pip package

The first versions I came up with for this project were returning JSON objects which, while easy to work with in Python, did not feel native enough.

I therefore restructured the project to use classes for a better object-orientated experience. Code generation via ChatGPT was helpful for filling gaps, particularly as PokeAPI has a lot of nested types in the responses to the pokemon endpoint - writing this out manually would have cost a lot of time and would be prone to errors.

While the main 3 methods mirror PokeAPI, I decided to add a fourth method get_pokemon_full_details which helps to amalgamate the other methods together. As previously mentioned, PokeAPI has a lot of nested types, but it does not retrieve the full details on the request for a Pokemon. This method therefore also resolves any Nature and Stat information and provides this alongside the normal Pokemon object.

The basic premise of the way this repo is set up is that on each push, the package is tested then published, via GitHub Actions, to Pypi, ready to be installed via Pip. Of course, as this is only a demo it simply runs this in a dumb fashion on each push (in the real world this would be configured differently).

As a little stretch goal, I did get ChatGPT to generate an OpenAPI spec based on my SDK and than ran this through SpeakEasy. The result is here, however I have not had chance to test this yet! It would be interesting to see how this compares to my "source" SDK.

Installation

The SDK demo can be installed into your Python environment via Pip:

pip install pokeapi-sdk-demo

Full details can be found on the Pypi project page.

Usage

After installation, you can import the package into your project with import pokeapi_sdk at the top of your file.

You can then instantiate an SDK client with client = pokeapi_sdk.PokeAPISDK(). See below examples for how to get started with the SDK.

Note that the SDK demo only covers the Pokemon, Nature and Stat endpoints of PokeAPI.

An additional method, get_pokemon_full_details(), has been provided which combines nested retrieval of details. For example, if a Pokemon has a Nature, rather than just returning the Pokeapi URL for that Nature, this method will resolve the Nature into a Nature object.

Examples

Retrieving Pokemon details from the ID number:

pokemon = client.get_pokemon(1)
print (pokemon.name)

>>> bulbasaur

Retrieving Pokemon ability from the name:

pokemon = client.get_pokemon('bulbasaur')
print (pokemon.abilities[0].ability.name)

>>> overgrow

Retrieving Nature details from an ID number:

nature = client.get_nature(1)
print (nature.name)

>>> 'hardy'

Retrieving Stat information from name:

stat = client.get_stat('hp')
stat.game_index

>>> 1

Testing

Testing is done via Pytest, which needs to be installed prior to running pip install pytest or via the requirements file pip install -r requirements.txt.

Then run pytest to run all the tests in the test directory. Sample output:

========================= test session starts ==========================
platform darwin -- Python 3.10.13, pytest-7.1.3, pluggy-1.0.0
rootdir: /Users/adam/GitHub/pokeapisdkdemo
plugins: testinfra-6.8.0
collected 8 items                                                      

tests/nature_test.py ..                                          [ 25%]
tests/pokemon_test.py ....                                       [ 75%]
tests/stat_test.py ..                                            [100%]

========================== 8 passed in 1.72s ===========================

Testing is also automated via GitHub Actions on each push to the repo (example pipeline)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pokeapi_sdk_demo-1.0.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

pokeapi_sdk_demo-1.0.1-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file pokeapi_sdk_demo-1.0.1.tar.gz.

File metadata

  • Download URL: pokeapi_sdk_demo-1.0.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for pokeapi_sdk_demo-1.0.1.tar.gz
Algorithm Hash digest
SHA256 a21d236c7e4b655cf53531d84115f048169f96877baccc72361dc0ae8c7598e7
MD5 eda1b36715d7a00d6dd7533c9fe1fe05
BLAKE2b-256 561a92e6c87e8e4e4d3eaa3807679da3252f3a6ae97432e75e004009c456c7bf

See more details on using hashes here.

File details

Details for the file pokeapi_sdk_demo-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pokeapi_sdk_demo-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4b01f3d7c4b98e238faa03e3f00467983f34bf7db15e9820f310def1369a036d
MD5 f57157d399489880925c2b625b7f7af8
BLAKE2b-256 3957abf2374505d1734764b2d4e292d52b29148fb385f473c9cd0bca4a490b86

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page