BigW Consortium Gitlab

Commit 2dc099eb by Dan McKinley

keys from configuration file, with tests

parent 54f47767
......@@ -64,8 +64,11 @@ on Code as Craft.
## Version History
### Version 0.2
* 05-30-2010 - Added support for the v2 API, configuration, and tests.
### Version 0.1
* 05-24-2010 - Initial release
\ No newline at end of file
### Version 0.2 - in progress
* Added support for the v2 API
* Added local configuration (~/.etsy) to eliminate cutting & pasting of api keys.
* Added a test suite.
* Added module to PyPI.
### Version 0.1 - 05-24-2010
Initial release
\ No newline at end of file
......@@ -3,33 +3,53 @@ from contextlib import closing
from simplejson.decoder import JSONDecoder
import urllib2
from urllib import urlencode
from ConfigParser import ConfigParser
import os
class API(object):
def __init__(self, api_key):
def __init__(self, api_key='', key_file=None):
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
if not getattr(self, 'api_version', None):
raise AssertionError('API object should define api_version')
if api_key:
self.api_key = api_key
else:
self.api_key = self._read_key(key_file)
d = JSONDecoder()
self.decode = d.decode
for method in self._get_method_table():
self.create_method(**method)
self._create_method(**method)
def _get_method_table(self):
return self.get('/')
return self._get('/')
def _read_key(self, key_file):
key_file = key_file or os.path.expanduser('~/.etsy/keys')
if not os.path.isfile(key_file):
raise AssertionError(
"The key file '%s' does not exist. Create a key file or "
'pass an API key explicitly.' % key_file)
gs = {}
execfile(key_file, gs)
return gs[self.api_version]
def create_method(self, name, description, uri, **kw):
def _create_method(self, name, description, uri, **kw):
def method(**kwargs):
return self.get(uri, **kwargs)
return self._get(uri, **kwargs)
method.__name__ = name
method.__doc__ = description
setattr(self, name, method)
......@@ -40,7 +60,7 @@ class API(object):
return f.read()
def get(self, url, **kwargs):
def _get(self, url, **kwargs):
for k, v in kwargs.items():
arg = '{%s}' % k
if arg in url:
......
......@@ -3,12 +3,13 @@ from unittest import TestCase
from etsy._core import API
from cgi import parse_qs
from urlparse import urlparse
import os
class MockAPI(API):
api_url = 'http://host'
api_version = 'v1'
def _get_method_table(self):
return [{'name': 'testMethod',
......@@ -93,3 +94,37 @@ class CoreTests(TestCase):
self.fail('should have failed')
def test_api_should_define_version(self):
class Foo(API):
api_url = 'http://host'
try:
Foo()
except AssertionError, e:
self.assertEqual(e.message, 'API object should define api_version')
else:
self.fail('should have failed')
def test_key_file_does_not_exist(self):
try:
MockAPI(key_file='this does not exist')
except AssertionError, e:
self.assertTrue("'this does not exist' does not exist"
in e.message)
else:
self.fail('should have failed')
def test_reading_api_key(self):
with open('testkeys', 'w') as f:
f.write("v1 = 'abcdef'")
try:
self.assertEqual(MockAPI(key_file='testkeys').api_key, 'abcdef')
finally:
os.unlink('testkeys')
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