Skip to content

Commit 2b6dc95

Browse files
authored
Merge pull request #31 from bugout-dev/resource-improvements
User create handler with application_id and same for resources list
2 parents 4ec7483 + 3843d49 commit 2b6dc95

File tree

10 files changed

+92
-51
lines changed

10 files changed

+92
-51
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.13"
10+
__version__ = "0.1.14"
1111

1212
__all__ = (
1313
"__author__",

bugout/app.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@
1111
from .settings import BUGOUT_BROOD_URL, BUGOUT_SPIRE_URL, REQUESTS_TIMEOUT
1212

1313

14-
class InvalidParameters(ValueError):
15-
"""
16-
Raised when provided invalid parameters.
17-
"""
18-
19-
2014
class Bugout:
2115
def __init__(
2216
self,
@@ -52,12 +46,17 @@ def create_user(
5246
username: str,
5347
email: str,
5448
password: str,
49+
application_id: Optional[Union[str, uuid.UUID]] = None,
5550
timeout: float = REQUESTS_TIMEOUT,
5651
**kwargs: Dict[str, Any],
5752
) -> data.BugoutUser:
5853
self.user.timeout = timeout
5954
return self.user.create_user(
60-
username=username, email=email, password=password, **kwargs
55+
username=username,
56+
email=email,
57+
password=password,
58+
application_id=application_id,
59+
**kwargs,
6160
)
6261

6362
def get_user(
@@ -136,10 +135,16 @@ def delete_user(
136135

137136
# Token handlers
138137
def create_token(
139-
self, username: str, password: str, timeout: float = REQUESTS_TIMEOUT
138+
self,
139+
username: str,
140+
password: str,
141+
application_id: Optional[Union[str, uuid.UUID]] = None,
142+
timeout: float = REQUESTS_TIMEOUT,
140143
) -> data.BugoutToken:
141144
self.user.timeout = timeout
142-
return self.user.create_token(username=username, password=password)
145+
return self.user.create_token(
146+
username=username, password=password, application_id=application_id
147+
)
143148

144149
def create_token_restricted(
145150
self,

bugout/calls.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,7 @@
33
import requests
44

55
from .data import Method
6-
7-
8-
class InvalidUrlSpec(ValueError):
9-
"""
10-
Raised when an invalid url is specified.
11-
"""
12-
13-
14-
class BugoutUnexpectedResponse(Exception):
15-
"""
16-
Raised when Bugout server response is unexpected (e.g. unparseable).
17-
"""
6+
from .exceptions import BugoutResponseException, BugoutUnexpectedResponse
187

198

209
def make_request(method: Method, url: str, **kwargs) -> Any:
@@ -23,8 +12,17 @@ def make_request(method: Method, url: str, **kwargs) -> Any:
2312
r = requests.request(method.value, url=url, **kwargs)
2413
r.raise_for_status()
2514
response_body = r.json()
15+
except requests.exceptions.RequestException as e:
16+
exception_detail = r.json()
17+
raise BugoutResponseException(
18+
"An exception occurred at Bugout API side",
19+
status_code=r.status_code,
20+
detail=exception_detail["detail"]
21+
if exception_detail["detail"] is not None
22+
else None,
23+
)
2624
except Exception as e:
27-
raise BugoutUnexpectedResponse(f"Exception {str(e)}")
25+
raise BugoutUnexpectedResponse(f"{str(e)}")
2826
return response_body
2927

3028

bugout/data.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class BugoutUser(BaseModel):
4545
normalized_email: Optional[str]
4646
verified: Optional[bool]
4747
autogenerated: Optional[bool]
48+
application_id: Optional[uuid.UUID]
4849
created_at: Optional[datetime]
4950
updated_at: Optional[datetime]
5051

bugout/exceptions.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from typing import Any, Optional
2+
3+
4+
class InvalidUrlSpec(ValueError):
5+
"""
6+
Raised when an invalid url is specified.
7+
"""
8+
9+
10+
class BugoutUnexpectedResponse(Exception):
11+
"""
12+
Raised when Bugout server response is unexpected (e.g. unparseable).
13+
"""
14+
15+
16+
class BugoutResponseException(Exception):
17+
"""
18+
Raised when Bugout server response with error.
19+
"""
20+
21+
def __init__(
22+
self,
23+
message,
24+
status_code: int,
25+
detail: Optional[Any] = None,
26+
) -> None:
27+
super().__init__(message)
28+
self.status_code = status_code
29+
if detail is not None:
30+
self.detail = detail
31+
32+
33+
class TokenInvalidParameters(ValueError):
34+
"""
35+
Raised when operations are applied to a token but invalid parameters are provided with which to
36+
specify that token.
37+
"""
38+
39+
40+
class GroupInvalidParameters(ValueError):
41+
"""
42+
Raised when operations are applied to a group but invalid parameters are provided.
43+
"""

bugout/group.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Any, Dict, Optional, Union
22
import uuid
33

4-
from .calls import make_request, InvalidUrlSpec
4+
from .calls import make_request
55
from .data import (
66
Method,
77
Role,
@@ -12,15 +12,10 @@
1212
BugoutApplication,
1313
BugoutApplications,
1414
)
15+
from .exceptions import InvalidUrlSpec, GroupInvalidParameters
1516
from .settings import REQUESTS_TIMEOUT
1617

1718

18-
class GroupInvalidParameters(ValueError):
19-
"""
20-
Raised when operations are applied to a group but invalid parameters are provided.
21-
"""
22-
23-
2419
class Group:
2520
"""
2621
Represent a group from Bugout.

bugout/humbug.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from typing import Any, Dict, Optional, Union
1+
from typing import Optional, Union
22
import uuid
33

4-
from .calls import make_request, InvalidUrlSpec
5-
from .data import Method, Role, BugoutHumbugIntegrationsList
4+
from .calls import make_request
5+
from .data import Method, BugoutHumbugIntegrationsList
6+
from .exceptions import InvalidUrlSpec
67
from .settings import REQUESTS_TIMEOUT
78

89

bugout/journal.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from typing import Any, Dict, List, Optional, Union
1+
from typing import Any, List, Optional, Union
22
import uuid
33

4-
from .calls import make_request, InvalidUrlSpec
4+
from .calls import make_request
55
from .data import (
66
BugoutJournal,
77
BugoutJournals,
@@ -18,6 +18,7 @@
1818
Method,
1919
JournalTypes,
2020
)
21+
from .exceptions import InvalidUrlSpec
2122
from .settings import REQUESTS_TIMEOUT
2223

2324

bugout/resource.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
from typing import Any, Dict, Optional, Union
22
import uuid
33

4-
from .calls import make_request, InvalidUrlSpec
4+
from .calls import make_request
55
from .data import Method, BugoutResource, BugoutResources
6+
from .exceptions import InvalidUrlSpec
67
from .settings import REQUESTS_TIMEOUT
78

89

9-
class ResourceInvalidParameters(ValueError):
10-
"""
11-
Raised when operations are applied to a resource but invalid parameters are provided.
12-
"""
13-
14-
1510
class Resource:
1611
"""
1712
Represent a resources from Bugout.

bugout/user.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
from typing import Any, Dict, List, Optional, Union
22
import uuid
33

4-
from .calls import make_request, InvalidUrlSpec
4+
from .calls import make_request
55
from .data import Method, TokenType, BugoutUser, BugoutToken, BugoutUserTokens
6+
from .exceptions import InvalidUrlSpec, TokenInvalidParameters
67
from .settings import REQUESTS_TIMEOUT
78

89

9-
class TokenInvalidParameters(ValueError):
10-
"""
11-
Raised when operations are applied to a token but invalid parameters are provided with which to
12-
specify that token.
13-
"""
14-
15-
1610
class User:
1711
"""
1812
Represent a user from Bugout.
@@ -37,13 +31,15 @@ def create_user(
3731
username: str,
3832
email: str,
3933
password: str,
34+
application_id: Optional[Union[str, uuid.UUID]] = None,
4035
**kwargs: Dict[str, Any],
4136
) -> BugoutUser:
4237
create_user_path = "user"
4338
data = {
4439
"username": username,
4540
"email": email,
4641
"password": password,
42+
"application_id": application_id,
4743
}
4844
headers = {}
4945
if "headers" in kwargs.keys():
@@ -160,11 +156,17 @@ def delete_user(
160156
return BugoutUser(**result)
161157

162158
# Token module
163-
def create_token(self, username: str, password: str) -> BugoutToken:
159+
def create_token(
160+
self,
161+
username: str,
162+
password: str,
163+
application_id: Optional[Union[str, uuid.UUID]] = None,
164+
) -> BugoutToken:
164165
create_token_path = "token"
165166
data = {
166167
"username": username,
167168
"password": password,
169+
"application_id": application_id,
168170
}
169171
result = self._call(method=Method.post, path=create_token_path, data=data)
170172
return BugoutToken(**result)

0 commit comments

Comments
 (0)