Skip to content

Commit 1c3e5da

Browse files
authored
Merge pull request #51 from bugout-dev/entity-base
Journal entity support
2 parents 3a3a0b0 + 6dcaeb4 commit 1c3e5da

File tree

5 files changed

+328
-6
lines changed

5 files changed

+328
-6
lines changed

bugout/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
__email__ = "engineering@bugout.dev"
99
__license__ = "MIT"
10-
__version__ = "0.2.10"
10+
__version__ = "0.2.11"
1111

1212
__all__ = (
1313
"__author__",

bugout/app.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,132 @@ def delete_entries_tags(
897897
**kwargs,
898898
)
899899

900+
# Entity
901+
def create_entity(
902+
self,
903+
token: Union[str, uuid.UUID],
904+
journal_id: Union[str, uuid.UUID],
905+
title: str,
906+
address: str,
907+
blockchain: str,
908+
required_fields: List[Dict[str, Union[str, bool, int, list]]] = [],
909+
secondary_fields: Dict[str, Any] = {},
910+
timeout: float = REQUESTS_TIMEOUT,
911+
auth_type: str = data.AuthType.bearer.name,
912+
**kwargs: Dict[str, Any],
913+
) -> data.BugoutJournalEntity:
914+
self.journal.timeout = timeout
915+
return self.journal.create_entity(
916+
token=token,
917+
journal_id=journal_id,
918+
title=title,
919+
address=address,
920+
blockchain=blockchain,
921+
required_fields=required_fields,
922+
secondary_fields=secondary_fields,
923+
auth_type=data.AuthType[auth_type],
924+
**kwargs,
925+
)
926+
927+
def create_entities_pack(
928+
self,
929+
token: Union[str, uuid.UUID],
930+
journal_id: Union[str, uuid.UUID],
931+
entities: List[Dict[str, Any]],
932+
timeout: float = REQUESTS_TIMEOUT,
933+
auth_type: str = data.AuthType.bearer.name,
934+
**kwargs: Dict[str, Any],
935+
) -> data.BugoutJournalEntities:
936+
self.journal.timeout = timeout
937+
938+
return self.journal.create_entities_pack(
939+
token=token,
940+
journal_id=journal_id,
941+
entities=[data.BugoutJournalEntityRequest(**entity) for entity in entities],
942+
auth_type=data.AuthType[auth_type],
943+
**kwargs,
944+
)
945+
946+
def get_entity(
947+
self,
948+
token: Union[str, uuid.UUID],
949+
journal_id: Union[str, uuid.UUID],
950+
entity_id: Union[str, uuid.UUID],
951+
timeout: float = REQUESTS_TIMEOUT,
952+
auth_type: str = data.AuthType.bearer.name,
953+
**kwargs: Dict[str, Any],
954+
) -> data.BugoutJournalEntity:
955+
self.journal.timeout = timeout
956+
return self.journal.get_entity(
957+
token=token,
958+
journal_id=journal_id,
959+
entity_id=entity_id,
960+
auth_type=data.AuthType[auth_type],
961+
**kwargs,
962+
)
963+
964+
def get_entities(
965+
self,
966+
token: Union[str, uuid.UUID],
967+
journal_id: Union[str, uuid.UUID],
968+
timeout: float = REQUESTS_TIMEOUT,
969+
auth_type: str = data.AuthType.bearer.name,
970+
**kwargs: Dict[str, Any],
971+
) -> data.BugoutJournalEntities:
972+
self.journal.timeout = timeout
973+
return self.journal.get_entities(
974+
token=token,
975+
journal_id=journal_id,
976+
auth_type=data.AuthType[auth_type],
977+
**kwargs,
978+
)
979+
980+
def update_entity(
981+
self,
982+
token: Union[str, uuid.UUID],
983+
journal_id: Union[str, uuid.UUID],
984+
entity_id: Union[str, uuid.UUID],
985+
title: str,
986+
address: str,
987+
blockchain: str,
988+
required_fields: List[Dict[str, Union[str, bool, int, list]]] = [],
989+
secondary_fields: Dict[str, Any] = {},
990+
timeout: float = REQUESTS_TIMEOUT,
991+
auth_type: str = data.AuthType.bearer.name,
992+
**kwargs: Dict[str, Any],
993+
) -> data.BugoutJournalEntity:
994+
self.journal.timeout = timeout
995+
return self.journal.update_entity(
996+
token=token,
997+
journal_id=journal_id,
998+
entity_id=entity_id,
999+
title=title,
1000+
address=address,
1001+
blockchain=blockchain,
1002+
required_fields=required_fields,
1003+
secondary_fields=secondary_fields,
1004+
auth_type=data.AuthType[auth_type],
1005+
**kwargs,
1006+
)
1007+
1008+
def delete_entity(
1009+
self,
1010+
token: Union[str, uuid.UUID],
1011+
journal_id: Union[str, uuid.UUID],
1012+
entity_id: Union[str, uuid.UUID],
1013+
timeout: float = REQUESTS_TIMEOUT,
1014+
auth_type: str = data.AuthType.bearer.name,
1015+
**kwargs: Dict[str, Any],
1016+
) -> data.BugoutJournalEntity:
1017+
self.journal.timeout = timeout
1018+
return self.journal.delete_entity(
1019+
token=token,
1020+
journal_id=journal_id,
1021+
entity_id=entity_id,
1022+
auth_type=data.AuthType[auth_type],
1023+
**kwargs,
1024+
)
1025+
9001026
# Search
9011027
def search(
9021028
self,

bugout/data.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import uuid
22
from datetime import datetime
33
from enum import Enum, unique
4-
from typing import Any, Dict, List, Optional, Set
4+
from typing import Any, Dict, List, Optional, Set, Union
55

6-
from pydantic import BaseModel, Field
6+
from pydantic import BaseModel, Extra, Field, root_validator
77

88

99
@unique
@@ -51,6 +51,11 @@ class JournalTypes(Enum):
5151
HUMBUG = "humbug"
5252

5353

54+
class EntryRepresentationTypes(Enum):
55+
ENTRY = "entry"
56+
ENTITY = "entity"
57+
58+
5459
class BugoutUser(BaseModel):
5560
id: uuid.UUID = Field(alias="user_id")
5661
username: str
@@ -290,3 +295,46 @@ class BugoutHumbugIntegrationsList(BaseModel):
290295

291296
class BugoutSearchResultWithEntryID(BugoutSearchResult):
292297
id: str
298+
299+
300+
class BugoutJournalEntityRequest(BaseModel, extra=Extra.allow):
301+
title: str
302+
address: str
303+
blockchain: str
304+
required_fields: List[Dict[str, Union[str, bool, int, list]]] = Field(
305+
default_factory=list
306+
)
307+
308+
extra: Dict[str, Any]
309+
310+
@root_validator(pre=True)
311+
def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]:
312+
all_required_field_names = {
313+
field.alias for field in cls.__fields__.values() if field.alias != "extra"
314+
}
315+
316+
extra: Dict[str, Any] = {}
317+
for field_name in list(values):
318+
if field_name not in all_required_field_names:
319+
extra[field_name] = values.pop(field_name)
320+
values["extra"] = extra
321+
return values
322+
323+
324+
class BugoutJournalEntity(BaseModel):
325+
id: uuid.UUID
326+
journal_id: uuid.UUID
327+
journal_url: Optional[str] = None
328+
title: Optional[str] = None
329+
address: Optional[str] = None
330+
blockchain: Optional[str] = None
331+
required_fields: Optional[List[Dict[str, Any]]] = None
332+
secondary_fields: Optional[Dict[str, Any]] = None
333+
created_at: Optional[datetime] = None
334+
updated_at: Optional[datetime] = None
335+
336+
locked_by: Optional[str] = None
337+
338+
339+
class BugoutJournalEntities(BaseModel):
340+
entities: List[BugoutJournalEntity] = Field(default_factory=list)

bugout/journal.py

Lines changed: 149 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1+
import json
12
import uuid
23
from enum import Enum
3-
import json
44
from typing import Any, Dict, List, Optional, Union
55

66
from .calls import make_request
77
from .data import (
88
AuthType,
99
BugoutJournal,
10+
BugoutJournalEntities,
11+
BugoutJournalEntity,
12+
BugoutJournalEntityRequest,
1013
BugoutJournalEntries,
1114
BugoutJournalEntriesRequest,
15+
BugoutJournalEntriesTagsRequest,
1216
BugoutJournalEntry,
1317
BugoutJournalEntryContent,
1418
BugoutJournalEntryTags,
15-
BugoutJournalEntriesTagsRequest,
1619
BugoutJournalPermissions,
1720
BugoutJournals,
1821
BugoutJournalScopeSpecs,
@@ -571,6 +574,150 @@ def delete_entries_tags(
571574
entries=[BugoutJournalEntry(**entry) for entry in result]
572575
)
573576

577+
# Entity module
578+
def create_entity(
579+
self,
580+
token: Union[str, uuid.UUID],
581+
journal_id: Union[str, uuid.UUID],
582+
title: str,
583+
address: str,
584+
blockchain: str,
585+
required_fields: List[Dict[str, Union[str, bool, int, list]]] = [],
586+
secondary_fields: Dict[str, Any] = {},
587+
auth_type: AuthType = AuthType.bearer,
588+
**kwargs: Dict[str, Any],
589+
) -> BugoutJournalEntity:
590+
path = f"journals/{journal_id}/entities"
591+
json = {
592+
"title": title,
593+
"address": address,
594+
"blockchain": blockchain,
595+
"required_fields": required_fields,
596+
**secondary_fields,
597+
}
598+
headers = {
599+
"Authorization": f"{auth_type.value} {token}",
600+
}
601+
if "headers" in kwargs.keys():
602+
headers.update(kwargs["headers"])
603+
result = self._call(method=Method.post, path=path, headers=headers, json=json)
604+
return BugoutJournalEntity(**result)
605+
606+
def create_entities_pack(
607+
self,
608+
token: Union[str, uuid.UUID],
609+
journal_id: Union[str, uuid.UUID],
610+
entities: List[BugoutJournalEntityRequest],
611+
auth_type: AuthType = AuthType.bearer,
612+
**kwargs: Dict[str, Any],
613+
) -> BugoutJournalEntities:
614+
path = f"journals/{journal_id}/entities/bulk"
615+
headers = {
616+
"Authorization": f"{auth_type.value} {token}",
617+
}
618+
if "headers" in kwargs.keys():
619+
headers.update(kwargs["headers"])
620+
json = {
621+
"entities": [
622+
{
623+
"title": entity.title,
624+
"address": entity.address,
625+
"blockchain": entity.blockchain,
626+
"required_fields": entity.required_fields,
627+
**entity.extra,
628+
}
629+
for entity in entities
630+
]
631+
}
632+
result = self._call(method=Method.post, path=path, headers=headers, json=json)
633+
return BugoutJournalEntities(**result)
634+
635+
def get_entity(
636+
self,
637+
token: Union[str, uuid.UUID],
638+
journal_id: Union[str, uuid.UUID],
639+
entity_id: Union[str, uuid.UUID],
640+
auth_type: AuthType = AuthType.bearer,
641+
**kwargs: Dict[str, Any],
642+
) -> BugoutJournalEntity:
643+
path = f"journals/{journal_id}/entities/{entity_id}"
644+
headers = {
645+
"Authorization": f"{auth_type.value} {token}",
646+
}
647+
if "headers" in kwargs.keys():
648+
headers.update(kwargs["headers"])
649+
result = self._call(method=Method.get, path=path, headers=headers)
650+
return BugoutJournalEntity(**result)
651+
652+
def get_entities(
653+
self,
654+
token: Union[str, uuid.UUID],
655+
journal_id: Union[str, uuid.UUID],
656+
auth_type: AuthType = AuthType.bearer,
657+
**kwargs: Dict[str, Any],
658+
) -> BugoutJournalEntities:
659+
path = f"journals/{journal_id}/entities"
660+
headers = {
661+
"Authorization": f"{auth_type.value} {token}",
662+
}
663+
if "headers" in kwargs.keys():
664+
headers.update(kwargs["headers"])
665+
result = self._call(method=Method.get, path=path, headers=headers)
666+
return BugoutJournalEntities(**result)
667+
668+
def update_entity(
669+
self,
670+
token: Union[str, uuid.UUID],
671+
journal_id: Union[str, uuid.UUID],
672+
entity_id: Union[str, uuid.UUID],
673+
title: str,
674+
address: str,
675+
blockchain: str,
676+
required_fields: List[Dict[str, Union[str, bool, int, list]]] = [],
677+
secondary_fields: Dict[str, Any] = {},
678+
auth_type: AuthType = AuthType.bearer,
679+
**kwargs: Dict[str, Any],
680+
) -> BugoutJournalEntity:
681+
path = f"journals/{journal_id}/entities/{entity_id}"
682+
params: Dict[str, str] = {}
683+
json = {
684+
"title": title,
685+
"address": address,
686+
"blockchain": blockchain,
687+
"required_fields": required_fields,
688+
**secondary_fields,
689+
}
690+
headers = {
691+
"Authorization": f"{auth_type.value} {token}",
692+
}
693+
if "headers" in kwargs.keys():
694+
headers.update(kwargs["headers"])
695+
result = self._call(
696+
method=Method.put,
697+
path=path,
698+
headers=headers,
699+
json=json,
700+
params=params,
701+
)
702+
return BugoutJournalEntity(**result)
703+
704+
def delete_entity(
705+
self,
706+
token: Union[str, uuid.UUID],
707+
journal_id: Union[str, uuid.UUID],
708+
entity_id: Union[str, uuid.UUID],
709+
auth_type: AuthType = AuthType.bearer,
710+
**kwargs: Dict[str, Any],
711+
) -> BugoutJournalEntity:
712+
path = f"journals/{journal_id}/entities/{entity_id}"
713+
headers = {
714+
"Authorization": f"{auth_type.value} {token}",
715+
}
716+
if "headers" in kwargs.keys():
717+
headers.update(kwargs["headers"])
718+
result = self._call(method=Method.delete, path=path, headers=headers)
719+
return BugoutJournalEntity(**result)
720+
574721
# Search module
575722
def search(
576723
self,

0 commit comments

Comments
 (0)