Skip to content

Commit 23c14b3

Browse files
authored
Merge pull request #21 from bugout-dev/humbug-endpoints
Humbug endpoints
2 parents ef5b50a + 9daac5e commit 23c14b3

File tree

6 files changed

+96
-7
lines changed

6 files changed

+96
-7
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.1.7"
10+
__version__ = "0.1.8"
1111

1212
__all__ = (
1313
"__author__",

bugout/app.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from . import data
55
from .calls import ping
66
from .group import Group
7+
from .humbug import Humbug
78
from .journal import Journal
89
from .user import User
910
from .settings import BUGOUT_BROOD_URL, BUGOUT_SPIRE_URL, REQUESTS_TIMEOUT
@@ -26,6 +27,7 @@ def __init__(
2627

2728
self.user = User(self.brood_api_url)
2829
self.group = Group(self.brood_api_url)
30+
self.humbug = Humbug(self.spire_api_url)
2931
self.journal = Journal(self.spire_api_url)
3032

3133
@property
@@ -185,13 +187,15 @@ def get_user_tokens(
185187
token: Union[str, uuid.UUID],
186188
active: Optional[bool] = None,
187189
token_type: Optional[Union[str, data.TokenType]] = None,
190+
restricted: Optional[bool] = None,
188191
timeout: float = REQUESTS_TIMEOUT,
189192
) -> data.BugoutUserTokens:
190193
self.user.timeout = timeout
191194
return self.user.get_user_tokens(
192195
token=token,
193196
active=active,
194197
token_type=data.TokenType(token_type) if token_type is not None else None,
198+
restricted=restricted,
195199
)
196200

197201
# Group handlers
@@ -344,10 +348,18 @@ def delete_journal_scopes(
344348

345349
# Journal handlers
346350
def create_journal(
347-
self, token: Union[str, uuid.UUID], name: str, timeout: float = REQUESTS_TIMEOUT
351+
self,
352+
token: Union[str, uuid.UUID],
353+
name: str,
354+
journal_type: Optional[Union[str, data.JournalTypes]] = None,
355+
timeout: float = REQUESTS_TIMEOUT,
348356
) -> data.BugoutJournal:
349357
self.journal.timeout = timeout
350-
return self.journal.create_journal(token=token, name=name)
358+
if journal_type is None:
359+
journal_type = data.JournalTypes.DEFAULT
360+
return self.journal.create_journal(
361+
token=token, name=name, journal_type=data.JournalTypes(journal_type)
362+
)
351363

352364
def list_journals(
353365
self, token: Union[str, uuid.UUID], timeout: float = REQUESTS_TIMEOUT
@@ -547,3 +559,13 @@ def search(
547559
) -> data.BugoutSearchResults:
548560
self.journal.timeout = timeout
549561
return self.journal.search(token, journal_id, query, limit, offset, content)
562+
563+
# Humbug
564+
def get_humbug_integrations(
565+
self,
566+
token: Union[str, uuid.UUID],
567+
group_id: Optional[Union[str, uuid.UUID]] = None,
568+
timeout: float = REQUESTS_TIMEOUT,
569+
) -> data.BugoutHumbugIntegrationsList:
570+
self.humbug.timeout = timeout
571+
return self.humbug.get_humbug_integrations(token=token, group_id=group_id)

bugout/data.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ class HolderType(Enum):
3333
group = "group"
3434

3535

36+
class JournalTypes(Enum):
37+
DEFAULT = "default"
38+
HUMBUG = "humbug"
39+
40+
3641
class BugoutUser(BaseModel):
3742
id: uuid.UUID = Field(alias="user_id")
3843
username: str
@@ -171,3 +176,16 @@ class BugoutSearchResults(BaseModel):
171176
next_offset: Optional[int]
172177
max_score: float
173178
results: List[BugoutSearchResult]
179+
180+
181+
class BugoutHumbugIntegration(BaseModel):
182+
id: uuid.UUID
183+
group_id: uuid.UUID
184+
journal_id: uuid.UUID
185+
journal_name: Optional[str] = None
186+
created_at: datetime
187+
updated_at: datetime
188+
189+
190+
class BugoutHumbugIntegrationsList(BaseModel):
191+
integrations: List[BugoutHumbugIntegration] = Field(default_factory=list)

bugout/humbug.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from typing import Any, Dict, Optional, Union
2+
import uuid
3+
4+
from .calls import make_request, InvalidUrlSpec
5+
from .data import Method, Role, BugoutHumbugIntegrationsList
6+
from .settings import REQUESTS_TIMEOUT
7+
8+
9+
class Humbug:
10+
"""
11+
Represent a humbug from Bugout.
12+
"""
13+
14+
def __init__(
15+
self, url: Optional[str] = None, timeout: float = REQUESTS_TIMEOUT
16+
) -> None:
17+
if url is None:
18+
raise InvalidUrlSpec("Invalid spire url specified")
19+
self.url = url
20+
self.timeout = timeout
21+
22+
def _call(self, method: Method, path: str, **kwargs):
23+
url = f"{self.url.rstrip('/')}/{path.rstrip('/')}"
24+
result = make_request(method=method, url=url, timeout=self.timeout, **kwargs)
25+
return result
26+
27+
def get_humbug_integrations(
28+
self,
29+
token: Union[str, uuid.UUID],
30+
group_id: Optional[Union[str, uuid.UUID]] = None,
31+
) -> BugoutHumbugIntegrationsList:
32+
humbug_path = "humbug/integrations"
33+
headers = {
34+
"Authorization": f"Bearer {token}",
35+
}
36+
query_params = {}
37+
if group_id is not None:
38+
query_params.update({"group_id": group_id})
39+
result = self._call(
40+
method=Method.get, path=humbug_path, params=query_params, headers=headers
41+
)
42+
return BugoutHumbugIntegrationsList(**result)

bugout/journal.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
BugoutSearchResults,
1515
HolderType,
1616
Method,
17+
JournalTypes,
1718
)
1819
from .settings import REQUESTS_TIMEOUT
1920

@@ -107,11 +108,14 @@ def delete_journal_scopes(
107108
return BugoutJournalScopeSpecs(**result)
108109

109110
# Journal module
110-
def create_journal(self, token: Union[str, uuid.UUID], name: str) -> BugoutJournal:
111+
def create_journal(
112+
self,
113+
token: Union[str, uuid.UUID],
114+
name: str,
115+
journal_type: JournalTypes,
116+
) -> BugoutJournal:
111117
journal_path = "journals/"
112-
json = {
113-
"name": name,
114-
}
118+
json = {"name": name, "journal_type": journal_type.value}
115119
headers = {
116120
"Authorization": f"Bearer {token}",
117121
}

bugout/user.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ def get_user_tokens(
235235
token: Union[str, uuid.UUID],
236236
active: Optional[bool] = None,
237237
token_type: Optional[TokenType] = None,
238+
restricted: Optional[bool] = None,
238239
) -> BugoutUserTokens:
239240
get_user_tokens_path = "tokens"
240241
headers = {
@@ -245,6 +246,8 @@ def get_user_tokens(
245246
query_params.update({"active": str(int(active))})
246247
if token_type is not None:
247248
query_params.update({"token_type": token_type.value})
249+
if restricted is not None:
250+
query_params.update({"restricted": str(int(restricted))})
248251
result = self._call(
249252
method=Method.get,
250253
path=get_user_tokens_path,

0 commit comments

Comments
 (0)