BigW Consortium Gitlab

Commit 5cb5597b by Dan McKinley

Refactoring to get core API object. Adding tests for it. README update for…

Refactoring to get core API object. Adding tests for it. README update for cheeseshop. Changing setuptools name. Adding module metadata.
parent cb7f1f6e
*~ *~
*egg-info* *egg-info*
build/ build/
dist/ dist/
\ No newline at end of file *pyc
...@@ -5,14 +5,21 @@ By Dan McKinley - dan@etsy.com - [http://mcfunley.com](http://mcfunley.com) ...@@ -5,14 +5,21 @@ By Dan McKinley - dan@etsy.com - [http://mcfunley.com](http://mcfunley.com)
## Installation ## Installation
After downloading and extracting the tarball, The simplest way to install the module is using
[setuptools](http://pypi.python.org/pypi/setuptools).
<pre>
$ easy_install etsy
</pre>
To install from source, extract the tarball and use the following commands.
<pre> <pre>
$ python setup.py build $ python setup.py build
$ sudo python setup.py install $ sudo python setup.py install
</pre> </pre>
## Example ## Simple Example
To use, first [register for an Etsy developer key](http://developer.etsy.com/). To use, first [register for an Etsy developer key](http://developer.etsy.com/).
Below is an example session. Below is an example session.
...@@ -30,7 +37,13 @@ Type "help", "copyright", "credits" or "license" for more information. ...@@ -30,7 +37,13 @@ Type "help", "copyright", "credits" or "license" for more information.
</pre> </pre>
## Configuration
## Version History ## Version History
### Version 0.2
* 05-30-2010 - Added support for the v2 API, configuration, and tests.
### Version 0.1 ### Version 0.1
* 05-24-2010 - Initial release * 05-24-2010 - Initial release
\ No newline at end of file
from __future__ import with_statement from _v1 import EtsyV1 as Etsy
from contextlib import closing from _v2 import EtsyV2
from simplejson.decoder import JSONDecoder
import urllib2
from urllib import urlencode
class Etsy(object):
api_url = 'http://beta-api.etsy.com/v1'
def __init__(self, api_key):
self.api_key = api_key
d = JSONDecoder()
self.decode = d.decode
for method in self.get('/'):
self.create_method(**method)
def create_method(self, name, description, uri, **kw):
def method(**kwargs):
return self.get(uri, **kwargs)
method.__name__ = name
method.__doc__ = description
setattr(self, name, method)
def get(self, url, **kwargs):
for k, v in kwargs.items():
arg = '{%s}' % k
if arg in url:
url = url.replace(arg, v)
kwargs.update(dict(api_key=self.api_key))
qs = urlencode(kwargs)
url = '%s%s?%s' % (self.api_url, url, qs)
with closing(urllib2.urlopen(url)) as f:
data = f.read()
self.data = self.decode(data)
self.count = self.data['count']
return self.data['results']
__version__ = '0.2'
__author__ = 'Dan McKinley'
__copyright__ = 'Copyright 2010, Etsy Inc.'
__license__ = 'GPL v3'
__email__ = 'dan@etsy.com'
from __future__ import with_statement
from contextlib import closing
from simplejson.decoder import JSONDecoder
import urllib2
from urllib import urlencode
class API(object):
def __init__(self, api_key):
if not getattr(self, 'api_url', None):
raise AssertionError('No api_url configured.')
if self.api_url.endswith('/'):
raise AssertionError('api_url should not end with a slash.')
self.api_key = api_key
d = JSONDecoder()
self.decode = d.decode
for method in self._get_method_table():
self.create_method(**method)
def _get_method_table(self):
return self.get('/')
def create_method(self, name, description, uri, **kw):
def method(**kwargs):
return self.get(uri, **kwargs)
method.__name__ = name
method.__doc__ = description
setattr(self, name, method)
def _get_url(self, url):
with closing(urllib2.urlopen(url)) as f:
return f.read()
def get(self, url, **kwargs):
for k, v in kwargs.items():
arg = '{%s}' % k
if arg in url:
url = url.replace(arg, v)
del kwargs[k]
kwargs.update(dict(api_key=self.api_key))
qs = urlencode(kwargs)
url = '%s%s?%s' % (self.api_url, url, qs)
self.last_url = url
data = self._get_url(url)
self.data = self.decode(data)
self.count = self.data['count']
return self.data['results']
from __future__ import with_statement
from _core import API
class EtsyV1(API):
api_url = 'http://beta-api.etsy.com/v1'
api_version = 'v1'
class EtsyV2(object):
pass
...@@ -6,8 +6,8 @@ this_dir = os.path.realpath(os.path.dirname(__file__)) ...@@ -6,8 +6,8 @@ this_dir = os.path.realpath(os.path.dirname(__file__))
long_description = open(os.path.join(this_dir, 'README.md'), 'r').read() long_description = open(os.path.join(this_dir, 'README.md'), 'r').read()
setup( setup(
name = 'etsy-python', name = 'etsy',
version = '0.1', version = '0.2',
author = 'Dan McKinley', author = 'Dan McKinley',
author_email = 'dan@etsy.com', author_email = 'dan@etsy.com',
description = 'Python access to the Etsy API', description = 'Python access to the Etsy API',
...@@ -15,4 +15,6 @@ setup( ...@@ -15,4 +15,6 @@ setup(
keywords = 'etsy api handmade', keywords = 'etsy api handmade',
packages = ['etsy'], packages = ['etsy'],
long_description = long_description, long_description = long_description,
test_suite = 'test',
setup_requires=['simplejson >= 2.0'],
) )
from __future__ import with_statement
from unittest import TestCase
class ConfigTests(TestCase):
pass
from __future__ import with_statement
from unittest import TestCase
from etsy._core import API
from cgi import parse_qs
from urlparse import urlparse
class MockAPI(API):
api_url = 'http://host'
def _get_method_table(self):
return [{'name': 'testMethod',
'uri': '/test/{test_id}',
'http_method': 'GET',
'params': {
'limit': 'int',
'test_id': 'int',
'offset': 'int',
},
'type': 'int',
'description': 'test method.'}]
def _get_url(self, url):
return '{ "count": 1, "results": [3] }'
class CoreTests(TestCase):
def setUp(self):
self.api = MockAPI('apikey')
def last_query(self):
qs = urlparse(self.api.last_url).query
return parse_qs(qs)
def test_method_created(self):
self.assertTrue('testMethod' in dir(self.api))
def test_url_params(self):
self.api.testMethod(test_id='foo')
self.assertEqual(self.api.last_url,
'http://host/test/foo?api_key=apikey')
def test_count_saved(self):
self.api.testMethod(test_id='foo')
self.assertTrue(self.api.count)
def test_results_returned(self):
x = self.api.testMethod(test_id='foo')
self.assertEquals(x, [3])
def test_query_params(self):
self.api.testMethod(test_id='foo', limit=1)
self.assertEqual(self.last_query(), {
'api_key': ['apikey'],
'limit': ['1'],
})
def test_docstring_set(self):
self.assertEquals(self.api.testMethod.__doc__,
'test method.')
def test_api_url_required(self):
try:
API('')
except AssertionError, e:
self.assertEqual('No api_url configured.', e.message)
else:
self.fail('should have failed')
def test_api_url_cannot_end_with_slash(self):
class Foo(API):
api_url = 'http://host/'
try:
Foo('')
except AssertionError, e:
self.assertEqual('api_url should not end with a slash.',
e.message)
else:
self.fail('should have failed')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment