Skip to content

Commit b7938ec

Browse files
committed
Finished user handlers
1 parent 492da4e commit b7938ec

4 files changed

Lines changed: 108 additions & 21 deletions

File tree

bugout/app.py

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

44
from . import data
@@ -44,9 +44,6 @@ def spire_ping(self) -> Dict[str, str]:
4444
return calls.ping(self.spire_api_url)
4545

4646
# User handlers
47-
def get_user(self, token: uuid.UUID) -> data.BugoutUser:
48-
return self.user.get_user(token)
49-
5047
def create_user(
5148
self,
5249
username: str,
@@ -58,14 +55,41 @@ def create_user(
5855
username, email, password, autogenerated_token=autogenerated_token
5956
)
6057

58+
def get_user(self, token: uuid.UUID) -> data.BugoutUser:
59+
return self.user.get_user(token)
60+
61+
def get_user_by_id(self, token: uuid.UUID, user_id: uuid.UUID) -> data.BugoutUser:
62+
return self.user.get_user_by_id(token, user_id)
63+
64+
def confirm_email(
65+
self, token: uuid.UUID, verification_code: str
66+
) -> data.BugoutUser:
67+
return self.user.confirm_email(token, verification_code)
68+
69+
def restore_password(self, email: str) -> Dict[str, str]:
70+
return self.user.restore_password(email)
71+
72+
def reset_password(self, reset_id: uuid.UUID, new_password: str) -> data.BugoutUser:
73+
return self.user.reset_password(reset_id, new_password)
74+
75+
def change_password(
76+
self, token: uuid.UUID, current_password: str, new_password: str
77+
) -> data.BugoutUser:
78+
return self.user.change_password(token, current_password, new_password)
79+
80+
def delete_user(
81+
self, token: uuid.UUID, user_id: uuid.UUID, password: str
82+
) -> data.BugoutUser:
83+
return self.user.delete_user(token, user_id, password)
84+
6185
# Token handlers
6286
def create_token(self, username: str, password: str) -> data.BugoutToken:
6387
return self.user.create_token(username, password)
6488

65-
def revoke_token(self, token: uuid.UUID) -> data.BugoutToken:
89+
def revoke_token(self, token: uuid.UUID) -> uuid.UUID:
6690
return self.user.revoke_token(token)
6791

68-
def revoke_token_by_id(self, token: uuid.UUID) -> data.BugoutToken:
92+
def revoke_token_by_id(self, token: uuid.UUID) -> uuid.UUID:
6993
return self.user.revoke_token_by_id(token)
7094

7195
def update_token(
@@ -84,7 +108,7 @@ def get_user_tokens(
84108
token: uuid.UUID,
85109
active: Optional[bool] = None,
86110
token_type: Optional[data.TokenType] = None,
87-
):
111+
) -> data.BugoutUserTokens:
88112
return self.user.get_user_tokens(token, active, token_type)
89113

90114
# Group handlers

bugout/calls.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import logging
21
from typing import Any, Dict, List, Optional, Tuple
32

43
import requests
54

65
from .data import Method
76

8-
logger = logging.getLogger(__name__)
9-
107

118
class InvalidUrlSpec(ValueError):
129
"""
@@ -27,8 +24,7 @@ def make_request(method: Method, url: str, **kwargs) -> Any:
2724
r.raise_for_status()
2825
response_body = r.json()
2926
except Exception as e:
30-
logger.error(f"Exception {str(e)}")
31-
raise
27+
raise BugoutUnexpectedResponse(f"Exception {str(e)}")
3228
return response_body
3329

3430

bugout/data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from datetime import datetime
22
from enum import Enum, unique
3-
from typing import Any, Dict, List, Optional, Tuple
3+
from typing import Any, Dict, List, Optional
44
import uuid
55

66
from pydantic import BaseModel, Field

bugout/user.py

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import json
2-
import logging
3-
from typing import Any, Dict, List, Optional, Tuple, Union
1+
from typing import Any, Dict, List, Optional
42
import uuid
53

64
from .calls import make_request, InvalidUrlSpec
75
from .data import Method, TokenType, BugoutUser, BugoutToken, BugoutUserTokens
86

9-
logger = logging.getLogger(__name__)
10-
117

128
class UserNotFound(Exception):
139
"""
@@ -69,6 +65,77 @@ def get_user(self, token: uuid.UUID) -> BugoutUser:
6965
result = self._call(method=Method.get, path=get_user_path, headers=headers)
7066
return BugoutUser(**result)
7167

68+
def get_user_by_id(self, token: uuid.UUID, user_id: uuid.UUID) -> BugoutUser:
69+
get_user_by_id_path = f"user/{user_id}"
70+
headers = {
71+
"Authorization": f"Bearer {token}",
72+
}
73+
result = self._call(
74+
method=Method.get, path=get_user_by_id_path, headers=headers
75+
)
76+
return BugoutUser(**result)
77+
78+
def confirm_email(self, token: uuid.UUID, verification_code: str) -> BugoutUser:
79+
confirm_user_email_path = "confirm"
80+
data = {
81+
"verification_code": verification_code,
82+
}
83+
headers = {
84+
"Authorization": f"Bearer {token}",
85+
}
86+
result = self._call(
87+
method=Method.post, path=confirm_user_email_path, headers=headers, data=data
88+
)
89+
return BugoutUser(**result)
90+
91+
def restore_password(self, email: str) -> Dict[str, str]:
92+
restore_password_path = "password/restore"
93+
data = {
94+
"email": email,
95+
}
96+
result = self._call(method=Method.post, path=restore_password_path, data=data)
97+
return result
98+
99+
def reset_password(self, reset_id: uuid.UUID, new_password: str) -> BugoutUser:
100+
reset_password_path = "password/reset"
101+
data = {
102+
"reset_id": reset_id,
103+
"new_password": new_password,
104+
}
105+
result = self._call(method=Method.post, path=reset_password_path, data=data)
106+
return BugoutUser(**result)
107+
108+
def change_password(
109+
self, token: uuid.UUID, current_password: str, new_password: str
110+
) -> BugoutUser:
111+
change_password_path = "password/change"
112+
data = {
113+
"new_password": new_password,
114+
"current_password": current_password,
115+
}
116+
headers = {
117+
"Authorization": f"Bearer {token}",
118+
}
119+
result = self._call(
120+
method=Method.post, path=change_password_path, headers=headers, data=data
121+
)
122+
return BugoutUser(**result)
123+
124+
def delete_user(
125+
self, token: uuid.UUID, user_id: uuid.UUID, password: str
126+
) -> BugoutUser:
127+
delete_user_path = f"user/{user_id}"
128+
data = {
129+
"password": password,
130+
}
131+
headers = {
132+
"Authorization": f"Bearer {token}",
133+
}
134+
result = self._call(
135+
method=Method.delete, path=delete_user_path, headers=headers, data=data
136+
)
137+
return BugoutUser(**result)
138+
72139
# Token module
73140
def create_token(self, username: str, password: str) -> BugoutToken:
74141
create_token_path = "token"
@@ -79,7 +146,7 @@ def create_token(self, username: str, password: str) -> BugoutToken:
79146
result = self._call(method=Method.post, path=create_token_path, data=data)
80147
return BugoutToken(**result)
81148

82-
def revoke_token(self, token: uuid.UUID) -> BugoutToken:
149+
def revoke_token(self, token: uuid.UUID) -> uuid.UUID:
83150
revoke_token_path = "token"
84151
headers = {
85152
"Authorization": f"Bearer {token}",
@@ -89,7 +156,7 @@ def revoke_token(self, token: uuid.UUID) -> BugoutToken:
89156
)
90157
return result
91158

92-
def revoke_token_by_id(self, token: uuid.UUID) -> BugoutToken:
159+
def revoke_token_by_id(self, token: uuid.UUID) -> uuid.UUID:
93160
revoke_token_path = f"token/{token}"
94161
result = self._call(method=Method.delete, path=revoke_token_path)
95162
return result
@@ -135,7 +202,7 @@ def get_user_tokens(
135202
token: uuid.UUID,
136203
active: Optional[bool] = None,
137204
token_type: Optional[TokenType] = None,
138-
):
205+
) -> BugoutUserTokens:
139206
get_user_tokens_path = "tokens"
140207
headers = {
141208
"Authorization": f"Bearer {token}",

0 commit comments

Comments
 (0)