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

Add ability works with links in Relation ship. #41

Open
wants to merge 3 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
9 changes: 8 additions & 1 deletion jsonapi_requests/orm/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,14 @@ def get_object_key(self):
except KeyError:
raise ObjectKeyError
else:
data = relationship.data
data = None
if (hasattr(relationship, 'data') and relationship.data.as_data() != {}) or relationship.as_data() == {}:
data = relationship.data
elif hasattr(relationship, 'links') and relationship.links != {}:
res = self.instance._options.api.endpoint(path=relationship.links.as_data()['self'])
relationship_data = res.get()
data = relationship_data.data

if data.type is None or data.id is None:
raise ObjectKeyError
else:
Expand Down
4 changes: 2 additions & 2 deletions jsonapi_requests/request_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def request(self, api_path, method, *, object: data.JsonApiObject=None, **kwargs
@property
def retrying(self):
retry_condition = (
tenacity.retry_if_exception_type(ApiConnectionError)
| tenacity.retry_if_exception_type(ApiInternalServerError)
tenacity.retry_if_exception_type(ApiConnectionError) |
tenacity.retry_if_exception_type(ApiInternalServerError)
)
return tenacity.Retrying(
reraise=True,
Expand Down
24 changes: 24 additions & 0 deletions tests/test_orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,30 @@ class Meta:
test = Test.from_response_content(response_content)
assert test.other is None

def test_from_response_with_relationship_with_links_only(self):
mock_api = mock.MagicMock()
mock_api.endpoint().get().data = data.ResourceIdentifier(type='test', id='159')
response_content = data.JsonApiResponse(
data=data.JsonApiObject(id='1', type='test', relationships={
'other': data.Relationship(links=data.Dictionary(self='http://example.org/api/rest/test/1/relationships'
'/vendor',
related='http://example.org/api/rest/test/1/vendor'
)
)
})
)
orm_api = orm.OrmApi(mock_api)

class Test(orm.ApiModel):
class Meta:
api = orm_api
type = 'test'
other = orm.RelationField(source='other')
name = orm.AttributeField(source='name')
test = Test.from_response_content(response_content)
assert test.other.type is 'test'
assert test.other.id is '159'

def test_issue_19_attributes_are_readable_with_multiple_relations(self):
response_content = data.JsonApiResponse(
data=data.JsonApiObject(
Expand Down