Skip to content

Commit

Permalink
Merge pull request #12 from PokeAPI/beckett
Browse files Browse the repository at this point in the history
Uses Beckett API Client Framework
  • Loading branch information
phalt committed Jun 11, 2016
2 parents 37870ad + 96c3094 commit fb2a94e
Show file tree
Hide file tree
Showing 14 changed files with 417 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*.so

# Packages
*.egg
*.egg*
*.cache
*.egg-info
dist
build
Expand Down
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ language: python

python:
- "2.7"
- "pypy"

# command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors
install: pip install -r requirements.txt

# command to run tests, e.g. python setup.py test
script: python setup.py test
script: py.test test
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
History
-------

0.2.0 (2016-06-11)
++++++++++++++++++

* Beckett API Client framework added

0.1.2 (2014-1-3)
++++++++++++++++++

Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2014, Paul Hallett
Copyright (c) 2016, Paul Hallett
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ lint:
flake8 pykemon tests

test:
python setup.py test
py.test tests/

test-all:
tox
Expand Down
9 changes: 3 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,9 @@ Even simpler:
.. code-block:: python
>>> import pykemon
>>> pykemon.get(pokemon='bulbasaur')
<Pokemon - Bulbasaur>
>>> p = pykemon.get(pokemon_id=1)
<Pokemon - Bulbasaur>
>>> pykemon.get(move_id=15)
<Move - cut>
>>> client = pykemon.V1Client()
>>> p = client.get_pokemon(uid=1)
[<Pokemon - Bulbasaur>]
Features
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
contain the root `toctree` directive.
Welcome to Pykemon's documentation!
======================================
===================================

Contents:

Expand Down
28 changes: 27 additions & 1 deletion docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,33 @@ To use Pykemon in a project::

>>> import pykemon

=======
New API
=======

Since version 0.2.0, Pykemon now works with `Beckett <https://phalt.github.io/beckett>`_, an easy to use API Client Framework::

>>> client = pykemon.V1Client()
>>> bulba = client.get_pokemon(uid=1)[0]
<Pokemon | Bulbasaur>
>>> bulba.name
Bulbasaur

The following methods work with this client and all take a `uid` parameter:

* get_pokemon
* get_move
* get_sprite
* get_ability
* get_game
* get_type
* get_egg


================
Version 0.1* API
================

Then you can start grabbing stuff from the API::

>>> pykemon.get(pokemon='mew')
Expand Down Expand Up @@ -57,4 +84,3 @@ The Pokemon resource can also be requested using the name:
<Pokemon - Rotom>

Make sure you use lower case strings!

8 changes: 4 additions & 4 deletions pykemon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

__author__ = 'Paul Hallett'
__email__ = 'hello@phalt.co'
__version__ = '0.1.2'
__copyright__ = 'Copyright Paul Hallett 2014'
__version__ = '0.2.0'
__copyright__ = 'Copyright Paul Hallett 2016'
__license__ = 'BSD'

from api import get
from exceptions import ResourceNotFoundError
from api import get, V1Client # NOQA
from exceptions import ResourceNotFoundError # NOQA


"""
Expand Down
24 changes: 24 additions & 0 deletions pykemon/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@
User interaction with this package is done through this file.
"""

from beckett.clients import BaseClient

from request import CHOICES
from request import make_request

from .resources import (
MoveResource, PokemonResource, TypeResource, AbilityResource, EggResource,
DescriptionResource, SpriteResource, GameResource
)


def get(**kwargs):
"""
Expand Down Expand Up @@ -36,3 +43,20 @@ def get(**kwargs):

else:
raise ValueError('An invalid argument was passed')


class V1Client(BaseClient):

class Meta(BaseClient.Meta):
name = 'pykemon-v1-client'
base_url = 'http://pokeapi.co/api/v1'
resources = (
MoveResource,
PokemonResource,
TypeResource,
AbilityResource,
EggResource,
DescriptionResource,
SpriteResource,
GameResource,
)
219 changes: 219 additions & 0 deletions pykemon/resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
# -*- coding: utf-8 -*-

from beckett.resources import BaseResource


class PokemonResource(BaseResource):
class Meta(BaseResource.Meta):
name = 'Pokemon'
resource_name = 'pokemon'
identifier = 'id'
methods = (
'get',
)
attributes = (
'created',
'modified',
'national_id',
'abilities',
'egg_groups',
'evolutions',
'descriptions',
'moves',
'types',
'catch_rate',
'species',
'hp',
'attack',
'defense',
'name',
'sp_atk',
'sp_def',
'speed',
'total',
'egg_cycles',
'ev_yield',
'exp',
'growth_rate',
'height',
'weight',
'happiness',
'male_female_ratio',
'sprites',
)

@staticmethod
def get_single_resource_url(url, uid, **kwargs):
# Needs a slash on the end!
return '{}/{}/'.format(url, uid)


class MoveResource(BaseResource):

class Meta(BaseResource.Meta):
name = 'Move'
resource_name = 'move'
identifier = 'id'
methods = (
'get',
)
attributes = (
'created',
'modified',
'id',
'accuracy',
'category',
'power',
'pp',
'name',
)

@staticmethod
def get_single_resource_url(url, uid, **kwargs):
# Needs a slash on the end!
return '{}/{}/'.format(url, uid)


class TypeResource(BaseResource):

class Meta(BaseResource.Meta):
name = 'Type'
resource_name = 'type'
identifier = 'id'
methods = (
'get',
)
attributes = (
'created',
'modified',
'id',
'name',
'ineffective',
'resistance',
'super_effective',
'weakness',
)

@staticmethod
def get_single_resource_url(url, uid, **kwargs):
# Needs a slash on the end!
return '{}/{}/'.format(url, uid)


class AbilityResource(BaseResource):

class Meta(BaseResource.Meta):
name = 'Ability'
resource_name = 'ability'
identifier = 'id'
methods = (
'get',
)
attributes = (
'created',
'modified',
'id',
'name',
'description',
)

@staticmethod
def get_single_resource_url(url, uid, **kwargs):
# Needs a slash on the end!
return '{}/{}/'.format(url, uid)


class EggResource(BaseResource):

class Meta(BaseResource.Meta):
name = 'Egg'
resource_name = 'egg'
identifier = 'id'
methods = (
'get',
)
attributes = (
'created',
'modified',
'id',
'name',
'pokemon',
)

@staticmethod
def get_single_resource_url(url, uid, **kwargs):
# Needs a slash on the end!
return '{}/{}/'.format(url, uid)


class DescriptionResource(BaseResource):

class Meta(BaseResource.Meta):
name = 'Description'
resource_name = 'description'
identifier = 'id'
methods = (
'get',
)
attributes = (
'created',
'modified',
'id',
'name',
'description',
'pokemon',
'games',
)

@staticmethod
def get_single_resource_url(url, uid, **kwargs):
# Needs a slash on the end!
return '{}/{}/'.format(url, uid)


class SpriteResource(BaseResource):

class Meta(BaseResource.Meta):
name = 'Sprite'
resource_name = 'sprite'
identifier = 'id'
methods = (
'get',
)
attributes = (
'created',
'modified',
'id',
'name',
'pokemon',
'image',
)

@staticmethod
def get_single_resource_url(url, uid, **kwargs):
# Needs a slash on the end!
return '{}/{}/'.format(url, uid)


class GameResource(BaseResource):

class Meta(BaseResource.Meta):
name = 'Game'
resource_name = 'game'
identifier = 'id'
methods = (
'get',
)
attributes = (
'created',
'modified',
'id',
'name',
'generation',
'release_year',
)

@staticmethod
def get_single_resource_url(url, uid, **kwargs):
# Needs a slash on the end!
return '{}/{}/'.format(url, uid)

0 comments on commit fb2a94e

Please sign in to comment.