Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass HTTP client instances through when creating children #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion remoteobjects/dataobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,11 @@ def to_dict(self):
return data

@classmethod
def from_dict(cls, data):
def from_dict(cls, data, client=None):
"""Decodes a dictionary into a new `DataObject` instance."""
self = cls()
if client:
self._http = client
self.update_from_dict(data)
return self

Expand Down
22 changes: 11 additions & 11 deletions remoteobjects/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def __get__(self, obj, cls):
else:
value = self.default
else:
value = self.decode(value)
value = self.decode(value, client=getattr(obj, "_http", None))
# Store the value so we need decode it only once.
obj.__dict__[self.attrname] = value

Expand All @@ -167,7 +167,7 @@ def __delete__(self, obj):
except KeyError:
pass

def decode(self, value):
def decode(self, value, client=None):
"""Decodes a dictionary value into a `DataObject` attribute value.

This implementation returns the `value` parameter unchanged. This is
Expand Down Expand Up @@ -239,7 +239,7 @@ def __set__(self, obj, value):
raise ValueError('Value %r is not expected value %r'
% (value, self.value))

def decode(self, value):
def decode(self, value, client=None):
if value != self.value:
raise ValueError('Value %r is not expected value %r'
% (value, self.value))
Expand Down Expand Up @@ -276,10 +276,10 @@ def install(self, attrname, cls):
# Make sure our content field knows its owner too.
self.fld.install(attrname, cls)

def decode(self, value):
def decode(self, value, client=None):
"""Decodes the dictionary value (a list of dictionary values) into a
`DataObject` attribute (a list of `DataObject` attribute values)."""
return [self.fld.decode(v) for v in value]
return [self.fld.decode(v, client=client) for v in value]

def encode(self, value):
"""Encodes a `DataObject` attribute (a list of `DataObject` attribute
Expand All @@ -296,11 +296,11 @@ class Dict(List):

"""

def decode(self, value):
def decode(self, value, client=None):
"""Decodes the dictionary value (a dictionary with dictionary values
for values) into a `DataObject` attribute (a dictionary with
`DataObject` attributes for values)."""
return dict((k, self.fld.decode(v)) for k, v in value.iteritems())
return dict((k, self.fld.decode(v, client=client)) for k, v in value.iteritems())

def encode(self, value):
"""Encodes a `DataObject` attribute (a dictionary with decoded
Expand Down Expand Up @@ -346,14 +346,14 @@ class is the leafmost `DataObject` subclass declared with that name.
super(Object, self).__init__(**kwargs)
self.cls = cls

def decode(self, value):
def decode(self, value, client=None):
"""Decodes the dictionary value into an instance of the `DataObject`
class the field references."""
if value is None:
if callable(self.default):
return self.default()
return self.default
return self.cls.from_dict(value)
return self.cls.from_dict(value, client=client)

def encode(self, value):
"""Encodes an instance of the field's DataObject class into its
Expand Down Expand Up @@ -387,7 +387,7 @@ def __init__(self, dateformat=None, **kwargs):
if dateformat is not None:
self.dateformat = dateformat

def decode(self, value):
def decode(self, value, client=None):
"""Decodes a timestamp string into a `DataObject` attribute (a Python
`datetime` instance).

Expand Down Expand Up @@ -475,4 +475,4 @@ def __get__(self, instance, owner):
if instance._location is None:
raise AttributeError('Cannot find URL of %s relative to URL-less %s' % (self.cls.__name__, owner.__name__))
newurl = urlparse.urljoin(instance._location, self.api_name)
return self.cls.get(newurl)
return self.cls.get(newurl, http=instance._http)
6 changes: 6 additions & 0 deletions remoteobjects/promise.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ def options(self, http=None, **kwargs):
resp._method = 'OPTIONS'
return resp

def post(self, obj, http=None):
if http is None:
http = self._http

return super(PromiseObject, self).post(obj, http=http)

def __setattr__(self, name, value):
if name is not '_delivered' and not self._delivered and name in self.fields:
self.deliver()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

setup(
name='remoteobjects',
version='1.2.1',
version='1.2.1.post1',
description='an Object RESTational Model',
author='SAY Media Ltd.',
author_email='[email protected]',
Expand Down