Skip to content

Commit 4ec7483

Browse files
authored
Merge pull request #30 from bugout-dev/app-resources
Application and resources handlers
2 parents 060945a + 6528df8 commit 4ec7483

File tree

6 files changed

+265
-2
lines changed

6 files changed

+265
-2
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.12"
10+
__version__ = "0.1.13"
1111

1212
__all__ = (
1313
"__author__",

bugout/app.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .group import Group
77
from .humbug import Humbug
88
from .journal import Journal
9+
from .resource import Resource
910
from .user import User
1011
from .settings import BUGOUT_BROOD_URL, BUGOUT_SPIRE_URL, REQUESTS_TIMEOUT
1112

@@ -29,6 +30,7 @@ def __init__(
2930
self.group = Group(self.brood_api_url)
3031
self.humbug = Humbug(self.spire_api_url)
3132
self.journal = Journal(self.spire_api_url)
33+
self.resource = Resource(self.brood_api_url)
3234

3335
@property
3436
def brood_url(self):
@@ -293,6 +295,87 @@ def delete_group(
293295
self.group.timeout = timeout
294296
return self.group.delete_group(token=token, group_id=group_id)
295297

298+
# Application handlers
299+
def create_application(
300+
self,
301+
token: Union[str, uuid.UUID],
302+
name: str,
303+
description: str,
304+
group_id: Union[str, uuid.UUID],
305+
timeout: float = REQUESTS_TIMEOUT,
306+
) -> data.BugoutApplication:
307+
self.group.timeout = timeout
308+
return self.group.create_application(
309+
token=token, name=name, description=description, group_id=group_id
310+
)
311+
312+
def get_application(
313+
self,
314+
token: Union[str, uuid.UUID],
315+
application_id: Union[str, uuid.UUID],
316+
timeout: float = REQUESTS_TIMEOUT,
317+
) -> data.BugoutApplication:
318+
self.group.timeout = timeout
319+
return self.group.get_application(token=token, application_id=application_id)
320+
321+
def list_applications(
322+
self,
323+
token: Union[str, uuid.UUID],
324+
group_id: Optional[Union[str, uuid.UUID]] = None,
325+
timeout: float = REQUESTS_TIMEOUT,
326+
) -> data.BugoutApplications:
327+
self.group.timeout = timeout
328+
return self.group.list_applications(token=token, group_id=group_id)
329+
330+
def delete_application(
331+
self,
332+
token: Union[str, uuid.UUID],
333+
application_id: Union[str, uuid.UUID],
334+
timeout: float = REQUESTS_TIMEOUT,
335+
) -> data.BugoutApplication:
336+
self.group.timeout = timeout
337+
return self.group.delete_application(token=token, application_id=application_id)
338+
339+
# Resource handlers
340+
def create_resource(
341+
self,
342+
token: Union[str, uuid.UUID],
343+
application_id: Union[str, uuid.UUID],
344+
resource_data: Dict[str, Any],
345+
timeout: float = REQUESTS_TIMEOUT,
346+
) -> data.BugoutResource:
347+
self.resource.timeout = timeout
348+
return self.resource.create_resource(
349+
token=token, application_id=application_id, resource_data=resource_data
350+
)
351+
352+
def get_resource(
353+
self,
354+
token: Union[str, uuid.UUID],
355+
resource_id: Union[str, uuid.UUID],
356+
timeout: float = REQUESTS_TIMEOUT,
357+
) -> data.BugoutResource:
358+
self.resource.timeout = timeout
359+
return self.resource.get_resource(token=token, resource_id=resource_id)
360+
361+
def list_resources(
362+
self,
363+
token: Union[str, uuid.UUID],
364+
params: Optional[Dict[str, Any]] = None,
365+
timeout: float = REQUESTS_TIMEOUT,
366+
) -> data.BugoutResources:
367+
self.resource.timeout = timeout
368+
return self.resource.list_resources(token=token, params=params)
369+
370+
def delete_resource(
371+
self,
372+
token: Union[str, uuid.UUID],
373+
resource_id: Union[str, uuid.UUID],
374+
timeout: float = REQUESTS_TIMEOUT,
375+
) -> data.BugoutResource:
376+
self.resource.timeout = timeout
377+
return self.resource.delete_resource(token=token, resource_id=resource_id)
378+
296379
# Journal scopes handlers
297380
def list_scopes(
298381
self, token: Union[str, uuid.UUID], api: str, timeout: float = REQUESTS_TIMEOUT

bugout/data.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,29 @@ class BugoutGroupMembers(BaseModel):
9797
users: List[BugoutUserShort]
9898

9999

100+
class BugoutApplication(BaseModel):
101+
id: uuid.UUID
102+
name: str
103+
description: Optional[str] = None
104+
group_id: uuid.UUID
105+
106+
107+
class BugoutApplications(BaseModel):
108+
applications: List[BugoutApplication]
109+
110+
111+
class BugoutResource(BaseModel):
112+
id: uuid.UUID
113+
application_id: str
114+
resource_data: Dict[str, Any]
115+
created_at: datetime
116+
updated_at: datetime
117+
118+
119+
class BugoutResources(BaseModel):
120+
resources: List[BugoutResource]
121+
122+
100123
class BugoutJournalPermission(BaseModel):
101124
holder_type: HolderType
102125
holder_id: str

bugout/group.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
BugoutGroupUser,
1010
BugoutGroupMembers,
1111
BugoutUserGroups,
12+
BugoutApplication,
13+
BugoutApplications,
1214
)
1315
from .settings import REQUESTS_TIMEOUT
1416

@@ -194,3 +196,70 @@ def delete_group(
194196
method=Method.delete, path=delete_group_path, headers=headers
195197
)
196198
return BugoutGroup(**result)
199+
200+
def create_application(
201+
self,
202+
token: Union[str, uuid.UUID],
203+
name: str,
204+
description: str,
205+
group_id: Union[str, uuid.UUID],
206+
) -> BugoutApplication:
207+
applications_path = "applications"
208+
headers = {
209+
"Authorization": f"Bearer {token}",
210+
}
211+
data = {
212+
"name": name,
213+
"description": description,
214+
"group_id": group_id,
215+
}
216+
result = self._call(
217+
method=Method.post, path=applications_path, headers=headers, data=data
218+
)
219+
return BugoutApplication(**result)
220+
221+
def get_application(
222+
self,
223+
token: Union[str, uuid.UUID],
224+
application_id: Union[str, uuid.UUID],
225+
) -> BugoutApplication:
226+
applications_path = f"applications/{application_id}"
227+
headers = {
228+
"Authorization": f"Bearer {token}",
229+
}
230+
result = self._call(method=Method.get, path=applications_path, headers=headers)
231+
return BugoutApplication(**result)
232+
233+
def list_applications(
234+
self,
235+
token: Union[str, uuid.UUID],
236+
group_id: Optional[Union[str, uuid.UUID]] = None,
237+
) -> BugoutApplications:
238+
applications_path = "applications"
239+
headers = {
240+
"Authorization": f"Bearer {token}",
241+
}
242+
query_params = {
243+
"group_id": group_id,
244+
}
245+
result = self._call(
246+
method=Method.get,
247+
path=applications_path,
248+
params=query_params,
249+
headers=headers,
250+
)
251+
return BugoutApplications(**result)
252+
253+
def delete_application(
254+
self,
255+
token: Union[str, uuid.UUID],
256+
application_id: Union[str, uuid.UUID],
257+
) -> BugoutApplication:
258+
applications_path = f"applications/{application_id}"
259+
headers = {
260+
"Authorization": f"Bearer {token}",
261+
}
262+
result = self._call(
263+
method=Method.delete, path=applications_path, headers=headers
264+
)
265+
return BugoutApplication(**result)

bugout/resource.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
from typing import Any, Dict, Optional, Union
2+
import uuid
3+
4+
from .calls import make_request, InvalidUrlSpec
5+
from .data import Method, BugoutResource, BugoutResources
6+
from .settings import REQUESTS_TIMEOUT
7+
8+
9+
class ResourceInvalidParameters(ValueError):
10+
"""
11+
Raised when operations are applied to a resource but invalid parameters are provided.
12+
"""
13+
14+
15+
class Resource:
16+
"""
17+
Represent a resources from Bugout.
18+
"""
19+
20+
def __init__(
21+
self, url: Optional[str] = None, timeout: float = REQUESTS_TIMEOUT
22+
) -> None:
23+
if url is None:
24+
raise InvalidUrlSpec("Invalid brood url specified")
25+
self.url = url
26+
self.timeout = timeout
27+
28+
def _call(self, method: Method, path: str, **kwargs):
29+
url = f"{self.url.rstrip('/')}/{path.rstrip('/')}"
30+
result = make_request(method=method, url=url, timeout=self.timeout, **kwargs)
31+
return result
32+
33+
def create_resource(
34+
self,
35+
token: Union[str, uuid.UUID],
36+
application_id: Union[str, uuid.UUID],
37+
resource_data: Dict[str, Any],
38+
) -> BugoutResource:
39+
resources_path = "resources"
40+
headers = {
41+
"Authorization": f"Bearer {token}",
42+
}
43+
json_data = {
44+
"application_id": application_id,
45+
"resource_data": resource_data,
46+
}
47+
result = self._call(
48+
method=Method.post, path=resources_path, headers=headers, json=json_data
49+
)
50+
return BugoutResource(**result)
51+
52+
def get_resource(
53+
self,
54+
token: Union[str, uuid.UUID],
55+
resource_id: Union[str, uuid.UUID],
56+
) -> BugoutResource:
57+
resources_path = f"resources/{resource_id}"
58+
headers = {
59+
"Authorization": f"Bearer {token}",
60+
}
61+
result = self._call(method=Method.get, path=resources_path, headers=headers)
62+
return BugoutResource(**result)
63+
64+
def list_resources(
65+
self,
66+
token: Union[str, uuid.UUID],
67+
params: Optional[Dict[str, Any]] = None,
68+
) -> BugoutResources:
69+
resources_path = "resources"
70+
headers = {
71+
"Authorization": f"Bearer {token}",
72+
}
73+
result = self._call(
74+
method=Method.get, path=resources_path, params=params, headers=headers
75+
)
76+
return BugoutResources(**result)
77+
78+
def delete_resource(
79+
self,
80+
token: Union[str, uuid.UUID],
81+
resource_id: Union[str, uuid.UUID],
82+
) -> BugoutResource:
83+
resources_path = f"resources/{resource_id}"
84+
headers = {
85+
"Authorization": f"Bearer {token}",
86+
}
87+
result = self._call(method=Method.delete, path=resources_path, headers=headers)
88+
return BugoutResource(**result)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
zip_safe=False,
4141
install_requires=["pydantic>=1.6", "requests"],
4242
extras_require={
43-
"dev": ["black", "mypy"],
43+
"dev": ["black", "mypy", "types-requests"],
4444
"distribute": ["setuptools", "twine", "wheel"],
4545
},
4646
entry_points={

0 commit comments

Comments
 (0)