From 0fd99425b6f2f8c0da71b9832b964014b70ae5ab Mon Sep 17 00:00:00 2001 From: Nanda Scott Date: Thu, 15 Feb 2018 15:13:39 -0500 Subject: [PATCH] Updated rulings to be consistent with cards. --- scrython/rulings/mtgo.py | 2 +- scrython/rulings/multiverse_id.py | 2 +- scrython/rulings/rulings_object.py | 30 ++++++++++++++++-------------- scrython/rulings/scryfall_id.py | 2 +- scrython/rulings/set_code.py | 2 +- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/scrython/rulings/mtgo.py b/scrython/rulings/mtgo.py index 21fc15d..ea256f5 100644 --- a/scrython/rulings/mtgo.py +++ b/scrython/rulings/mtgo.py @@ -5,5 +5,5 @@ class Mtgo(RulingsObject): if kwargs.get('id') is None: raise TypeError('No id provided to search by') - self.url = 'cards/mtgo/{}/rulings'.format(str(kwargs.get('id'))) + self.url = 'cards/mtgo/{}/rulings?'.format(str(kwargs.get('id'))) super(Mtgo, self).__init__(self.url) diff --git a/scrython/rulings/multiverse_id.py b/scrython/rulings/multiverse_id.py index c7de8d2..9dd3b89 100644 --- a/scrython/rulings/multiverse_id.py +++ b/scrython/rulings/multiverse_id.py @@ -5,5 +5,5 @@ class Multiverse(RulingsObject): if kwargs.get('id') is None: raise TypeError('No id provided to search by') - self.url = 'cards/multiverse/{}/rulings'.format(str(kwargs.get('id'))) + self.url = 'cards/multiverse/{}/rulings?'.format(str(kwargs.get('id'))) super(Multiverse, self).__init__(self.url) diff --git a/scrython/rulings/rulings_object.py b/scrython/rulings/rulings_object.py index 95df49c..5781081 100644 --- a/scrython/rulings/rulings_object.py +++ b/scrython/rulings/rulings_object.py @@ -1,29 +1,31 @@ import asyncio import aiohttp +import urllib.parse class RulingsObject(object): def __init__(self, _url, **kwargs): - self._url = 'https://api.scryfall.com/' + _url - loop = asyncio.get_event_loop() - self.session = aiohttp.ClientSession(loop=loop) + self.params = { + 'format': kwargs.get('format', 'json'), 'face': kwargs.get('face', ''), + 'version': kwargs.get('version', ''), 'pretty': kwargs.get('pretty', '') + } - async def getRequest(url, **kwargs): - async with self.session.get(url, **kwargs) as response: + self.encodedParams = urllib.parse.urlencode(self.params) + self._url = 'https://api.scryfall.com/' + _url + "&" + self.encodedParams #Find a fix for this later + + async def getRequest(client, url, **kwargs): + async with client.get(url, **kwargs) as response: return await response.json() - self.scryfallJson = loop.run_until_complete(getRequest( - url = self._url, - params={ - 'format': kwargs.get('format', 'json'), - 'pretty': kwargs.get('pretty', 'false') - })) + async def main(loop): + async with aiohttp.ClientSession(loop=loop) as client: + self.scryfallJson = await getRequest(client, self._url) + + loop = asyncio.get_event_loop() + loop.run_until_complete(main(loop)) if self.scryfallJson['object'] == 'error': - self.session.close() raise Exception(self.scryfallJson['details']) - self.session.close() - def __checkForKey(self, key): try: return self.scryfallJson[key] diff --git a/scrython/rulings/scryfall_id.py b/scrython/rulings/scryfall_id.py index 9bbcf3a..8fbf4f3 100644 --- a/scrython/rulings/scryfall_id.py +++ b/scrython/rulings/scryfall_id.py @@ -5,5 +5,5 @@ class Id(RulingsObject): if kwargs.get('id') is None: raise TypeError('No id provided to search by') - self.url = 'cards/{}/rulings'.format(str(kwargs.get('id'))) + self.url = 'cards/{}/rulings?'.format(str(kwargs.get('id'))) super(Id, self).__init__(self.url) diff --git a/scrython/rulings/set_code.py b/scrython/rulings/set_code.py index c2250e1..d38a1f8 100644 --- a/scrython/rulings/set_code.py +++ b/scrython/rulings/set_code.py @@ -2,5 +2,5 @@ from .rulings_object import RulingsObject class Code(RulingsObject): def __init__(self, code, collector_number): - self.url = 'cards/{}/{}/rulings'.format(code.lower(), str(collector_number)) + self.url = 'cards/{}/{}/rulings?'.format(code.lower(), str(collector_number)) super(Code, self).__init__(self.url)