BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
etsy-python
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
etsy-python
Commits
2dc099eb
Commit
2dc099eb
authored
May 30, 2010
by
Dan McKinley
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
keys from configuration file, with tests
parent
54f47767
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
72 additions
and
14 deletions
+72
-14
README.md
README.md
+9
-6
_core.py
etsy/_core.py
+27
-7
test_core.py
test/test_core.py
+36
-1
No files found.
README.md
View file @
2dc099eb
...
...
@@ -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
etsy/_core.py
View file @
2dc099eb
...
...
@@ -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
:
...
...
test/test_core.py
View file @
2dc099eb
...
...
@@ -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'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment