Skip to content

Commit 501ee44

Browse files
committed
Refactor response
1 parent 3e05837 commit 501ee44

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

loading_sdk/api.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ def _get_threads_in_forum_category(self, category_name, page):
4848

4949
# Doing this checks to make sure it only return data from a page that exists.
5050
if page and page < 1:
51-
return {"code": 404, "post": {"posts": [], "users": []}}
51+
return {"code": 404, "data": {"posts": [], "users": []}}
5252

5353
response = requests.get(url, headers=headers)
5454
data = response.json()
5555

5656
# Page out of range.
5757
if not len(data["posts"]):
58-
return {"code": 404, "post": data}
58+
return {"code": 404, "data": data}
5959

60-
return {"code": 200, "post": data}
60+
return {"code": 200, "data": data}
6161

6262
def get_profile(self):
6363
url = f"{API_URL}/{API_VERSION}/users/profile"
@@ -69,7 +69,7 @@ def get_profile(self):
6969
if response.status_code == 200:
7070
return {
7171
"code": response.status_code,
72-
"profile": response.json(),
72+
"data": response.json(),
7373
}
7474

7575
return response.json()
@@ -85,7 +85,7 @@ def search(self, query):
8585
if response.status_code == 200:
8686
return {
8787
"code": response.status_code,
88-
"search_results": response.json(),
88+
"data": response.json(),
8989
}
9090

9191
return response.json()
@@ -104,7 +104,7 @@ def get_post(self, post_id):
104104
if response.status_code == 200:
105105
return {
106106
"code": response.status_code,
107-
"post": response.json(),
107+
"data": response.json(),
108108
}
109109

110110
return response.json()
@@ -148,10 +148,10 @@ def get_thread(self, thread_id, page=None):
148148
if page < 1 or page > pages:
149149
return {
150150
"code": response.status_code,
151-
"post": {"posts": [], "users": []},
151+
"data": {"posts": [], "users": []},
152152
}
153153

154-
successful_response = {"code": response.status_code, "post": data}
154+
successful_response = {"code": response.status_code, "data": data}
155155

156156
return successful_response
157157

@@ -187,16 +187,16 @@ def get_editorials(self, page=None, post_type=None, sort=None):
187187

188188
# Doing this checks to make sure it only return data from a page that exists.
189189
if page and page < 1:
190-
return {"code": 404, "post": {"posts": [], "users": []}}
190+
return {"code": 404, "data": {"posts": [], "users": []}}
191191

192192
response = requests.get(url, headers=headers)
193193
data = response.json()
194194

195195
# Page out of range.
196196
if not len(data["posts"]):
197-
return {"code": 404, "post": data}
197+
return {"code": 404, "data": data}
198198

199-
return {"code": 200, "post": data}
199+
return {"code": 200, "data": data}
200200

201201
def create_post(self, thread_id, message):
202202
if not thread_id:

tests/test_api.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def test_get_profile_success(self, mock_requests, mock_authenticate):
151151
self.assertIsNotNone(api._cookies)
152152
self.assertEqual(api._cookies, self.cookie_jar)
153153
self.assertEqual(response.get("code"), 200)
154-
self.assertDictEqual(response.get("profile"), expected_response)
154+
self.assertDictEqual(response.get("data"), expected_response)
155155

156156
@patch("loading_sdk.api.requests")
157157
def test_get_profile_failure(self, mock_requests):
@@ -209,7 +209,7 @@ def test_search_success(self, mock_requests):
209209
response = api.search("zGwszApFEcY")
210210

211211
self.assertEqual(response.get("code"), 200)
212-
self.assertEqual(response.get("search_results"), expected_response)
212+
self.assertEqual(response.get("data"), expected_response)
213213

214214
@patch("loading_sdk.api.requests")
215215
def test_search_success_no_results(self, mock_requests):
@@ -224,7 +224,7 @@ def test_search_success_no_results(self, mock_requests):
224224
response = api.search("zGwszApFEcYesf")
225225

226226
self.assertEqual(response.get("code"), 200)
227-
self.assertEqual(response.get("search_results"), expected_response)
227+
self.assertEqual(response.get("data"), expected_response)
228228

229229
@patch("loading_sdk.api.requests")
230230
def test_search_failure_empty_query(self, mock_requests):
@@ -327,7 +327,7 @@ def test_get_post_success(self, mock_requests):
327327
response = api.get_post("none_existing_post_id")
328328

329329
self.assertEqual(response.get("code"), 200)
330-
self.assertEqual(response.get("post"), expected_response)
330+
self.assertEqual(response.get("data"), expected_response)
331331

332332
@patch("loading_sdk.api.requests")
333333
def test_get_thread_success(self, mock_requests):
@@ -369,19 +369,19 @@ def test_get_thread_success(self, mock_requests):
369369
response = api.get_thread("5f9e4e8c2c32e2001ed17170")
370370

371371
self.assertEqual(response.get("code"), 200)
372-
self.assertEqual(response.get("post"), expected_response)
372+
self.assertEqual(response.get("data"), expected_response)
373373

374374
api = LoadingApiClient()
375375
response = api.get_thread("5f9e4e8c2c32e2001ed17170", page=0)
376376

377377
self.assertEqual(response.get("code"), 200)
378-
self.assertEqual(response.get("post"), expected_response)
378+
self.assertEqual(response.get("data"), expected_response)
379379

380380
api = LoadingApiClient()
381381
response = api.get_thread("5f9e4e8c2c32e2001ed17170", page=1)
382382

383383
self.assertEqual(response.get("code"), 200)
384-
self.assertEqual(response.get("post"), expected_response)
384+
self.assertEqual(response.get("data"), expected_response)
385385

386386
@patch("loading_sdk.api.requests")
387387
def test_get_thread_failure_empty_thread_id(self, mock_requests):
@@ -464,7 +464,7 @@ def test_get_thread_failure_does_not_exist(self, mock_requests):
464464
@patch("loading_sdk.api.requests")
465465
def test_get_thread_failure_page_too_low(self, mock_requests):
466466
status_code = 200
467-
expected_response = {"code": 200, "post": {"posts": [], "users": []}}
467+
expected_response = {"code": 200, "data": {"posts": [], "users": []}}
468468

469469
mock_response = MagicMock()
470470
mock_response.status_code = status_code
@@ -506,7 +506,7 @@ def test_get_thread_failure_page_too_low(self, mock_requests):
506506
@patch("loading_sdk.api.requests")
507507
def test_get_thread_failure_page_too_high(self, mock_requests):
508508
status_code = 200
509-
expected_response = {"code": 200, "post": {"posts": [], "users": []}}
509+
expected_response = {"code": 200, "data": {"posts": [], "users": []}}
510510

511511
mock_response = MagicMock()
512512
mock_response.status_code = status_code
@@ -927,12 +927,12 @@ def test_get_games_success(self, mock_requests):
927927
api = LoadingApiClient()
928928
response = api.get_games(page=91)
929929

930-
self.assertDictEqual(response.get("post"), expected_response)
930+
self.assertDictEqual(response.get("data"), expected_response)
931931

932932
@patch("loading_sdk.api.requests")
933933
def test_get_games_failure_page_too_low(self, mock_requests):
934934
status_code = 404
935-
expected_response = {"code": status_code, "post": {"posts": [], "users": []}}
935+
expected_response = {"code": status_code, "data": {"posts": [], "users": []}}
936936

937937
mock_response = MagicMock()
938938
mock_response.status_code = status_code
@@ -947,7 +947,7 @@ def test_get_games_failure_page_too_low(self, mock_requests):
947947
@patch("loading_sdk.api.requests")
948948
def test_get_games_failure_page_too_high(self, mock_requests):
949949
status_code = 404
950-
expected_response = {"code": status_code, "post": {"posts": [], "users": []}}
950+
expected_response = {"code": status_code, "data": {"posts": [], "users": []}}
951951

952952
mock_response = MagicMock()
953953
mock_response.status_code = status_code
@@ -1170,17 +1170,17 @@ def test_get_other_success(self, mock_requests):
11701170
api = LoadingApiClient()
11711171
response = api.get_games(page=91)
11721172

1173-
self.assertDictEqual(response.get("post"), expected_response)
1173+
self.assertDictEqual(response.get("data"), expected_response)
11741174

1175-
threads = response.get("post").get("posts")
1175+
threads = response.get("data").get("posts")
11761176

11771177
for thread in threads:
11781178
self.assertEqual(thread.get("category"), "other")
11791179

11801180
@patch("loading_sdk.api.requests")
11811181
def test_get_other_failure_page_too_low(self, mock_requests):
11821182
status_code = 404
1183-
expected_response = {"code": status_code, "post": {"posts": [], "users": []}}
1183+
expected_response = {"code": status_code, "data": {"posts": [], "users": []}}
11841184

11851185
mock_response = MagicMock()
11861186
mock_response.status_code = status_code
@@ -1195,7 +1195,7 @@ def test_get_other_failure_page_too_low(self, mock_requests):
11951195
@patch("loading_sdk.api.requests")
11961196
def test_get_other_failure_page_too_high(self, mock_requests):
11971197
status_code = 404
1198-
expected_response = {"code": status_code, "post": {"posts": [], "users": []}}
1198+
expected_response = {"code": status_code, "data": {"posts": [], "users": []}}
11991199

12001200
mock_response = MagicMock()
12011201
mock_response.status_code = status_code
@@ -1505,17 +1505,17 @@ def test_get_editorials_success(self, mock_requests):
15051505
api = LoadingApiClient()
15061506
response = api.get_editorials(page=1, post_type="update", sort="title")
15071507

1508-
self.assertDictEqual(response.get("post"), expected_response)
1508+
self.assertDictEqual(response.get("data"), expected_response)
15091509

1510-
threads = response.get("post").get("posts")
1510+
threads = response.get("data").get("posts")
15111511

15121512
for thread in threads:
15131513
self.assertEqual(thread.get("postType"), "update")
15141514

15151515
@patch("loading_sdk.api.requests")
15161516
def test_get_editorials_failure_page_too_low(self, mock_requests):
15171517
status_code = 404
1518-
expected_response = {"code": status_code, "post": {"posts": [], "users": []}}
1518+
expected_response = {"code": status_code, "data": {"posts": [], "users": []}}
15191519

15201520
mock_response = MagicMock()
15211521
mock_response.status_code = status_code
@@ -1534,7 +1534,7 @@ def test_get_editorials_failure_page_too_low(self, mock_requests):
15341534
@patch("loading_sdk.api.requests")
15351535
def test_get_editorials_failure_page_too_high(self, mock_requests):
15361536
status_code = 404
1537-
expected_response = {"code": status_code, "post": {"posts": [], "users": []}}
1537+
expected_response = {"code": status_code, "data": {"posts": [], "users": []}}
15381538

15391539
mock_response = MagicMock()
15401540
mock_response.status_code = status_code

0 commit comments

Comments
 (0)