Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
adback03 committed Feb 16, 2016
0 parents commit cd02621
Show file tree
Hide file tree
Showing 24 changed files with 785 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .coveragerc
@@ -0,0 +1,22 @@
# This file is part of mtgsdk.
# https://github.com/MagicTheGathering/mtg-sdk-python

# Licensed under the MIT license:
# http://www.opensource.org/licenses/MIT-license
# Copyright (c) 2016, Andrew Backes <backes.andrew@gmail.com>

[run]
omit =
*tests.py
branch = True
source =
mtgsdk

[report]
exclude_lines =
pragma: no cover
def __repr__
raise NotImplementedError
if __name__ == .__main__.:
from urllib.parse import parse_qs
except ImportError:
63 changes: 63 additions & 0 deletions .gitignore
@@ -0,0 +1,63 @@
# This file is part of mtgsdk.
# https://github.com/MagicTheGathering/mtg-sdk-python

# Licensed under the MIT license:
# http://www.opensource.org/licenses/MIT-license
# Copyright (c) 2016, Andrew Backes <backes.andrew@gmail.com>

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

# C extensions
*.so

# Distribution / packaging
.Python
env/
bin/
build/
develop-eggs/
dist/
eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.cache
nosetests.xml
coverage.xml

# Translations
*.mo

# Mr Developer
.mr.developer.cfg
.project
.pydevproject

# Rope
.ropeproject

# Django stuff:
*.log
*.pot

# Sphinx documentation
docs/_build/

# Fixtures
fixtures/*.yaml
20 changes: 20 additions & 0 deletions .travis.yml
@@ -0,0 +1,20 @@
# This file is part of mtgsdk.
# https://github.com/MagicTheGathering/mtg-sdk-python

# Licensed under the MIT license:
# http://www.opensource.org/licenses/MIT-license
# Copyright (c) 2016, Andrew Backes <backes.andrew@gmail.com>

language: python

python:
- "3.4"
- "pypy"

install:
# install python requirements
- make setup

script:
# run tests
- make test
34 changes: 34 additions & 0 deletions Makefile
@@ -0,0 +1,34 @@
# This file is part of mtgsdk.
# https://github.com/MagicTheGathering/mtg-sdk-python

# Licensed under the MIT license:
# http://www.opensource.org/licenses/MIT-license
# Copyright (c) 2016, Andrew Backes <backes.andrew@gmail.com>

# lists all available targets
list:
@sh -c "$(MAKE) -p no_targets__ | awk -F':' '/^[a-zA-Z0-9][^\$$#\/\\t=]*:([^=]|$$)/ {split(\$$1,A,/ /);for(i in A)print A[i]}' | grep -v '__\$$' | grep -v 'make\[1\]' | grep -v 'Makefile' | sort"
# required for list
no_targets__:

# install all dependencies (do not forget to create a virtualenv first)
setup:
@pip3 install -U -e .\[tests\]

# test your application (tests in the tests/ directory)
test: unit

unit:
@coverage run --branch `which nosetests` -vv --with-yanc -s tests/
@coverage report -m --fail-under=80

# show coverage in html format
coverage-html: unit
@coverage html

# run tests against all supported python versions
tox:
@tox

#docs:
#@cd mtgsdk/docs && make html && open _build/html/index.html
19 changes: 19 additions & 0 deletions mtgsdk/__init__.py
@@ -0,0 +1,19 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# This file is part of mtgsdk.
# https://github.com/MagicTheGathering/mtg-sdk-python

# Licensed under the MIT license:
# http://www.opensource.org/licenses/MIT-license
# Copyright (c) 2016, Andrew Backes <backes.andrew@gmail.com>

from mtgsdk.config import __version__, __pypi_packagename__, __github_username__, __github_reponame__, __endpoint__
from mtgsdk.card import Card
from mtgsdk.set import Set
from mtgsdk.supertype import Supertype
from mtgsdk.subtype import Subtype
from mtgsdk.type import Type
from mtgsdk.changelog import Changelog
from mtgsdk.restclient import RestClient
from mtgsdk.querybuilder import QueryBuilder
66 changes: 66 additions & 0 deletions mtgsdk/card.py
@@ -0,0 +1,66 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# This file is part of mtgsdk.
# https://github.com/MagicTheGathering/mtg-sdk-python

# Licensed under the MIT license:
# http://www.opensource.org/licenses/MIT-license
# Copyright (c) 2016, Andrew Backes <backes.andrew@gmail.com>

import json
from mtgsdk.querybuilder import QueryBuilder

class Card(object):
RESOURCE = 'cards'

def __init__(self, response_dict={}):
self.name = response_dict.get('name')
self.layout = response_dict.get('layout')
self.mana_cost = response_dict.get('manaCost')
self.cmc = response_dict.get('cmc')
self.colors = response_dict.get('colors')
self.names = response_dict.get('names')
self.type = response_dict.get('type')
self.supertypes = response_dict.get('supertypes')
self.subtypes = response_dict.get('subtypes')
self.types = response_dict.get('types')
self.rarity = response_dict.get('rarity')
self.text = response_dict.get('text')
self.flavor = response_dict.get('flavor')
self.artist = response_dict.get('artist')
self.number = response_dict.get('number')
self.power = response_dict.get('power')
self.toughness = response_dict.get('toughness')
self.loyalty = response_dict.get('loyalty')
self.multiverse_id = response_dict.get('multiverseid')
self.variations = response_dict.get('variations')
self.watermark = response_dict.get('watermark')
self.border = response_dict.get('border')
self.timeshifted = response_dict.get('timeshifted')
self.hand = response_dict.get('hand')
self.life = response_dict.get('life')
self.release_date = response_dict.get('releaseDate')
self.starter = response_dict.get('starter')
self.printings = response_dict.get('printings')
self.original_text = response_dict.get('originalText')
self.original_type = response_dict.get('originalType')
self.source = response_dict.get('source')
self.image_url = response_dict.get('imageUrl')
self.set = response_dict.get('set')
self.id = response_dict.get('id')
self.legalities = response_dict.get('legalities')
self.rulings = response_dict.get('rulings')
self.foreign_names = response_dict.get('foreignNames')

@staticmethod
def find(id):
return QueryBuilder(Card).find(id)

@staticmethod
def where(**kwargs):
return QueryBuilder(Card).where(**kwargs)

@staticmethod
def all():
return QueryBuilder(Card).all()
24 changes: 24 additions & 0 deletions mtgsdk/changelog.py
@@ -0,0 +1,24 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# This file is part of mtgsdk.
# https://github.com/MagicTheGathering/mtg-sdk-python

# Licensed under the MIT license:
# http://www.opensource.org/licenses/MIT-license
# Copyright (c) 2016, Andrew Backes <backes.andrew@gmail.com>

from mtgsdk.querybuilder import QueryBuilder

class Changelog(object):
RESOURCE = 'changelogs'

def __init__(self, response_dict={}):
self.id = response_dict.get('id')
self.version = response_dict.get('version')
self.details = response_dict.get('details')
self.release_date = response_dict.get('releaseDate')

@staticmethod
def all():
return QueryBuilder(Changelog).all()
15 changes: 15 additions & 0 deletions mtgsdk/config.py
@@ -0,0 +1,15 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# This file is part of mtgsdk.
# https://github.com/MagicTheGathering/mtg-sdk-python

# Licensed under the MIT license:
# http://www.opensource.org/licenses/MIT-license
# Copyright (c) 2016, Andrew Backes <backes.andrew@gmail.com>

__version__ = "1.0.0"
__pypi_packagename__ = "mtgsdk"
__github_username__ = "MagicTheGathering"
__github_reponame__ = "mtg-sdk-python"
__endpoint__ = "https://api.magicthegathering.io/v1"
92 changes: 92 additions & 0 deletions mtgsdk/querybuilder.py
@@ -0,0 +1,92 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# This file is part of mtgsdk.
# https://github.com/MagicTheGathering/mtg-sdk-python

# Licensed under the MIT license:
# http://www.opensource.org/licenses/MIT-license
# Copyright (c) 2016, Andrew Backes <backes.andrew@gmail.com>

import json
from mtgsdk.restclient import RestClient
from mtgsdk.config import __endpoint__

class QueryBuilder(object):
def __init__(self, type):
self.params = {}
self.type = type

def find(self, id):
"""Get a resource by its id
Args:
id (string): Resource id
Returns:
object: Instance of the resource type
"""
url = "{}/{}/{}".format(__endpoint__, self.type.RESOURCE, id)
response = RestClient.get(url)[self.type.RESOURCE[:-1]]
return self.type(response)

def find_many(self, url, type, resource):
"""Get a list of resources
Args:
url (string): URL to invoke
type (class): Class type
resource (string): The REST Resource
Returns:
list of object: List of resource instances
"""
list = []
response = RestClient.get(url)[resource]
if (len(response) > 0):
for item in response:
list.append(type(item))

return list

def where(self, **kwargs):
"""Adds a parameter to the dictionary of query parameters
Args:
**kwargs: Arbitrary keyword arguments.
Returns:
QueryBuilder: Instance of the QueryBuilder
"""
for key, value in kwargs.items():
self.params[key] = value

return self

def all(self):
"""Get all resources, automatically paging through data
Returns:
list of object: List of resource objects
"""
list = []
page = 0
url = "{}/{}".format(__endpoint__, self.type.RESOURCE)

while(True):
page = page + 1
self.where(page=page)
response = RestClient.get(url, self.params)[self.type.RESOURCE]
if (len(response) > 0):
for item in response:
list.append(self.type(item))
else:
break

return list

def array(self):
"""Get all resources and return the result as an array
Returns:
array of str: Array of resources
"""
url = "{}/{}".format(__endpoint__, self.type.RESOURCE)
return RestClient.get(url, self.params)[self.type.RESOURCE]

0 comments on commit cd02621

Please sign in to comment.