Skip to content

Commit 8d559a3

Browse files
fixup! [BUG] Fix issue with results_iter when API returns an object CAT-213
1 parent 3222221 commit 8d559a3

File tree

5 files changed

+36
-39
lines changed

5 files changed

+36
-39
lines changed

ChangeLog.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9-
10-
## [v0.3.1] - 2019-08-02
119
### Fixed
12-
- Resolved issue with results_iter where API calls
13-
that returned objects failed. Fixes GitHub Issue #34
10+
- Fixed an issue where the DataApi class's results_iter method would return no data
11+
when receiving responses from Data API endpoints that set the "data" field of the
12+
response to an object (like the [itembank/questions endpoint](https://reference.learnosity.com/data-api/endpoints/itembank_endpoints#getQuestions)
13+
when item_references is included in the request).
14+
15+
### Added
16+
- utils.Future
1417

1518
## [v0.3.0] - 2019-06-17
1619
### Added

learnosity_sdk/request/dataapi.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from learnosity_sdk.exceptions import DataApiException
55
from learnosity_sdk.request import Init
6+
from learnosity_sdk.utils import Future
67

78

89
class DataApi(object):
@@ -60,7 +61,7 @@ def results_iter(self, endpoint, security_packet,
6061
secret, request_packet,
6162
action):
6263
if isinstance(response['data'], dict):
63-
for key, value in self.__iteritems(response['data']):
64+
for key, value in Future.iteritems(response['data']):
6465
yield {key: value}
6566
else:
6667
for result in response['data']:
@@ -128,12 +129,3 @@ def request_iter(self, endpoint, security_packet,
128129
'server returned unsuccessful status: ' + res.text)
129130
else:
130131
yield data
131-
132-
@staticmethod
133-
def __iteritems(obj, **kwargs):
134-
func = getattr(obj, "iteritems", None)
135-
136-
if not func:
137-
func = obj.items
138-
139-
return func(**kwargs)

learnosity_sdk/utils/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from .lrnuuid import Uuid
2+
from .lrnfuture import Future
23

34
__all__ = [
45
"Uuid",
6+
"Future"
57
]

learnosity_sdk/utils/lrnfuture.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Future:
2+
"""
3+
This helper method allows the .items method of Python 3
4+
to be wrapped to the equivalent .iteritems method
5+
of Python 2.7
6+
7+
The code for this method was sourced from Python Future:
8+
https://python-future.org/
9+
10+
Python Future is licenced under the MIT Licence. Full licence details
11+
and credits can be found here:
12+
https://python-future.org/credits.html
13+
"""
14+
@staticmethod
15+
def iteritems(obj, **kwargs):
16+
func = getattr(obj, "iteritems", None)
17+
18+
if not func:
19+
func = obj.items
20+
21+
return func(**kwargs)

tests/integration/test_dataapi.py

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22
import os
33
from learnosity_sdk.request import DataApi
4+
from learnosity_sdk.utils import Future
45

56
# This test uses the consumer key and secret for the demos consumer
67
# this is the only consumer with publicly available keys
@@ -55,33 +56,20 @@ def _build_base_url(self):
5556
def test_real_request(self):
5657
"""Make a request against Data Api to ensure the SDK works"""
5758
client = DataApi()
58-
59-
test_url = self._build_base_url() + items_endpoint
60-
61-
print("test_url is", test_url)
62-
63-
res = client.request(test_url, security, consumer_secret, items_request,
59+
res = client.request(self._build_base_url() + items_endpoint, security, consumer_secret, items_request,
6460
action)
65-
6661
returned_json = res.json()
6762

6863
assert len(returned_json['data']) > 0
6964

7065
returned_ref = returned_json['data'][0]['reference']
71-
7266
assert returned_ref in items_request['references']
7367

7468
def test_paging(self):
7569
"""Verify that paging works"""
7670
client = DataApi()
77-
78-
test_url = self._build_base_url() + items_endpoint
79-
80-
print("test_url is", test_url)
81-
82-
pages = client.request_iter(test_url, security, consumer_secret,
71+
pages = client.request_iter(self._build_base_url() + items_endpoint, security, consumer_secret,
8372
items_request, action)
84-
8573
results = set()
8674

8775
for page in pages:
@@ -110,7 +98,7 @@ def test_real_question_request(self):
11098

11199
keys = set()
112100

113-
for key, value in self.__iteritems(returned_json['data']):
101+
for key, value in Future.iteritems(returned_json['data']):
114102
keys.add(key)
115103

116104
assert keys == {'py-sdk-test-2019-1', 'py-sdk-test-2019-2'}
@@ -140,12 +128,3 @@ def test_question_paging(self):
140128

141129
assert len(results) == 3
142130
assert keys == {'py-sdk-test-2019-1', 'py-sdk-test-2019-2'}
143-
144-
@staticmethod
145-
def __iteritems(obj, **kwargs):
146-
func = getattr(obj, "iteritems", None)
147-
148-
if not func:
149-
func = obj.items
150-
151-
return func(**kwargs)

0 commit comments

Comments
 (0)