Skip to content

Commit fc9b763

Browse files
committed
Create method and tests for the other category and refactor get_games
1 parent 76a7bf2 commit fc9b763

File tree

2 files changed

+276
-16
lines changed

2 files changed

+276
-16
lines changed

loading_api_wrapper/api.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@ def _authenticate(self, email, password):
3434

3535
return response.json()
3636

37+
def _get_threads_in_forum_category(self, category_name, page):
38+
url = f"{API_URL}/{API_VERSION}/posts/"
39+
headers = {"User-Agent": USER_AGENT, category_name: category_name}
40+
41+
# Chooses a specific page instead of the first page which is the default page.
42+
if page and page > 1:
43+
headers["page"] = str(page)
44+
45+
# Doing this checks to make sure it only return data from a page that exists.
46+
if page and page < 1:
47+
return {"code": 404, "post": {"posts": [], "users": []}}
48+
49+
response = requests.get(url, headers=headers)
50+
data = response.json()
51+
52+
# Page out of range.
53+
if not len(data["posts"]):
54+
return {"code": 404, "post": data}
55+
56+
return {"code": 200, "post": data}
57+
3758
def get_profile(self):
3859
url = f"{API_URL}/{API_VERSION}/users/profile"
3960
headers = {
@@ -131,22 +152,13 @@ def get_thread(self, thread_id, page=None):
131152
return successful_response
132153

133154
def get_games(self, page=None):
134-
url = f"{API_URL}/{API_VERSION}/posts/"
135-
headers = {"User-Agent": USER_AGENT, "games": "games"}
155+
category_name = "games"
156+
thread_data = self._get_threads_in_forum_category(category_name, page)
136157

137-
# Chooses a specific page instead of the first page which is the default page.
138-
if page and page > 1:
139-
headers["page"] = str(page)
158+
return thread_data
140159

141-
# Doing this checks to make sure it only return data from a page that exists.
142-
if page and page < 1:
143-
return {"code": 404, "post": {"posts": [], "users": []}}
160+
def get_other(self, page=None):
161+
category_name = "other"
162+
thread_data = self._get_threads_in_forum_category(category_name, page)
144163

145-
response = requests.get(url, headers=headers)
146-
data = response.json()
147-
148-
# Page out of range.
149-
if not len(data["posts"]):
150-
return {"code": 404, "post": data}
151-
152-
return {"code": 200, "post": data}
164+
return thread_data

0 commit comments

Comments
 (0)