Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

Commit

Permalink
added search and file download functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
firelilith committed Jul 8, 2021
1 parent 0dbb6f3 commit 4f8b12a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
9 changes: 9 additions & 0 deletions pykanka/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,12 @@ def all_events(self) -> typing.Dict[typing.Tuple[str, int], "ct.Event"]:

def all_abilities(self) -> typing.Dict[typing.Tuple[str, int], "ct.Ability"]:
return self._get_all_of_type(f"{self.campaign_base_url}abilities", ct.Ability)

def search(self, name: str, all_results: bool = False, results: int = 5) -> list["ent.Entity"]:
response = self.request_get(f"{self.campaign_base_url}search/{name}").json()["data"]

res = []
for entry, _ in zip(response, range(results)):
res.append(self.get_entity(entry["entity_id"]))

return res
20 changes: 17 additions & 3 deletions pykanka/child_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,15 @@ def _prepare_post(self, json_data: str, **kwargs): # implement support for imag
existing_values = self._get_post_values()
existing_values.update(values)

if "name" not in existing_values.keys():
raise ValueError("'name' is a required field, but is missing")
self._validate_parameters(existing_values, files)

return existing_values, files

@staticmethod
def _validate_parameters(values, files):
if "name" not in values.keys():
raise ValueError("'name' is a required field, but is missing")

def _get_post_values(self):
values = dict()

Expand All @@ -168,12 +172,15 @@ def _get_post_values(self):

return values

def get_image(self):
return self.client.request_get(self.data.image_full, stream=True)


class Location(GenericChildType):
"""A class representing a location child contained within an Entity."""

# keys accepted by POST and also delivered by GET as per API documentation
_possible_keys = ["name", "type", "parent_location_id", "tags", "is_private", "image_full", "map_full", "is_map_private"]
_possible_keys = ["name", "type", "parent_location_id", "tags", "is_private", "image_full", "map", "is_map_private"]
# keys called differently in GET compared to POST as per API documentation, format: (get_version, post_version)
_key_replacer = [("image_full", "image_url"), ("map", "map_url")]
# fields that accept stream object, not yet supported in API 1.0
Expand Down Expand Up @@ -439,6 +446,13 @@ def __init__(self, client: "pykanka.KankaClient", parent: "pykanka.entities.Enti

self.base_url = f"{self.client.campaign_base_url}maps/"

@staticmethod
def _validate_parameters(values: set, files: set):
if "name" not in values:
raise ValueError("'name' is a required field, but is missing")
if "image" not in files and "image_url" not in values:
raise ValueError("either 'image' or 'image_url' is required, but both are missing")


class Tag(GenericChildType):
"""A class representing a Tag child contained within an Entity."""
Expand Down
13 changes: 5 additions & 8 deletions pykanka/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def __init__(self, client: "pykanka.KankaClient", child=None):

self.client = client
self.data = self.EntityData()
self.entity_id = None

self._child = child

Expand Down Expand Up @@ -77,7 +76,6 @@ def from_id(cls, client: "pykanka.KankaClient", entity_id: int, child=None) -> "
response_data.pop("child")

obj.data = obj.EntityData(response_data)
obj.entity_id = obj.data.id

if not obj.child:
obj._child = obj._build_child_from_json(child_json=child_data, child_type=obj.data.type,)
Expand All @@ -102,15 +100,14 @@ def from_json(cls, client: "pykanka.KankaClient", content: typing.Union[str, dic
obj = Entity(client)

obj.data = cls.EntityData(val=content)
obj.entity_id = obj.data.id

if child_data:
obj._child = obj._build_child_from_json(child_json=child_data, child_type=obj.data.type)

return obj

def _request_data(self):
response = self.client.request_get(f"{self.client.campaign_base_url}entities/{self.entity_id}")
response = self.client.request_get(f"{self.client.campaign_base_url}entities/{self.data.id}")

if not response.ok:
raise ResponseNotOkError(f"Response not OK, code {response.status_code}: {response.text}")
Expand All @@ -121,10 +118,9 @@ def _build_child_from_json(self, child_json: dict, child_type: str):
@classmethod
def _get_child_class(cls, child_type: str):
type_dictionary = dict(location=ct.Location,
character=ct.Character)
""",
character=ct.Character,
family=ct.Family,
organisation=ct.Organization,
organisation=ct.Organisation,
timeline=ct.Timeline,
race=ct.Race,
note=ct.Note,
Expand All @@ -135,5 +131,6 @@ def _get_child_class(cls, child_type: str):
item=ct.Item,
event=ct.Event,
ability=ct.Ability
)"""
)

return type_dictionary[child_type]

0 comments on commit 4f8b12a

Please sign in to comment.