BigW Consortium Gitlab

Commit cfa60fe2 by Marc Abramowitz

Have basic support for Etsy V2 API working, including OAuth.

I was able to add a listing using createListing.
parent 08ebe94c
from _v1 import EtsyV1, EtsyV1 as Etsy
from _v2 import EtsyV2
from _v2 import EtsyV2, EtsyOAuthClient
__version__ = '0.2.1'
......
......@@ -76,6 +76,20 @@ class TypeChecker(object):
class APIMethod(object):
def __init__(self, api, spec):
"""
Parameters:
api - API object that this method is associated with.
spec - dict with the method specification; e.g.:
{'name': 'createListing', 'uri': '/listings', 'visibility':
'private', 'http_method': 'POST', 'params': {'description':
'text', 'tags': 'array(string)', 'price': 'float', 'title':
'string', 'materials': 'array(string)', 'shipping_template_id':
'int', 'quantity': 'int', 'shop_section_id': 'int'}, 'defaults':
{'materials': None, 'shop_section_id': None}, 'type': 'Listing',
'description': 'Creates a new Listing'}
"""
self.api = api
self.spec = spec
self.type_checker = self.api.type_checker
......@@ -123,7 +137,7 @@ class APIMethod(object):
del kwargs[p]
self.type_checker(self.spec, **kwargs)
return self.api._get(self.uri_format % ps, **kwargs)
return self.api._get(self.spec['http_method'], self.uri_format % ps, **kwargs)
......@@ -256,6 +270,8 @@ class API(object):
for method in ms:
setattr(self, method['name'], APIMethod(self, method))
# self.log('API._get_methods: self._methods = %r' % self._methods)
def etsy_home(self):
return os.path.expanduser('~/.etsy')
......@@ -277,18 +293,26 @@ class API(object):
return gs[self.api_version]
def _get_url(self, url):
def _get_url(self, url, http_method, body):
print "_get_url: url = %r" % url
with closing(urllib2.urlopen(url)) as f:
return f.read()
def _get(self, url, **kwargs):
def _get(self, http_method, url, **kwargs):
kwargs.update(dict(api_key=self.api_key))
qs = urlencode(kwargs)
url = '%s%s?%s' % (self.api_url, url, qs)
if http_method == 'GET':
url = '%s%s?%s' % (self.api_url, url, urlencode(kwargs))
body = None
elif http_method == 'POST':
url = '%s%s' % (self.api_url, url)
body = urlencode(kwargs)
self.last_url = url
data = self._get_url(url)
data = self._get_url(url, http_method, body)
self.log('API._get: http_method = %r, url = %r, data = %r' % (http_method, url, data))
self.data = self.decode(data)
self.count = self.data['count']
......
import urllib
from _core import API, missing
import oauth2 as oauth
try:
from urlparse import parse_qsl
except ImportError:
from cgi import parse_qsl
class EtsyV2(API):
api_url = 'http://openapi.etsy.com/v2/sandbox/public'
api_version = 'v2'
class EtsyV2(object):
def __init__(self, *args, **kwargs):
raise NotImplementedError()
def __init__(self, api_key='', key_file=None, method_cache=missing,
log=None, etsy_oauth_client=None):
if etsy_oauth_client:
self.api_url = 'http://openapi.etsy.com/v2/sandbox/private'
self.etsy_oauth_client = etsy_oauth_client
super(EtsyV2, self).__init__(api_key, key_file, method_cache, log)
def _get_url(self, url, http_method, body):
return self.etsy_oauth_client.do_oauth_request(url, http_method, body)
class EtsyOAuthClient(oauth.Client):
request_token_url = 'http://openapi.etsy.com/v2/sandbox/oauth/request_token'
access_token_url = 'http://openapi.etsy.com/v2/sandbox/oauth/access_token'
signin_url = 'https://www.etsy.com/oauth/signin'
def __init__(self, oauth_consumer_key, oauth_consumer_secret):
consumer = oauth.Consumer(oauth_consumer_key, oauth_consumer_secret)
super(EtsyOAuthClient, self).__init__(consumer)
def get_request_token(self):
resp, content = self.request(self.request_token_url, 'GET')
return self._get_token(content)
def get_signin_url(self):
self.token = self.get_request_token()
return self.signin_url + '?' + \
urllib.urlencode({'oauth_token': self.token.key})
def get_access_token_url(self, oauth_verifier):
return self.access_token_url + '?' + \
urllib.urlencode({'oauth_verifier': oauth_verifier})
def get_access_token(self, oauth_verifier):
resp, content = self.request(self.get_access_token_url(oauth_verifier), 'GET')
return self._get_token(content)
def set_oauth_verifier(self, oauth_verifier):
self.token = self.get_access_token(oauth_verifier)
def do_oauth_request(self, url, http_method, body):
resp, content = self.request(url, http_method, body=body)
return content
def _get_token(self, content):
d = dict(parse_qsl(content))
return oauth.Token(d['oauth_token'], d['oauth_token_secret'])
#!/usr/bin/env python
import os, sys
import oauth2 as oauth
import webbrowser
from etsy import EtsyV2 as Etsy, EtsyOAuthClient
logging_enabled = False
def my_log(msg):
if logging_enabled: print(msg)
def write_config_file(oauth_token):
os.umask(0077)
config_file = file('config.py', 'w')
if config:
config_file.write("oauth_consumer_key = %r\n" % config.oauth_consumer_key)
config_file.write("oauth_consumer_secret = %r\n" % config.oauth_consumer_secret)
if oauth_token:
config_file.write("oauth_token_key = %r\n" % oauth_token.key)
config_file.write("oauth_token_secret = %r\n" % oauth_token.secret)
try:
import config
except ImportError:
config = None
write_config_file(oauth_token=None)
if hasattr(config, 'oauth_consumer_key') and hasattr(config, 'oauth_consumer_secret'):
oauth_client = EtsyOAuthClient(
oauth_consumer_key=config.oauth_consumer_key,
oauth_consumer_secret=config.oauth_consumer_secret)
else:
sys.stderr.write('ERROR: You must set oauth_consumer_key and oauth_consumer_secret in config.py\n')
sys.exit(1)
if hasattr(config, 'oauth_token_key') and hasattr(config, 'oauth_token_secret'):
oauth_client.token = oauth.Token(
key=config.oauth_token_key,
secret=config.oauth_token_secret)
else:
webbrowser.open(oauth_client.get_signin_url())
oauth_client.set_oauth_verifier(raw_input('Enter OAuth verifier: '))
write_config_file(oauth_client.token)
etsy_api = Etsy(etsy_oauth_client=oauth_client, log=my_log)
# print 'oauth access token: (key=%r; secret=%r)' % (oauth_client.token.key, oauth_client.token.secret)
print('findAllShopListingsActive => %r' % etsy_api.findAllShopListingsActive(shop_id=config.user_id, sort_on='created', limit=1))
# print('getListing => %r' % etsy_api.getListing(listing_id=63067548))
print('findAllUserShippingTemplates => %r' % etsy_api.findAllUserShippingTemplates(user_id=config.user_id))
def testCreateListing():
print "Creating listing..."
print etsy_api.createListing(
description='Description of my item',
title='Title of my item',
price=5.95,
# tags='accessories,case,compact,men,mirror,money_clip,card,cigarette,ipod,mp3_player,wallet,pocket,retro,women',
tags='accessories',
materials='case',
shipping_template_id=6509333,
shop_section_id=6866915,
quantity=5
)
# testCreateListing()
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