Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bugout/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

__email__ = "engineering@bugout.dev"
__license__ = "MIT"
__version__ = "0.2.10"
__version__ = "0.2.11"

__all__ = (
"__author__",
Expand Down
126 changes: 126 additions & 0 deletions bugout/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,132 @@ def delete_entries_tags(
**kwargs,
)

# Entity
def create_entity(
self,
token: Union[str, uuid.UUID],
journal_id: Union[str, uuid.UUID],
title: str,
address: str,
blockchain: str,
required_fields: List[Dict[str, Union[str, bool, int, list]]] = [],
secondary_fields: Dict[str, Any] = {},
timeout: float = REQUESTS_TIMEOUT,
auth_type: str = data.AuthType.bearer.name,
**kwargs: Dict[str, Any],
) -> data.BugoutJournalEntity:
self.journal.timeout = timeout
return self.journal.create_entity(
token=token,
journal_id=journal_id,
title=title,
address=address,
blockchain=blockchain,
required_fields=required_fields,
secondary_fields=secondary_fields,
auth_type=data.AuthType[auth_type],
**kwargs,
)

def create_entities_pack(
self,
token: Union[str, uuid.UUID],
journal_id: Union[str, uuid.UUID],
entities: List[Dict[str, Any]],
timeout: float = REQUESTS_TIMEOUT,
auth_type: str = data.AuthType.bearer.name,
**kwargs: Dict[str, Any],
) -> data.BugoutJournalEntities:
self.journal.timeout = timeout

return self.journal.create_entities_pack(
token=token,
journal_id=journal_id,
entities=[data.BugoutJournalEntityRequest(**entity) for entity in entities],
auth_type=data.AuthType[auth_type],
**kwargs,
)

def get_entity(
self,
token: Union[str, uuid.UUID],
journal_id: Union[str, uuid.UUID],
entity_id: Union[str, uuid.UUID],
timeout: float = REQUESTS_TIMEOUT,
auth_type: str = data.AuthType.bearer.name,
**kwargs: Dict[str, Any],
) -> data.BugoutJournalEntity:
self.journal.timeout = timeout
return self.journal.get_entity(
token=token,
journal_id=journal_id,
entity_id=entity_id,
auth_type=data.AuthType[auth_type],
**kwargs,
)

def get_entities(
self,
token: Union[str, uuid.UUID],
journal_id: Union[str, uuid.UUID],
timeout: float = REQUESTS_TIMEOUT,
auth_type: str = data.AuthType.bearer.name,
**kwargs: Dict[str, Any],
) -> data.BugoutJournalEntities:
self.journal.timeout = timeout
return self.journal.get_entities(
token=token,
journal_id=journal_id,
auth_type=data.AuthType[auth_type],
**kwargs,
)

def update_entity(
self,
token: Union[str, uuid.UUID],
journal_id: Union[str, uuid.UUID],
entity_id: Union[str, uuid.UUID],
title: str,
address: str,
blockchain: str,
required_fields: List[Dict[str, Union[str, bool, int, list]]] = [],
secondary_fields: Dict[str, Any] = {},
timeout: float = REQUESTS_TIMEOUT,
auth_type: str = data.AuthType.bearer.name,
**kwargs: Dict[str, Any],
) -> data.BugoutJournalEntity:
self.journal.timeout = timeout
return self.journal.update_entity(
token=token,
journal_id=journal_id,
entity_id=entity_id,
title=title,
address=address,
blockchain=blockchain,
required_fields=required_fields,
secondary_fields=secondary_fields,
auth_type=data.AuthType[auth_type],
**kwargs,
)

def delete_entity(
self,
token: Union[str, uuid.UUID],
journal_id: Union[str, uuid.UUID],
entity_id: Union[str, uuid.UUID],
timeout: float = REQUESTS_TIMEOUT,
auth_type: str = data.AuthType.bearer.name,
**kwargs: Dict[str, Any],
) -> data.BugoutJournalEntity:
self.journal.timeout = timeout
return self.journal.delete_entity(
token=token,
journal_id=journal_id,
entity_id=entity_id,
auth_type=data.AuthType[auth_type],
**kwargs,
)

# Search
def search(
self,
Expand Down
52 changes: 50 additions & 2 deletions bugout/data.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import uuid
from datetime import datetime
from enum import Enum, unique
from typing import Any, Dict, List, Optional, Set
from typing import Any, Dict, List, Optional, Set, Union

from pydantic import BaseModel, Field
from pydantic import BaseModel, Extra, Field, root_validator


@unique
Expand Down Expand Up @@ -51,6 +51,11 @@ class JournalTypes(Enum):
HUMBUG = "humbug"


class EntryRepresentationTypes(Enum):
ENTRY = "entry"
ENTITY = "entity"


class BugoutUser(BaseModel):
id: uuid.UUID = Field(alias="user_id")
username: str
Expand Down Expand Up @@ -290,3 +295,46 @@ class BugoutHumbugIntegrationsList(BaseModel):

class BugoutSearchResultWithEntryID(BugoutSearchResult):
id: str


class BugoutJournalEntityRequest(BaseModel, extra=Extra.allow):
title: str
address: str
blockchain: str
required_fields: List[Dict[str, Union[str, bool, int, list]]] = Field(
default_factory=list
)

extra: Dict[str, Any]

@root_validator(pre=True)
def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]:
all_required_field_names = {
field.alias for field in cls.__fields__.values() if field.alias != "extra"
}

extra: Dict[str, Any] = {}
for field_name in list(values):
if field_name not in all_required_field_names:
extra[field_name] = values.pop(field_name)
values["extra"] = extra
return values


class BugoutJournalEntity(BaseModel):
id: uuid.UUID
journal_id: uuid.UUID
journal_url: Optional[str] = None
title: Optional[str] = None
address: Optional[str] = None
blockchain: Optional[str] = None
required_fields: Optional[List[Dict[str, Any]]] = None
secondary_fields: Optional[Dict[str, Any]] = None
created_at: Optional[datetime] = None
updated_at: Optional[datetime] = None

locked_by: Optional[str] = None


class BugoutJournalEntities(BaseModel):
entities: List[BugoutJournalEntity] = Field(default_factory=list)
151 changes: 149 additions & 2 deletions bugout/journal.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import json
import uuid
from enum import Enum
import json
from typing import Any, Dict, List, Optional, Union

from .calls import make_request
from .data import (
AuthType,
BugoutJournal,
BugoutJournalEntities,
BugoutJournalEntity,
BugoutJournalEntityRequest,
BugoutJournalEntries,
BugoutJournalEntriesRequest,
BugoutJournalEntriesTagsRequest,
BugoutJournalEntry,
BugoutJournalEntryContent,
BugoutJournalEntryTags,
BugoutJournalEntriesTagsRequest,
BugoutJournalPermissions,
BugoutJournals,
BugoutJournalScopeSpecs,
Expand Down Expand Up @@ -571,6 +574,150 @@ def delete_entries_tags(
entries=[BugoutJournalEntry(**entry) for entry in result]
)

# Entity module
def create_entity(
self,
token: Union[str, uuid.UUID],
journal_id: Union[str, uuid.UUID],
title: str,
address: str,
blockchain: str,
required_fields: List[Dict[str, Union[str, bool, int, list]]] = [],
secondary_fields: Dict[str, Any] = {},
auth_type: AuthType = AuthType.bearer,
**kwargs: Dict[str, Any],
) -> BugoutJournalEntity:
path = f"journals/{journal_id}/entities"
json = {
"title": title,
"address": address,
"blockchain": blockchain,
"required_fields": required_fields,
**secondary_fields,
}
headers = {
"Authorization": f"{auth_type.value} {token}",
}
if "headers" in kwargs.keys():
headers.update(kwargs["headers"])
result = self._call(method=Method.post, path=path, headers=headers, json=json)
return BugoutJournalEntity(**result)

def create_entities_pack(
self,
token: Union[str, uuid.UUID],
journal_id: Union[str, uuid.UUID],
entities: List[BugoutJournalEntityRequest],
auth_type: AuthType = AuthType.bearer,
**kwargs: Dict[str, Any],
) -> BugoutJournalEntities:
path = f"journals/{journal_id}/entities/bulk"
headers = {
"Authorization": f"{auth_type.value} {token}",
}
if "headers" in kwargs.keys():
headers.update(kwargs["headers"])
json = {
"entities": [
{
"title": entity.title,
"address": entity.address,
"blockchain": entity.blockchain,
"required_fields": entity.required_fields,
**entity.extra,
}
for entity in entities
]
}
result = self._call(method=Method.post, path=path, headers=headers, json=json)
return BugoutJournalEntities(**result)

def get_entity(
self,
token: Union[str, uuid.UUID],
journal_id: Union[str, uuid.UUID],
entity_id: Union[str, uuid.UUID],
auth_type: AuthType = AuthType.bearer,
**kwargs: Dict[str, Any],
) -> BugoutJournalEntity:
path = f"journals/{journal_id}/entities/{entity_id}"
headers = {
"Authorization": f"{auth_type.value} {token}",
}
if "headers" in kwargs.keys():
headers.update(kwargs["headers"])
result = self._call(method=Method.get, path=path, headers=headers)
return BugoutJournalEntity(**result)

def get_entities(
self,
token: Union[str, uuid.UUID],
journal_id: Union[str, uuid.UUID],
auth_type: AuthType = AuthType.bearer,
**kwargs: Dict[str, Any],
) -> BugoutJournalEntities:
path = f"journals/{journal_id}/entities"
headers = {
"Authorization": f"{auth_type.value} {token}",
}
if "headers" in kwargs.keys():
headers.update(kwargs["headers"])
result = self._call(method=Method.get, path=path, headers=headers)
return BugoutJournalEntities(**result)

def update_entity(
self,
token: Union[str, uuid.UUID],
journal_id: Union[str, uuid.UUID],
entity_id: Union[str, uuid.UUID],
title: str,
address: str,
blockchain: str,
required_fields: List[Dict[str, Union[str, bool, int, list]]] = [],
secondary_fields: Dict[str, Any] = {},
auth_type: AuthType = AuthType.bearer,
**kwargs: Dict[str, Any],
) -> BugoutJournalEntity:
path = f"journals/{journal_id}/entities/{entity_id}"
params: Dict[str, str] = {}
json = {
"title": title,
"address": address,
"blockchain": blockchain,
"required_fields": required_fields,
**secondary_fields,
}
headers = {
"Authorization": f"{auth_type.value} {token}",
}
if "headers" in kwargs.keys():
headers.update(kwargs["headers"])
result = self._call(
method=Method.put,
path=path,
headers=headers,
json=json,
params=params,
)
return BugoutJournalEntity(**result)

def delete_entity(
self,
token: Union[str, uuid.UUID],
journal_id: Union[str, uuid.UUID],
entity_id: Union[str, uuid.UUID],
auth_type: AuthType = AuthType.bearer,
**kwargs: Dict[str, Any],
) -> BugoutJournalEntity:
path = f"journals/{journal_id}/entities/{entity_id}"
headers = {
"Authorization": f"{auth_type.value} {token}",
}
if "headers" in kwargs.keys():
headers.update(kwargs["headers"])
result = self._call(method=Method.delete, path=path, headers=headers)
return BugoutJournalEntity(**result)

# Search module
def search(
self,
Expand Down
Loading