rb-commons 0.1.7__tar.gz → 0.1.8__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: rb-commons
3
- Version: 0.1.7
3
+ Version: 0.1.8
4
4
  Summary: Commons of project and simplified orm based on sqlalchemy.
5
5
  Home-page: https://github.com/Abdulvoris101/rb-commons
6
6
  Author: Abdulvoris
@@ -0,0 +1,53 @@
1
+ import uuid
2
+ from enum import Enum
3
+ from typing import Optional
4
+
5
+ from pydantic import BaseModel, Field, ConfigDict
6
+
7
+
8
+ class UserRole(str, Enum):
9
+ ADMIN = "admin"
10
+ CUSTOMER = "customer"
11
+ GUEST = "guest"
12
+
13
+ class Claims(BaseModel):
14
+ model_config = ConfigDict(extra="ignore")
15
+
16
+ class Claims(BaseModel):
17
+ model_config = ConfigDict(extra="ignore")
18
+
19
+ user_id: Optional[int] = Field(None, alias="x-user-id")
20
+ user_role: UserRole = Field(UserRole.GUEST, alias="x-user-role")
21
+ shop_id: Optional[uuid.UUID] = Field(None, alias="x-shop-id")
22
+
23
+ @classmethod
24
+ def from_headers(cls, headers: dict) -> 'Claims':
25
+ raw_claims = {
26
+ "x-user-id": headers.get("x-user-id"),
27
+ "x-user-role": headers.get("x-user-role"),
28
+ "x-shop-id": headers.get("x-shop-id")
29
+ }
30
+
31
+ try:
32
+ if raw_claims["x-user-id"]:
33
+ try:
34
+ raw_claims["x-user-id"] = int(raw_claims["x-user-id"])
35
+ except ValueError as e:
36
+ raise ValueError(f"Invalid user_id format: {e}")
37
+
38
+ if raw_claims["x-shop-id"]:
39
+ try:
40
+ raw_claims["x-shop-id"] = uuid.UUID(raw_claims["x-shop-id"])
41
+ except ValueError as e:
42
+ raise ValueError(f"Invalid shop_id format: {e}")
43
+
44
+ if raw_claims["x-user-role"]:
45
+ try:
46
+ UserRole(raw_claims["x-user-role"].lower())
47
+ except ValueError as e:
48
+ raise ValueError(f"Invalid user_role: {e}")
49
+
50
+ return cls(**raw_claims)
51
+
52
+ except Exception as e:
53
+ raise ValueError(f"Failed to parse claims: {str(e)}")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: rb-commons
3
- Version: 0.1.7
3
+ Version: 0.1.8
4
4
  Summary: Commons of project and simplified orm based on sqlalchemy.
5
5
  Home-page: https://github.com/Abdulvoris101/rb-commons
6
6
  Author: Abdulvoris
@@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
5
5
 
6
6
  setup(
7
7
  name="rb-commons",
8
- version="0.1.7",
8
+ version="0.1.8",
9
9
  author="Abdulvoris",
10
10
  author_email="erkinovabdulvoris101@gmail.com",
11
11
  description="Commons of project and simplified orm based on sqlalchemy.",
@@ -1,16 +0,0 @@
1
- import uuid
2
- from enum import Enum
3
- from pydantic import BaseModel, Field, ConfigDict
4
-
5
-
6
- class UserRole(str, Enum):
7
- ADMIN = "admin"
8
- CUSTOMER = "customer"
9
- GUEST = "guest"
10
-
11
- class Claims(BaseModel):
12
- model_config = ConfigDict(extra="ignore")
13
-
14
- user_id: str = Field(None, alias="x-user-id")
15
- user_role: UserRole = Field(UserRole.GUEST, alias="x-user-role")
16
- shop_id: uuid.UUID = Field(None, alias="x-shop-id")
File without changes