Skip to content

Commit 2046850

Browse files
committed
fixing big test pb + changing API Listing method + new functions
1 parent 30995ae commit 2046850

File tree

11 files changed

+64
-38
lines changed

11 files changed

+64
-38
lines changed
9 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
25 Bytes
Binary file not shown.
536 Bytes
Binary file not shown.
418 Bytes
Binary file not shown.

onepyece/common.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
"episodes": ["id", "count", "title", "saga_id", "arc_id"],
66
"movies": ["id", "count", "title"],
77
"tomes": ["id", "count", "title"],
8-
"chapters": ["id", "count", "title", "tome_id", "tome"],
8+
"chapters": ["id", "count", "title", "tome_id"],
99
"arcs": ["id", "count", "saga_id"],
1010
"sagas": ["id", "count", "title"],
1111
"hakis": ["id", "count", "name", "roman_name", "characters_id"],
12-
"characters": ["id", "count", "name", "job", "bounty", "status", "size"],
12+
"characters": ["id", "count", "name", "job", "bounty", "status", "size", "crew_id", "fruit_id"],
1313
"dials": ["id", "count", "name", "type"],
1414
"luffy/gears": ["id", "count", "title"],
1515
"luffy/techniques": ["id", "count", "name", "translation", "gear_id"],
@@ -20,50 +20,52 @@
2020
"crews": ["id", "count", "name", "status", "yonko"],
2121
}
2222

23-
STRING_SEARCHES = ["name", "job", "bounty", "status", "size", "type", "tome_name", "roman_name", "sea", "affiliation"]
23+
STRING_SEARCHES = ["name", "job", "bounty", "status", "size", "type", "roman_name", "sea", "affiliation"]
2424
ID_SEARCHES = ["crew_id", "captain_id"]
25-
NO_SEARCH_ID_SEARCHES = ["saga_id", "arc_id", "characters_id", "gear_id", "tome_id"]
25+
NO_SEARCH_ID_SEARCHES = ["saga_id", "arc_id", "characters_id", "gear_id", "tome_id", "fruit_id", "crew_id"]
2626
NO_RESOURCE_SEARCHES = ["count", "yonko"]
2727

2828

29-
def check_params(endpoint, search=None, resource_id=None):
29+
def check_params(endpoint, search=None, resource=None):
3030
if endpoint not in ENDPOINTS:
3131
raise ValueError(f"Unknown API endpoint '{endpoint}'")
3232
if search is not None and search not in ENDPOINTS[endpoint]:
3333
raise ValueError(f"Unknown search '{search}' for endpoint '{endpoint}'")
34-
if search is not None and search not in NO_RESOURCE_SEARCHES and resource_id is None:
35-
raise ValueError("Resource ID is required for this search")
36-
if resource_id is not None and not isinstance(resource_id, str):
37-
raise ValueError("Resource ID must be a string, even if it's a number")
34+
if search is not None and search not in NO_RESOURCE_SEARCHES and resource is None:
35+
raise ValueError("Resource is required for this search")
36+
if resource is not None and "id" in search and not isinstance(resource, int):
37+
raise ValueError("Resource must be an integer for this search")
3838
return None
3939

4040

41-
def build_url(endpoint, search=None, resource_id=None):
42-
check_params(endpoint, search, resource_id)
41+
def build_url(endpoint, search=None, resource=None):
42+
check_params(endpoint, search, resource)
4343
if search is not None:
44-
if resource_id is not None:
45-
resource_id = convert_name(resource_id)
46-
return adding_search(endpoint, search, resource_id)
44+
if resource is not None:
45+
resource = convert_resource(resource)
46+
return adding_search(endpoint, search, resource)
4747
return f"{URL}{endpoint}/{search}"
4848
return f"{URL}{endpoint}"
4949

5050

51-
def adding_search(endpoint, search, resource_id=None):
51+
def adding_search(endpoint, search, resource=None):
5252
if search in STRING_SEARCHES:
53-
return f"{URL}{endpoint}/search/{search}/{resource_id}"
54-
elif search in ID_SEARCHES or (endpoint == "arcs" and 'id' in search and search != "id"):
55-
return f"{URL}{endpoint}/search/{search[:-3]}/{resource_id}"
56-
elif search in NO_SEARCH_ID_SEARCHES:
57-
return f"{URL}{endpoint}/{search[:-3]}/{resource_id}"
53+
return f"{URL}{endpoint}/search/{search}/{resource}"
54+
elif search in NO_SEARCH_ID_SEARCHES and endpoint not in ['boats', 'arcs']:
55+
return f"{URL}{endpoint}/{search[:-3]}/{resource}"
56+
elif search in ID_SEARCHES:
57+
return f"{URL}{endpoint}/search/{search[:-3]}/{resource}"
5858
elif search == "title":
59-
return f"{URL}{endpoint}/search/{resource_id}"
60-
return f"{URL}{endpoint}/{resource_id}"
59+
return f"{URL}{endpoint}/search/{resource}"
60+
return f"{URL}{endpoint}/{resource}"
6161

6262

63-
def convert_name(name):
64-
if ' / ' in name:
65-
name = name.split(" / ")[0]
66-
return name.replace(" ", "%20")
63+
def convert_resource(resource):
64+
if isinstance(resource, int):
65+
return resource
66+
if ' / ' in resource:
67+
resource = resource.split(" / ")[0]
68+
return resource.replace(" ", "%20")
6769

6870

6971
def pretty_print(data):

onepyece/functions.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ def chapter_by_title(resource):
5353
return API("chapters", "title", resource)
5454

5555

56+
def chapter_by_tome_id(resource):
57+
return API("chapters", "tome_id", resource)
58+
59+
60+
def chapter_by_tome(resource):
61+
return API("chapters", "tome", resource)
62+
63+
5664
def count_chapters():
5765
return API("chapters", "count")
5866

@@ -129,6 +137,14 @@ def character_by_size(resource):
129137
return API("characters", "size", resource)
130138

131139

140+
def character_by_crew_id(resource):
141+
return API("characters", "crew_id", resource)
142+
143+
144+
def character_by_fruit_id(resource):
145+
return API("characters", "fruit_id", resource)
146+
147+
132148
def count_characters():
133149
return API("characters", "count")
134150

onepyece/interface.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,31 @@
33

44

55
class API:
6-
def __init__(self, endpoint, search=None, resource=None):
6+
def __init__(self, endpoint=None, search=None, resource=None, object=False, data=None):
77
self.endpoint = endpoint
88
self.search = search
99
self.resource = resource
10-
self.url = build_url(endpoint, search, resource)
11-
self.load()
10+
self.object = object
11+
if endpoint is not None:
12+
self.url = build_url(endpoint, search, resource)
13+
if not object:
14+
self.__load()
15+
else:
16+
self.__dict__.update(data)
17+
1218

1319
def __repr__(self):
14-
return f"API(endpoint={self.endpoint}, resource_id={self.resource})"
20+
if self.object:
21+
return f"API(object={self.__dict__})"
22+
if self.resource is not None:
23+
return f"API(endpoint='{self.endpoint}', search='{self.search}', resource='{self.resource}', url='{self.url}')"
24+
if self.search is not None:
25+
return f"API(endpoint='{self.endpoint}', search='{self.search}', url='{self.url}')"
26+
return f"API(endpoint='{self.endpoint}', url='{self.url}')"
1527

1628
def __str__(self):
17-
if getattr(self, "results", None) is not None:
18-
for result in self.results:
19-
print(pretty_print(result))
20-
return ""
2129
if getattr(self, "count", None) is not None:
22-
return f"Total {self.endpoint} found: {self.count}"
30+
return f"Total {self.endpoint} found for this search: {self.count}.\nTo see the results, iterate over the object."
2331
return pretty_print(self.__dict__)
2432

2533
def __iter__(self):
@@ -37,12 +45,12 @@ def __len__(self):
3745
return self.count
3846
raise TypeError(f"Object of type {type(self)} has no len() for unique result")
3947

40-
def load(self):
48+
def __load(self):
4149
api_data = get_data(self.url)
4250
if api_data is None:
4351
raise ValueError(f"No data found for the given search:{self.search} and resource_id:{self.resource}")
4452
elif isinstance(api_data, list) and len(api_data) > 1:
45-
self.results = api_data
53+
self.results = [API(object=True, data=result) for result in api_data]
4654
self.count = len(api_data)
4755
elif isinstance(api_data, list) and len(api_data) == 1:
4856
self.__dict__.update(api_data[0])
155 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)