-
Notifications
You must be signed in to change notification settings - Fork 1
Add compatibility for DLCs #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
ac15d3d
cb9ab0b
be057a0
3768595
5402c0d
bf19604
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,12 +16,14 @@ class compendium(object): | |
* `default_timeout`: Default seconds to wait for response for all API calling functions until raising `requests.exceptions.ReadTimeout` | ||
- default: `None` (no timeout) | ||
- type: integer, float, tuple (for connect and read timeouts) | ||
- notes: If an API calling function has a parameter `timeout`, it will overide this | ||
- notes: If an API calling function has a parameter `timeout`, it will override this | ||
""" | ||
|
||
def __init__(self, base_url: str="https://botw-compendium.herokuapp.com/api/v2", default_timeout: Union[int, float, None]=None): | ||
def __init__(self, base_url: str="https://botw-compendium.herokuapp.com/api/v2", default_timeout: Union[int, float, None]=None, master_mode: bool = False): | ||
self.api: api = api(base_url) | ||
self.default_timeout = default_timeout | ||
self.master_mode = master_mode | ||
if self.master_mode: self.master_api: api = api("https://botw-compendium.herokuapp.com/api/v2/master_mode") | ||
|
||
def get_entry(self, entry: types.entry, timeout: types.timeout=None) -> dict: | ||
""" | ||
|
@@ -42,10 +44,11 @@ def get_entry(self, entry: types.entry, timeout: types.timeout=None) -> dict: | |
timeout = self.default_timeout | ||
|
||
res: dict = self.api.request(f"/entry/{entry}", timeout) | ||
if res == {}: | ||
raise exceptions.NoEntryError(entry) | ||
if not res: | ||
res = self.master_api.request(f"/entry/{entry}", timeout) | ||
shaunikm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return res | ||
if res: return res | ||
raise exceptions.NoEntryError(entry) | ||
|
||
def get_category(self, category: str, timeout: types.timeout=None) -> Union[dict, list]: | ||
""" | ||
|
@@ -70,7 +73,10 @@ def get_category(self, category: str, timeout: types.timeout=None) -> Union[dict | |
if category not in ["creatures", "equipment", "materials", "monsters", "treasure"]: | ||
raise exceptions.NoCategoryError(category) | ||
|
||
return self.api.request(f"/category/{category}", timeout) | ||
if self.master_mode and category == "monsters": | ||
return self.api.request("/category/monsters", timeout) + api_req(self.master_api.base_url, timeout) | ||
shaunikm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else: | ||
return self.api.request(f"/category/{category}", timeout) | ||
|
||
def get_all(self, timeout: types.timeout=None) -> Union[dict, list]: | ||
""" | ||
|
@@ -88,7 +94,12 @@ def get_all(self, timeout: types.timeout=None) -> Union[dict, list]: | |
if not timeout: | ||
timeout = self.default_timeout | ||
|
||
return api_req(self.api.base_url, timeout) | ||
if self.master_mode: | ||
res: dict = api_req(self.api.base_url, timeout) | ||
res["monsters"] += api_req(self.master_api.base_url, timeout) | ||
shaunikm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return res | ||
else: | ||
return api_req(self.api.base_url, timeout) | ||
|
||
def get_image(self, entry: types.entry) -> objects.entry_image: | ||
""" | ||
|
@@ -102,4 +113,31 @@ def get_image(self, entry: types.entry) -> objects.entry_image: | |
- type: `objects.entry_image` | ||
""" | ||
|
||
return objects.entry_image(self.get_entry(entry), self.api) | ||
if self._is_master_mode_entry(entry): return objects.entry_image(self.get_entry(entry), self.master_api) | ||
else: return objects.entry_image(self.get_entry(entry), self.api) | ||
shaunikm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def _is_master_mode_entry(self, entry: types.entry, timeout: types.timeout=None): | ||
""" | ||
Determines if an entry is from master mode or not. | ||
|
||
Parameters: | ||
* `entry`: The ID or name of the entry. | ||
- type: str, int | ||
* `timeout`: Seconds to wait for response until raising `requests.exceptions.ReadTimeout` | ||
- default: `compendium.default_timeout` | ||
- type: int, float, tuple (for connect and read timeouts) | ||
|
||
Returns: Whether an entry is from master mode or not | ||
- type: `objects.entry_image` | ||
""" | ||
|
||
if not timeout: | ||
timeout = self.default_timeout | ||
|
||
res: dict = self.api.request(f"/entry/{entry}", timeout) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I plan on adding a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other than using local data, I don’t see any way to determine if an entry is from |
||
if res: return False | ||
|
||
res = self.master_api.request(f"/entry/{entry}", timeout) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Too many requests? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, I don’t see a way to minimize the number of requests we have here other than using local data. |
||
if res: return True | ||
|
||
else: raise exceptions.NoEntryError(entry) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
from typing import Union, Any | ||
from requests import get | ||
|
||
|
||
class types: | ||
entry = Union[int, str] | ||
timeout = Union[int, float, None] | ||
|
||
def api_req(url: str, timeout: types.timeout=None) -> Union[list, dict]: return get(url, timeout=timeout).json()["data"] | ||
|
||
class api(): | ||
def api_req(url: str, timeout: types.timeout = None) -> Union[list, dict]: return get(url, timeout=timeout).json()[ | ||
"data"] | ||
|
||
|
||
class api: | ||
def __init__(self, base_url: str): self.base_url = base_url | ||
|
||
def endpoint(self, target_endpoint: str) -> str: return f"{self.base_url}{target_endpoint}" | ||
def request(self, endpoint: str, timeout: types.timeout=None) -> Any: return api_req(f"{self.endpoint(endpoint)}", timeout) | ||
|
||
def request(self, endpoint: str, timeout: types.timeout = None) -> Any: return api_req(f"{self.endpoint(endpoint)}", | ||
timeout) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
master_mode
should be passed into the functions rather than to the class, because we do not want to create another object formaster_mode
data.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel that having
master_mode
passed when the class is initialized would remove the need for a redundant parameter in every function of the class.