Skip to content

Commit 3843d49

Browse files
committed
Bypass exceptions message and code from api
1 parent 0e0d1ec commit 3843d49

File tree

9 files changed

+84
-52
lines changed

9 files changed

+84
-52
lines changed

bugout/app.py

Lines changed: 9 additions & 11 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,
@@ -141,10 +135,16 @@ def delete_user(
141135

142136
# Token handlers
143137
def create_token(
144-
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,
145143
) -> data.BugoutToken:
146144
self.user.timeout = timeout
147-
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+
)
148148

149149
def create_token_restricted(
150150
self,
@@ -370,9 +370,7 @@ def list_resources(
370370
timeout: float = REQUESTS_TIMEOUT,
371371
) -> data.BugoutResources:
372372
self.resource.timeout = timeout
373-
return self.resource.list_resources(
374-
token=token, params=params
375-
)
373+
return self.resource.list_resources(token=token, params=params)
376374

377375
def delete_resource(
378376
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: 9 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.
@@ -162,11 +156,17 @@ def delete_user(
162156
return BugoutUser(**result)
163157

164158
# Token module
165-
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:
166165
create_token_path = "token"
167166
data = {
168167
"username": username,
169168
"password": password,
169+
"application_id": application_id,
170170
}
171171
result = self._call(method=Method.post, path=create_token_path, data=data)
172172
return BugoutToken(**result)

0 commit comments

Comments
 (0)