Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

135 lines (108 sloc) 4.948 kb
import pyactiveresource.connection
from pyactiveresource.activeresource import ActiveResource, ResourceMeta
import shopify.yamlobjects
import shopify.mixins as mixins
import shopify
import threading
import urllib
import urllib2
import urlparse
import sys
# Store the response from the last request in the connection object
class ShopifyConnection(pyactiveresource.connection.Connection):
response = None
def _open(self, *args, **kwargs):
self.response = None
try:
self.response = super(ShopifyConnection, self)._open(*args, **kwargs)
except pyactiveresource.connection.ConnectionError, err:
self.response = err.response
raise
return self.response
# Inherit from pyactiveresource's metaclass in order to use ShopifyConnection
class ShopifyResourceMeta(ResourceMeta):
@property
def connection(cls):
"""HTTP connection for the current thread"""
super_class = cls.__mro__[1]
local = cls._threadlocal
if not getattr(local, 'connection', None):
# Make sure these variables are no longer affected by other threads.
local.user = cls.user
local.password = cls.password
local.site = cls.site
local.timeout = cls.timeout
local.connection = ShopifyConnection(
cls.site, cls.user, cls.password, cls.timeout, cls.format)
return local.connection
def get_user(cls):
return getattr(cls._threadlocal, 'user', ShopifyResource._user)
def set_user(cls, value):
cls._threadlocal.connection = None
ShopifyResource._user = cls._threadlocal.user = value
user = property(get_user, set_user, None,
"The username for HTTP Basic Auth.")
def get_password(cls):
return getattr(cls._threadlocal, 'password', ShopifyResource._password)
def set_password(cls, value):
cls._threadlocal.connection = None
ShopifyResource._password = cls._threadlocal.password = value
password = property(get_password, set_password, None,
"The password for HTTP Basic Auth.")
def get_site(cls):
return getattr(cls._threadlocal, 'site', ShopifyResource._site)
def set_site(cls, value):
cls._threadlocal.connection = None
ShopifyResource._site = cls._threadlocal.site = value
if value is not None:
host = urlparse.urlsplit(value)[1]
auth_info, host = urllib2.splituser(host)
if auth_info:
user, password = urllib2.splitpasswd(auth_info)
if user:
cls.user = urllib.unquote(user)
if password:
cls.password = urllib.unquote(password)
site = property(get_site, set_site, None,
'The base REST site to connect to.')
def get_timeout(cls):
return getattr(cls._threadlocal, 'timeout', ShopifyResource._timeout)
def set_timeout(cls, value):
cls._threadlocal.connection = None
ShopifyResource._timeout = cls._threadlocal.timeout = value
timeout = property(get_timeout, set_timeout, None,
'Socket timeout for HTTP requests')
def get_primary_key(cls):
return cls._primary_key
def set_primary_key(cls, value):
cls._primary_key = value
primary_key = property(get_primary_key, set_primary_key, None,
'Name of attribute that uniquely identies the resource')
class ShopifyResource(ActiveResource, mixins.Countable):
__metaclass__ = ShopifyResourceMeta
_primary_key = "id"
_threadlocal = threading.local()
_headers = { 'User-Agent': 'ShopifyPythonAPI/%s Python/%s' % (shopify.VERSION, sys.version.split(' ', 1)[0]) }
def __init__(self, attributes=None, prefix_options=None):
if attributes is not None and prefix_options is None:
prefix_options, attributes = self.__class__._split_options(attributes)
return super(ShopifyResource, self).__init__(attributes, prefix_options)
def is_new(self):
return not self.id
def _load_attributes_from_response(self, response):
self._update(self.__class__.format.decode(response.body))
def __get_id(self):
if self.klass.primary_key != "id":
return self.attributes.get(self.klass.primary_key)
else:
return super(ShopifyResource, self).id
def __set_id(self, value):
if self.klass.primary_key != "id":
self.attributes[self.klass.primary_key] = value
else:
super(ShopifyResource, self).id = value
id = property(__get_id, __set_id, None, 'Value stored in the primary key')
def save(self):
# See pyactiveresource issue 14: http://code.google.com/p/pyactiveresource/issues/detail?id=14
self.errors.clear()
return super(ShopifyResource, self).save()
Jump to Line
Something went wrong with that request. Please try again.