pwd-client 3.0.0__py3-none-any.whl

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.
Files changed (59) hide show
  1. pw_client/__init__.py +11 -0
  2. pw_client/core/__init__.py +0 -0
  3. pw_client/core/announcement/__init__.py +4 -0
  4. pw_client/core/announcement/constants.py +1 -0
  5. pw_client/core/announcement/endpoints.py +16 -0
  6. pw_client/core/announcement/parsers.py +26 -0
  7. pw_client/core/announcement/schemas.py +14 -0
  8. pw_client/core/announcement/service.py +16 -0
  9. pw_client/core/api.py +79 -0
  10. pw_client/core/auth/__init__.py +4 -0
  11. pw_client/core/auth/constants.py +5 -0
  12. pw_client/core/auth/endpoints.py +58 -0
  13. pw_client/core/auth/parsers.py +93 -0
  14. pw_client/core/auth/schema.py +26 -0
  15. pw_client/core/auth/service.py +30 -0
  16. pw_client/core/batch/__init__.py +19 -0
  17. pw_client/core/batch/constants.py +7 -0
  18. pw_client/core/batch/endpoints.py +81 -0
  19. pw_client/core/batch/parsers.py +348 -0
  20. pw_client/core/batch/schema.py +124 -0
  21. pw_client/core/batch/service.py +75 -0
  22. pw_client/core/clients/__init__.py +0 -0
  23. pw_client/core/clients/http.py +151 -0
  24. pw_client/core/config.py +21 -0
  25. pw_client/core/constants.py +59 -0
  26. pw_client/core/dashboard/__init__.py +4 -0
  27. pw_client/core/dashboard/constants.py +3 -0
  28. pw_client/core/dashboard/endpoints.py +41 -0
  29. pw_client/core/dashboard/parsers.py +78 -0
  30. pw_client/core/dashboard/schemas.py +40 -0
  31. pw_client/core/dashboard/service.py +28 -0
  32. pw_client/core/errors.py +63 -0
  33. pw_client/core/lifecycle.py +15 -0
  34. pw_client/core/quiz/__init__.py +4 -0
  35. pw_client/core/quiz/constants.py +2 -0
  36. pw_client/core/quiz/endpoints.py +19 -0
  37. pw_client/core/quiz/parsers.py +208 -0
  38. pw_client/core/quiz/schema.py +124 -0
  39. pw_client/core/quiz/service.py +20 -0
  40. pw_client/interfaces/__init__.py +0 -0
  41. pw_client/interfaces/api/__init__.py +0 -0
  42. pw_client/interfaces/api/__main__.py +5 -0
  43. pw_client/interfaces/api/app.py +46 -0
  44. pw_client/interfaces/api/deps.py +23 -0
  45. pw_client/interfaces/api/entry.py +26 -0
  46. pw_client/interfaces/api/errors.py +45 -0
  47. pw_client/interfaces/api/routes/__init__.py +0 -0
  48. pw_client/interfaces/api/routes/announcement.py +20 -0
  49. pw_client/interfaces/api/routes/auth.py +45 -0
  50. pw_client/interfaces/api/routes/batch.py +83 -0
  51. pw_client/interfaces/api/routes/dashboard.py +27 -0
  52. pw_client/interfaces/api/routes/quiz.py +28 -0
  53. pw_client/interfaces/api/routes/system.py +28 -0
  54. pw_client/interfaces/api/schema.py +14 -0
  55. pwd_client-3.0.0.dist-info/METADATA +47 -0
  56. pwd_client-3.0.0.dist-info/RECORD +59 -0
  57. pwd_client-3.0.0.dist-info/WHEEL +4 -0
  58. pwd_client-3.0.0.dist-info/entry_points.txt +3 -0
  59. pwd_client-3.0.0.dist-info/licenses/LICENSE +7 -0
pw_client/__init__.py ADDED
@@ -0,0 +1,11 @@
1
+ from importlib.metadata import PackageNotFoundError, version
2
+
3
+ from pw_client.core.api import App
4
+ from pw_client.core.errors import AppError
5
+
6
+ try:
7
+ __version__ = version("pw-client")
8
+ except PackageNotFoundError:
9
+ __version__ = "0+unknown"
10
+
11
+ __all__ = ["App", "AppError", "__version__"]
File without changes
@@ -0,0 +1,4 @@
1
+ from pw_client.core.announcement.schemas import Annoucement
2
+ from pw_client.core.announcement.service import AnnouncementService
3
+
4
+ __all__ = ["AnnouncementService", "Annoucement"]
@@ -0,0 +1 @@
1
+ ANNOUNCEMENT_PATH = "/v1/batches/{batch_id}/announcement/v2"
@@ -0,0 +1,16 @@
1
+ import httpx
2
+
3
+ from pw_client.core.announcement.constants import ANNOUNCEMENT_PATH
4
+ from pw_client.core.clients.http import HTTPClient
5
+ from pw_client.core.constants import build_headers
6
+
7
+
8
+ async def announcement(
9
+ http: HTTPClient, *, token: str, batch_id: str, page: int = 1
10
+ ) -> httpx.Response:
11
+ return await http.request(
12
+ "GET",
13
+ ANNOUNCEMENT_PATH.format(batch_id=batch_id),
14
+ params={"page": page},
15
+ headers=build_headers(token=token),
16
+ )
@@ -0,0 +1,26 @@
1
+ import httpx
2
+
3
+ from pw_client.core.announcement.schemas import Annoucement
4
+ from pw_client.core.clients.http import read_json
5
+ from pw_client.core.errors import InvalidResponse
6
+
7
+
8
+ def parse_annoucement(response: httpx.Response) -> list[Annoucement]:
9
+ data = read_json(response)
10
+ if response.status_code == 200:
11
+ return [
12
+ Annoucement(
13
+ id=ann.get("_id"),
14
+ heading=ann.get("heading"),
15
+ announcement=ann.get("announcement"),
16
+ type=ann.get("type"),
17
+ schedule_time=ann.get("scheduleTime"),
18
+ use_case=ann.get("announcementUseCase"),
19
+ url=f"{ann.get('attachment').get('baseUrl')}{ann.get('attachment').get('key')}",
20
+ )
21
+ for ann in data.get("data")
22
+ ]
23
+ else:
24
+ raise InvalidResponse(
25
+ f"annoucement: invalid response {data.get('error').get('message')}"
26
+ )
@@ -0,0 +1,14 @@
1
+ from typing import Optional
2
+
3
+ from pydantic import BaseModel, ConfigDict, Field
4
+
5
+
6
+ class Annoucement(BaseModel):
7
+ model_config = ConfigDict(frozen=True)
8
+ id: str
9
+ heading: Optional[str] = Field(default=None)
10
+ announcement: Optional[str] = Field(default=None)
11
+ type: Optional[str] = Field(default=None)
12
+ schedule_time: Optional[str] = Field(default=None)
13
+ use_case: Optional[str] = Field(default=None)
14
+ url: Optional[str] = Field(default=None)
@@ -0,0 +1,16 @@
1
+ from pw_client.core.announcement import endpoints, parsers
2
+ from pw_client.core.announcement.schemas import Annoucement
3
+ from pw_client.core.clients.http import HTTPClient
4
+
5
+
6
+ class AnnouncementService:
7
+ def __init__(self, http: HTTPClient) -> None:
8
+ self._http = http
9
+
10
+ async def announcement(
11
+ self, *, token: str, batch_id: str, page: int = 1
12
+ ) -> list[Annoucement]:
13
+ response = await endpoints.announcement(
14
+ self._http, token=token, batch_id=batch_id, page=page
15
+ )
16
+ return parsers.parse_annoucement(response)
pw_client/core/api.py ADDED
@@ -0,0 +1,79 @@
1
+ from contextlib import AsyncExitStack
2
+
3
+ from pw_client.core import lifecycle
4
+ from pw_client.core.announcement.service import AnnouncementService
5
+ from pw_client.core.auth import AuthService
6
+ from pw_client.core.batch import BatchService
7
+ from pw_client.core.config import Settings
8
+ from pw_client.core.dashboard.service import DashboardService
9
+ from pw_client.core.errors import AppNotStarted
10
+ from pw_client.core.quiz.service import QuizService
11
+
12
+
13
+ class App:
14
+ def __init__(self, config: Settings | None = None) -> None:
15
+ self.config = config or Settings()
16
+ self._stack = AsyncExitStack()
17
+ self._started = False
18
+
19
+ self._auth: AuthService | None = None
20
+ self._batch: BatchService | None = None
21
+ self._dashboard: DashboardService | None = None
22
+ self._quiz: QuizService | None = None
23
+ self._announcement: AnnouncementService | None = None
24
+
25
+ @property
26
+ def auth(self) -> AuthService:
27
+ if not self._started or self._auth is None:
28
+ raise AppNotStarted("App not started")
29
+ return self._auth
30
+
31
+ @property
32
+ def batch(self) -> BatchService:
33
+ if not self._started or self._batch is None:
34
+ raise AppNotStarted("App not started")
35
+ return self._batch
36
+
37
+ @property
38
+ def dashboard(self) -> DashboardService:
39
+ if not self._started or self._dashboard is None:
40
+ raise AppNotStarted("App not started")
41
+ return self._dashboard
42
+
43
+ @property
44
+ def quiz(self) -> QuizService:
45
+ if not self._started or self._quiz is None:
46
+ raise AppNotStarted("App not started")
47
+ return self._quiz
48
+
49
+ @property
50
+ def announcement(self) -> AnnouncementService:
51
+ if not self._started or self._announcement is None:
52
+ raise AppNotStarted("App not started")
53
+ return self._announcement
54
+
55
+ async def start(self) -> "App":
56
+ if self._started:
57
+ return self
58
+ try:
59
+ res = await lifecycle.start(self.config, self._stack)
60
+ except BaseException:
61
+ await self._stack.aclose()
62
+ raise
63
+ self._http = res.http
64
+ self._started = True
65
+ self._auth = AuthService(res.http)
66
+ self._batch = BatchService(res.http)
67
+ self._dashboard = DashboardService(res.http)
68
+ self._quiz = QuizService(res.http)
69
+ self._announcement = AnnouncementService(res.http)
70
+ return self
71
+
72
+ async def close(self) -> None:
73
+ if self._started:
74
+ self._started = False
75
+ await self._stack.aclose()
76
+
77
+ async def health(self) -> dict[str, bool]:
78
+ http_ok = self._started and self._http.is_started
79
+ return {"http": http_ok}
@@ -0,0 +1,4 @@
1
+ from pw_client.core.auth.schema import RefreshToken, Token
2
+ from pw_client.core.auth.service import AuthService
3
+
4
+ __all__ = ["AuthService", "Token", "RefreshToken"]
@@ -0,0 +1,5 @@
1
+ TOKEN_PATH = "/v3/oauth/token"
2
+ VERIFY_TOKEN_PATH = "/v3/oauth/verify-token"
3
+ REFRESH_TOKEN_PATH = "/v3/oauth/refresh-token"
4
+ EXCHANGE_TOKEN_PATH = "/v3/oauth/exchange-token"
5
+ LOGOUT_PATH = "/v1/oauth/logout"
@@ -0,0 +1,58 @@
1
+ import httpx
2
+ from pydantic import Field
3
+
4
+ from pw_client.core.auth.constants import (
5
+ EXCHANGE_TOKEN_PATH,
6
+ LOGOUT_PATH,
7
+ REFRESH_TOKEN_PATH,
8
+ TOKEN_PATH,
9
+ VERIFY_TOKEN_PATH,
10
+ )
11
+ from pw_client.core.clients.http import HTTPClient
12
+ from pw_client.core.constants import ORGID, TOKEN_PAYLOAD_BASE, build_headers
13
+
14
+
15
+ async def token(
16
+ http: HTTPClient, *, mobile_no: str, otp: str = Field(min_length=6)
17
+ ) -> httpx.Response:
18
+ return await http.request(
19
+ "POST",
20
+ TOKEN_PATH,
21
+ json={
22
+ "username": mobile_no,
23
+ "otp": otp,
24
+ "client_id": "system-admin",
25
+ "client_secret": ":",
26
+ "grant_type": "password",
27
+ "latitude": 0,
28
+ "longitude": 0,
29
+ "organizationId": ORGID,
30
+ },
31
+ headers=build_headers(),
32
+ idempotent=False,
33
+ )
34
+
35
+
36
+ async def verify_token(http: HTTPClient, *, token: str) -> httpx.Response:
37
+ return await http.request(
38
+ "POST", VERIFY_TOKEN_PATH, headers=build_headers(token=token)
39
+ )
40
+
41
+
42
+ async def refresh_token(http: HTTPClient, *, refresh_token: str) -> httpx.Response:
43
+ return await http.request(
44
+ "POST",
45
+ REFRESH_TOKEN_PATH,
46
+ json={**TOKEN_PAYLOAD_BASE, "refresh_token": refresh_token},
47
+ headers=build_headers(),
48
+ )
49
+
50
+
51
+ async def exchange_token(http: HTTPClient, *, token: str) -> httpx.Response:
52
+ return await http.request(
53
+ "GET", EXCHANGE_TOKEN_PATH, headers=build_headers(token=token)
54
+ )
55
+
56
+
57
+ async def logout(http: HTTPClient, *, token: str) -> httpx.Response:
58
+ return await http.request("POST", LOGOUT_PATH, headers=build_headers(token=token))
@@ -0,0 +1,93 @@
1
+ import httpx
2
+
3
+ from pw_client.core.auth.schema import RefreshToken, Token, User
4
+ from pw_client.core.clients.http import read_json
5
+ from pw_client.core.errors import (
6
+ InvalidCredentials,
7
+ InvalidResponse,
8
+ PreconditionFailed,
9
+ )
10
+
11
+
12
+ def parse_token(response: httpx.Response) -> Token:
13
+ data = read_json(response)
14
+ if response.status_code == 412:
15
+ raise PreconditionFailed(
16
+ f"token: unexpected status {response.status_code} -> {data.get('error').get('message') or {}}"
17
+ )
18
+ elif response.status_code == 200 and data.get("success") is True:
19
+ return Token(
20
+ access_token=data.get("data").get("access_token"),
21
+ refresh_token=data.get("data").get("refresh_token"),
22
+ expires_in=data.get("data").get("expires_in"),
23
+ user=User(
24
+ firstName=data.get("data").get("user").get("firstName"),
25
+ lastName=data.get("data").get("user").get("lastName"),
26
+ email=data.get("data").get("user").get("email"),
27
+ ),
28
+ )
29
+ else:
30
+ raise InvalidResponse(
31
+ f"token: unexpected status {response.status_code} -> {data.get('error').get('message') or {}}"
32
+ )
33
+
34
+
35
+ def parse_verify_token(response: httpx.Response) -> bool:
36
+ if response.status_code not in (200, 401):
37
+ raise InvalidResponse(f"verify_token: unexpected status {response.status_code}")
38
+ data = read_json(response)
39
+ return response.status_code == 200 and data.get("success") is True
40
+
41
+
42
+ def parse_refresh_token(response: httpx.Response) -> RefreshToken:
43
+ data = read_json(response)
44
+ if response.status_code == 412:
45
+ raise PreconditionFailed(
46
+ f"refresh_token: {response.status_code} -> {data.get('error').get('message') or {}}"
47
+ )
48
+ elif response.status_code == 401:
49
+ raise InvalidCredentials(
50
+ f"refresh_token: {response.status_code} -> {data.get('error').get('message') or {}}"
51
+ )
52
+ elif response.status_code == 200:
53
+ return RefreshToken(
54
+ access_token=data.get("data").get("access_token"),
55
+ refresh_token=data.get("data").get("refresh_token"),
56
+ expires_in=data.get("data").get("expires_in"),
57
+ )
58
+ else:
59
+ raise InvalidResponse(
60
+ f"refresh_token: {response.status_code} {response.status_code}"
61
+ )
62
+
63
+
64
+ def parse_exchange_token(response: httpx.Response) -> Token:
65
+ data = read_json(response)
66
+ if response.status_code == 412:
67
+ raise PreconditionFailed(
68
+ f"exchange_token: unexpected status {data.get('error').get('message') or {}}"
69
+ )
70
+ elif response.status_code == 401:
71
+ raise InvalidCredentials(
72
+ f"exchange_token: {data.get('error').get('message') or {}}"
73
+ )
74
+ elif response.status_code == 200 and data.get("success") is True:
75
+ return Token(
76
+ access_token=data.get("data").get("access_token"),
77
+ refresh_token=data.get("data").get("refresh_token"),
78
+ expires_in=data.get("data").get("expires_in"),
79
+ user=User(
80
+ firstName=data.get("data").get("user").get("firstName"),
81
+ lastName=data.get("data").get("user").get("lastName"),
82
+ email=data.get("data").get("user").get("email"),
83
+ ),
84
+ )
85
+ else:
86
+ raise InvalidResponse(
87
+ f"exchange_token: unexpected status {response.status_code}"
88
+ )
89
+
90
+
91
+ def parse_logout(response: httpx.Response) -> bool:
92
+ data = read_json(response)
93
+ return response.status_code == 200 and data.get("success") is True
@@ -0,0 +1,26 @@
1
+ from datetime import datetime
2
+
3
+ from pydantic import BaseModel, ConfigDict, EmailStr
4
+
5
+
6
+ class User(BaseModel):
7
+ model_config = ConfigDict(frozen=True)
8
+ firstName: str
9
+ lastName: str
10
+ username: str | None = None
11
+ email: EmailStr
12
+
13
+
14
+ class Token(BaseModel):
15
+ model_config = ConfigDict(frozen=True)
16
+ access_token: str
17
+ refresh_token: str
18
+ expires_in: datetime | None = None
19
+ user: User
20
+
21
+
22
+ class RefreshToken(BaseModel):
23
+ model_config = ConfigDict(frozen=True)
24
+ access_token: str
25
+ refresh_token: str
26
+ expires_in: datetime | None = None
@@ -0,0 +1,30 @@
1
+ from pw_client.core.auth import endpoints, parsers
2
+ from pw_client.core.auth.schema import RefreshToken, Token
3
+ from pw_client.core.clients.http import HTTPClient
4
+
5
+
6
+ class AuthService:
7
+ def __init__(self, http: HTTPClient) -> None:
8
+ self._http = http
9
+
10
+ async def token(self, *, mobile_no: str, otp: str) -> Token:
11
+ response = await endpoints.token(self._http, mobile_no=mobile_no, otp=otp)
12
+ return parsers.parse_token(response)
13
+
14
+ async def verify_token(self, *, token: str) -> bool:
15
+ response = await endpoints.verify_token(self._http, token=token)
16
+ return parsers.parse_verify_token(response)
17
+
18
+ async def refresh_token(self, *, refresh_token: str) -> RefreshToken:
19
+ response = await endpoints.refresh_token(
20
+ self._http, refresh_token=refresh_token
21
+ )
22
+ return parsers.parse_refresh_token(response)
23
+
24
+ async def exchange_token(self, *, token: str) -> Token:
25
+ response = await endpoints.exchange_token(self._http, token=token)
26
+ return parsers.parse_exchange_token(response)
27
+
28
+ async def logout(self, *, token: str) -> bool:
29
+ response = await endpoints.logout(self._http, token=token)
30
+ return parsers.parse_logout(response)
@@ -0,0 +1,19 @@
1
+ from pw_client.core.batch.schema import (
2
+ BatchDetail,
3
+ Batches,
4
+ Lecture,
5
+ Notes,
6
+ Quiz,
7
+ SubDetail,
8
+ )
9
+ from pw_client.core.batch.service import BatchService
10
+
11
+ __all__ = [
12
+ "BatchService",
13
+ "Batches",
14
+ "BatchDetail",
15
+ "SubDetail",
16
+ "Lecture",
17
+ "Notes",
18
+ "Quiz",
19
+ ]
@@ -0,0 +1,7 @@
1
+ BATCH_PATH = "/batch-service/v1/batches/purchased-batches"
2
+ BATCH_DETAILS_PATH = "/v3/batches/{batch_id}/details"
3
+ SUB_DETAILS_PATH = "/v2/batches/{batch_id}/subject/{sub_id}/topics"
4
+ CHAPTER_DETAILS_PATH = (
5
+ "/batch-service/v3/batch-subject-schedules/{batch_id}/subject/{sub_id}/contents"
6
+ )
7
+ DPP_LIST_PATH = "/v3/test-service/tests/new-dpp-list"
@@ -0,0 +1,81 @@
1
+ from pathlib import Path
2
+ from typing import Literal
3
+
4
+ import httpx
5
+
6
+ from pw_client.core.batch.constants import (
7
+ BATCH_DETAILS_PATH,
8
+ BATCH_PATH,
9
+ CHAPTER_DETAILS_PATH,
10
+ DPP_LIST_PATH,
11
+ SUB_DETAILS_PATH,
12
+ )
13
+ from pw_client.core.clients.http import HTTPClient
14
+ from pw_client.core.constants import build_headers
15
+
16
+
17
+ async def batches(http: HTTPClient, *, token: str, page: int = 1) -> httpx.Response:
18
+ return await http.request(
19
+ "GET", BATCH_PATH, params={page: page}, headers=build_headers(token=token)
20
+ )
21
+
22
+
23
+ async def batch_details(
24
+ http: HTTPClient, *, token: str, batch_id: str
25
+ ) -> httpx.Response:
26
+ return await http.request(
27
+ "GET",
28
+ BATCH_DETAILS_PATH.format(batch_id=batch_id),
29
+ headers=build_headers(token=token),
30
+ )
31
+
32
+
33
+ async def sub_details(
34
+ http: HTTPClient, *, token: str, batch_id: str, sub_id: str, page: int = 1
35
+ ) -> httpx.Response:
36
+ return await http.request(
37
+ "GET",
38
+ SUB_DETAILS_PATH.format(batch_id=batch_id, sub_id=sub_id),
39
+ params={"page": page},
40
+ headers=build_headers(token=token),
41
+ )
42
+
43
+
44
+ async def chapter_details(
45
+ http: HTTPClient,
46
+ *,
47
+ token: str,
48
+ batch_id: str,
49
+ sub_id: str,
50
+ chapter_id: str,
51
+ skip: int = 0,
52
+ limit: int = 20,
53
+ page: int = 1,
54
+ type: Literal["all", "notes", "lectures", "dpp_pdf", "dpp", "dpp_videos"] = "all",
55
+ ) -> httpx.Response:
56
+ if type == "dpp":
57
+ url = DPP_LIST_PATH
58
+ params = {
59
+ "page": page,
60
+ "limit": limit,
61
+ "batchId": batch_id,
62
+ "batchSubjectId": sub_id,
63
+ "chapterId": chapter_id,
64
+ "dppType": "ALL",
65
+ }
66
+ else:
67
+ url = CHAPTER_DETAILS_PATH.format(batch_id=batch_id, sub_id=sub_id)
68
+ params = {
69
+ "skip": skip,
70
+ "limit": limit,
71
+ "contentType": type.upper(),
72
+ "tagId": chapter_id,
73
+ "contentFilter": "ALL",
74
+ }
75
+ return await http.request(
76
+ "GET", url=url, params=params, headers=build_headers(token=token)
77
+ )
78
+
79
+
80
+ async def download_document(http: HTTPClient, *, url: str, dest: Path) -> None:
81
+ await http.download_to_file(url, dest)