Skip to content

Commit 489b611

Browse files
committed
modify base API url, few fixes and improvements
1 parent ff478c5 commit 489b611

2 files changed

Lines changed: 26 additions & 19 deletions

File tree

CHANGELOG.MD

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ All notable changes to this project will be documented in this file.
7070
### Fixed
7171
- Changed the param name of /search to `query` from `title` to match API change
7272

73-
# v1.3.2 - 24/7/23
73+
# v1.3.2 - 24/7/2023
7474

7575
### Fixed
76-
- Fixed dict key error in `CircularChecker` class.
76+
- Fixed dict key error in `CircularChecker` class.
77+
78+
# v1.2.3 - 8/4/2024
79+
80+
## Changed
81+
- The package now uses bpsapi.rajtech.me/latest/{category} instead of the params method for list and latest

pybpsapi.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class API:
66
"""Methods which communicate with the API"""
77

8-
def __init__(self, url="https://bpsapi.rajtech.me/v1/"):
8+
def __init__(self, url="https://bpsapi.rajtech.me/"):
99
self.url = url
1010

1111
json = requests.get(self.url + "categories").json()
@@ -15,21 +15,21 @@ def __init__(self, url="https://bpsapi.rajtech.me/v1/"):
1515
raise ConnectionError("Invalid API Response. API says there are no categories.")
1616

1717
# /latest endpoint
18-
def latest(self, category: str or int) -> dict or None:
18+
def latest(self, category: str or int) -> dict | None:
1919
"""The `/latest` endpoint returns the latest circular from a particular category"""
2020
if type(category) == int:
2121
category = int(category)
22+
2223
if not 1 < category < 100:
2324
raise ValueError("Invalid category Number")
2425

2526
else:
2627
if category not in self.categories:
2728
raise ValueError("Invalid category Name")
2829

29-
params = {'category': category}
30-
31-
request = requests.get(self.url + "latest", params=params)
30+
request = requests.get(f"{self.url}latest/{category}")
3231
json = request.json()
32+
3333
try:
3434
json['http_status']
3535
except KeyError:
@@ -38,7 +38,7 @@ def latest(self, category: str or int) -> dict or None:
3838
return json['data']
3939

4040
# /list endpoint
41-
def list(self, category: str or int, amount: int = -1) -> list or None:
41+
def list(self, category: str or int, amount: int = -1) -> list | None:
4242
"""The `/list` endpoint returns a list of circulars from a particular category"""
4343
if type(category) == int:
4444
category = int(category)
@@ -52,18 +52,17 @@ def list(self, category: str or int, amount: int = -1) -> list or None:
5252
if amount < -1:
5353
amount = -1
5454

55-
params = {'category': category}
56-
57-
request = requests.get(self.url + "list", params=params)
55+
request = requests.get(f"{self.url}list/{category}")
5856
json = request.json()
57+
5958
try:
6059
json['http_status']
6160
except KeyError:
6261
raise ConnectionError("Invalid API Response")
6362
if json['http_status'] == 200:
6463
return json['data'] if amount == -1 else json['data'][:amount]
6564

66-
def search(self, query: str or int, amount: int = 1) -> dict or None:
65+
def search(self, query: str or int, amount: int = 1) -> dict | None:
6766
"""The `/search` endpoint lets you search for a circular by its name or ID"""
6867
if query.isdigit() and len(query) == 4:
6968
query = int(query)
@@ -85,7 +84,7 @@ def search(self, query: str or int, amount: int = 1) -> dict or None:
8584
return json['data']
8685

8786
# /getpng endpoint
88-
def getpng(self, url: str) -> list or None:
87+
def getpng(self, url: str) -> list | None:
8988
"""The `/getpng` endpoint lets you get the pngs from a circular"""
9089
if type(url) != str:
9190
raise ValueError("Invalid URL")
@@ -106,13 +105,14 @@ def getpng(self, url: str) -> list or None:
106105

107106

108107
class CircularChecker:
109-
def __init__(self, category, url: str = "https://bpsapi.rajtech.me/v1/", cache_method=None, debug: bool = False,
108+
def __init__(self, category, url: str = "https://bpsapi.rajtech.me/", cache_method=None, debug: bool = False,
110109
**kwargs):
111110
self.url = url
112111
self.category = category
113112
self._cache = []
114113

115114
json = requests.get(self.url + "categories").json()
115+
116116
if json['http_status'] == 200:
117117
self.categories = json['data']
118118
else:
@@ -131,7 +131,6 @@ def __init__(self, category, url: str = "https://bpsapi.rajtech.me/v1/", cache_m
131131
if category not in self.categories:
132132
raise ValueError("Invalid category Name")
133133

134-
self._params = {'category': category}
135134
self.cache_method = cache_method
136135

137136
if cache_method is not None:
@@ -217,16 +216,18 @@ def _set_cache(self, data, title: str = "circular_list"):
217216
self._cache = data
218217

219218
def _refresh_cache(self):
220-
request = requests.get(self.url + "list", params=self._params)
219+
request = requests.get(f"{self.url}list/{self.category}")
221220
json = request.json()
221+
222222
try:
223223
json['http_status']
224224
except KeyError:
225225
raise ValueError("Invalid API Response")
226+
226227
if json['http_status'] == 200:
227228
self._set_cache(json['data'])
228229

229-
def check(self) -> list[dict] or list[None]:
230+
def check(self) -> list[dict] | list[None]:
230231
return_dict = []
231232
old_cached = self.get_cache()
232233

@@ -236,6 +237,7 @@ def check(self) -> list[dict] or list[None]:
236237

237238
self._cur.execute(f"SELECT * FROM {self.db_table} WHERE category = ?", (self.category,))
238239
res = self._cur.fetchone()
240+
239241
if res is None:
240242
cache = []
241243
else:
@@ -286,12 +288,12 @@ def add(self, checker: CircularChecker, *args: CircularChecker):
286288
raise ValueError("Invalid CircularChecker Object")
287289
self._checkers.append(arg)
288290

289-
def create(self, category, url: str = "https://bpsapi.rajtech.me/v1/", cache_method=None, debug: bool = False,
291+
def create(self, category, url: str = "https://bpsapi.rajtech.me/", cache_method=None, debug: bool = False,
290292
**kwargs):
291293
checker = CircularChecker(category, url, cache_method, debug, **kwargs)
292294
self._checkers.append(checker)
293295

294-
def check(self) -> dict[list[dict] or list[None]]:
296+
def check(self) -> dict[list[dict]]:
295297
return_dict = {}
296298
for checker in self._checkers:
297299
return_dict[checker.category] = checker.check()

0 commit comments

Comments
 (0)