Skip to content

Commit f66ef28

Browse files
committed
Add messages to all responses to make it clear what happened
1 parent f4a4b1b commit f66ef28

File tree

2 files changed

+83
-18
lines changed

2 files changed

+83
-18
lines changed

loading_sdk/api.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,20 @@ 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, "data": {"posts": [], "users": []}}
51+
return {
52+
"code": 404,
53+
"message": "Page number too low",
54+
"data": {"posts": [], "users": []},
55+
}
5256

5357
response = requests.get(url, headers=headers)
5458
data = response.json()
5559

5660
# Page out of range.
5761
if not len(data["posts"]):
58-
return {"code": 404, "data": data}
62+
return {"code": 404, "message": "Page number too high", "data": data}
5963

60-
return {"code": 200, "data": data}
64+
return {"code": 200, "message": "OK", "data": data}
6165

6266
def get_profile(self):
6367
url = f"{API_URL}/{API_VERSION}/users/profile"
@@ -69,6 +73,7 @@ def get_profile(self):
6973
if response.status_code == 200:
7074
return {
7175
"code": response.status_code,
76+
"message": "OK",
7277
"data": response.json(),
7378
}
7479

@@ -81,14 +86,16 @@ def search(self, query):
8186
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
8287
}
8388
response = requests.post(url, headers=headers, data={"query": query})
89+
data = response.json()
8490

8591
if response.status_code == 200:
8692
return {
8793
"code": response.status_code,
88-
"data": response.json(),
94+
"message": "OK" if len(data["posts"]) else "No results",
95+
"data": data,
8996
}
9097

91-
return response.json()
98+
return data
9299

93100
def get_post(self, post_id):
94101
if not post_id:
@@ -104,6 +111,7 @@ def get_post(self, post_id):
104111
if response.status_code == 200:
105112
return {
106113
"code": response.status_code,
114+
"message": "OK",
107115
"data": response.json(),
108116
}
109117

@@ -145,13 +153,24 @@ def get_thread(self, thread_id, page=None):
145153
pages = 1
146154

147155
# Page is out of range.
148-
if page < 1 or page > pages:
156+
if page < 1:
157+
return {
158+
"code": response.status_code,
159+
"message": "Page number too low",
160+
"data": {"posts": [], "users": []},
161+
}
162+
elif page > pages:
149163
return {
150164
"code": response.status_code,
165+
"message": "Page number too high",
151166
"data": {"posts": [], "users": []},
152167
}
153168

154-
successful_response = {"code": response.status_code, "data": data}
169+
successful_response = {
170+
"code": response.status_code,
171+
"message": "OK",
172+
"data": data,
173+
}
155174

156175
return successful_response
157176

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

188207
# Doing this checks to make sure it only return data from a page that exists.
189208
if page and page < 1:
190-
return {"code": 404, "data": {"posts": [], "users": []}}
209+
return {
210+
"code": 404,
211+
"message": "Page number too low",
212+
"data": {"posts": [], "users": []},
213+
}
191214

192215
response = requests.get(url, headers=headers)
193216
data = response.json()
194217

195218
# Page out of range.
196219
if not len(data["posts"]):
197-
return {"code": 404, "data": data}
220+
return {"code": 404, "message": "Page number too high", "data": data}
198221

199-
return {"code": 200, "data": data}
222+
return {"code": 200, "message": "OK", "data": data}
200223

201224
def create_post(self, thread_id, message):
202225
if not thread_id:

tests/test_api.py

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +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.assertEqual(response.get("message"), "OK")
154155
self.assertDictEqual(response.get("data"), expected_response)
155156

156157
@patch("loading_sdk.api.requests")
@@ -209,6 +210,7 @@ def test_search_success(self, mock_requests):
209210
response = api.search("zGwszApFEcY")
210211

211212
self.assertEqual(response.get("code"), 200)
213+
self.assertEqual(response.get("message"), "OK")
212214
self.assertEqual(response.get("data"), expected_response)
213215

214216
@patch("loading_sdk.api.requests")
@@ -224,6 +226,7 @@ def test_search_success_no_results(self, mock_requests):
224226
response = api.search("zGwszApFEcYesf")
225227

226228
self.assertEqual(response.get("code"), 200)
229+
self.assertEqual(response.get("message"), "No results")
227230
self.assertEqual(response.get("data"), expected_response)
228231

229232
@patch("loading_sdk.api.requests")
@@ -327,6 +330,7 @@ def test_get_post_success(self, mock_requests):
327330
response = api.get_post("none_existing_post_id")
328331

329332
self.assertEqual(response.get("code"), 200)
333+
self.assertEqual(response.get("message"), "OK")
330334
self.assertEqual(response.get("data"), expected_response)
331335

332336
@patch("loading_sdk.api.requests")
@@ -369,18 +373,21 @@ def test_get_thread_success(self, mock_requests):
369373
response = api.get_thread("5f9e4e8c2c32e2001ed17170")
370374

371375
self.assertEqual(response.get("code"), 200)
376+
self.assertEqual(response.get("message"), "OK")
372377
self.assertEqual(response.get("data"), expected_response)
373378

374379
api = LoadingApiClient()
375380
response = api.get_thread("5f9e4e8c2c32e2001ed17170", page=0)
376381

377382
self.assertEqual(response.get("code"), 200)
383+
self.assertEqual(response.get("message"), "OK")
378384
self.assertEqual(response.get("data"), expected_response)
379385

380386
api = LoadingApiClient()
381387
response = api.get_thread("5f9e4e8c2c32e2001ed17170", page=1)
382388

383389
self.assertEqual(response.get("code"), 200)
390+
self.assertEqual(response.get("message"), "OK")
384391
self.assertEqual(response.get("data"), expected_response)
385392

386393
@patch("loading_sdk.api.requests")
@@ -464,7 +471,11 @@ def test_get_thread_failure_does_not_exist(self, mock_requests):
464471
@patch("loading_sdk.api.requests")
465472
def test_get_thread_failure_page_too_low(self, mock_requests):
466473
status_code = 200
467-
expected_response = {"code": 200, "data": {"posts": [], "users": []}}
474+
expected_response = {
475+
"code": 200,
476+
"message": "Page number too low",
477+
"data": {"posts": [], "users": []},
478+
}
468479

469480
mock_response = MagicMock()
470481
mock_response.status_code = status_code
@@ -506,7 +517,11 @@ def test_get_thread_failure_page_too_low(self, mock_requests):
506517
@patch("loading_sdk.api.requests")
507518
def test_get_thread_failure_page_too_high(self, mock_requests):
508519
status_code = 200
509-
expected_response = {"code": 200, "data": {"posts": [], "users": []}}
520+
expected_response = {
521+
"code": 200,
522+
"message": "Page number too high",
523+
"data": {"posts": [], "users": []},
524+
}
510525

511526
mock_response = MagicMock()
512527
mock_response.status_code = status_code
@@ -543,6 +558,7 @@ def test_get_thread_failure_page_too_high(self, mock_requests):
543558
response = api.get_thread("5f9e4e8c2c32e2001ed17170", page=2)
544559

545560
self.assertEqual(response.get("code"), 200)
561+
self.assertEqual(response.get("message"), "Page number too high")
546562
self.assertEqual(response, expected_response)
547563

548564
@patch("loading_sdk.api.requests")
@@ -932,7 +948,11 @@ def test_get_games_success(self, mock_requests):
932948
@patch("loading_sdk.api.requests")
933949
def test_get_games_failure_page_too_low(self, mock_requests):
934950
status_code = 404
935-
expected_response = {"code": status_code, "data": {"posts": [], "users": []}}
951+
expected_response = {
952+
"code": status_code,
953+
"message": "Page number too low",
954+
"data": {"posts": [], "users": []},
955+
}
936956

937957
mock_response = MagicMock()
938958
mock_response.status_code = status_code
@@ -947,7 +967,11 @@ def test_get_games_failure_page_too_low(self, mock_requests):
947967
@patch("loading_sdk.api.requests")
948968
def test_get_games_failure_page_too_high(self, mock_requests):
949969
status_code = 404
950-
expected_response = {"code": status_code, "data": {"posts": [], "users": []}}
970+
expected_response = {
971+
"code": status_code,
972+
"message": "Page number too high",
973+
"data": {"posts": [], "users": []},
974+
}
951975

952976
mock_response = MagicMock()
953977
mock_response.status_code = status_code
@@ -1180,7 +1204,11 @@ def test_get_other_success(self, mock_requests):
11801204
@patch("loading_sdk.api.requests")
11811205
def test_get_other_failure_page_too_low(self, mock_requests):
11821206
status_code = 404
1183-
expected_response = {"code": status_code, "data": {"posts": [], "users": []}}
1207+
expected_response = {
1208+
"code": status_code,
1209+
"message": "Page number too low",
1210+
"data": {"posts": [], "users": []},
1211+
}
11841212

11851213
mock_response = MagicMock()
11861214
mock_response.status_code = status_code
@@ -1195,7 +1223,11 @@ def test_get_other_failure_page_too_low(self, mock_requests):
11951223
@patch("loading_sdk.api.requests")
11961224
def test_get_other_failure_page_too_high(self, mock_requests):
11971225
status_code = 404
1198-
expected_response = {"code": status_code, "data": {"posts": [], "users": []}}
1226+
expected_response = {
1227+
"code": status_code,
1228+
"message": "Page number too high",
1229+
"data": {"posts": [], "users": []},
1230+
}
11991231

12001232
mock_response = MagicMock()
12011233
mock_response.status_code = status_code
@@ -1505,6 +1537,8 @@ def test_get_editorials_success(self, mock_requests):
15051537
api = LoadingApiClient()
15061538
response = api.get_editorials(page=1, post_type="update", sort="title")
15071539

1540+
self.assertEqual(response.get("code"), 200)
1541+
self.assertEqual(response.get("message"), "OK")
15081542
self.assertDictEqual(response.get("data"), expected_response)
15091543

15101544
threads = response.get("data").get("posts")
@@ -1515,7 +1549,11 @@ def test_get_editorials_success(self, mock_requests):
15151549
@patch("loading_sdk.api.requests")
15161550
def test_get_editorials_failure_page_too_low(self, mock_requests):
15171551
status_code = 404
1518-
expected_response = {"code": status_code, "data": {"posts": [], "users": []}}
1552+
expected_response = {
1553+
"code": status_code,
1554+
"message": "Page number too low",
1555+
"data": {"posts": [], "users": []},
1556+
}
15191557

15201558
mock_response = MagicMock()
15211559
mock_response.status_code = status_code
@@ -1534,7 +1572,11 @@ def test_get_editorials_failure_page_too_low(self, mock_requests):
15341572
@patch("loading_sdk.api.requests")
15351573
def test_get_editorials_failure_page_too_high(self, mock_requests):
15361574
status_code = 404
1537-
expected_response = {"code": status_code, "data": {"posts": [], "users": []}}
1575+
expected_response = {
1576+
"code": status_code,
1577+
"message": "Page number too high",
1578+
"data": {"posts": [], "users": []},
1579+
}
15381580

15391581
mock_response = MagicMock()
15401582
mock_response.status_code = status_code

0 commit comments

Comments
 (0)