eclinical-requester 1.0.10__py3-none-any.whl → 1.0.11__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 (34) hide show
  1. api_requester/__init__.py +1 -1
  2. api_requester/core/admin/api/ecoa_auth_api.py +36 -0
  3. api_requester/core/admin/api/subject_auth_api.py +24 -0
  4. api_requester/core/admin/dto/__init__.py +8 -0
  5. api_requester/core/admin/dto/ecoa_onboard_request_dto.py +21 -0
  6. api_requester/core/admin/dto/reset_password_email_request_dto.py +19 -0
  7. api_requester/core/admin/dto/sso_token_retrieve_dto.py +20 -0
  8. api_requester/core/admin/service/auth_service.py +1 -1
  9. api_requester/core/admin/service/ecoa_auth_service.py +43 -0
  10. api_requester/core/admin/service/impl/system_env_service_impl.py +3 -2
  11. api_requester/core/admin/service/subject_auth_service.py +28 -0
  12. api_requester/core/admin/service/user_on_board_application_service.py +2 -2
  13. api_requester/core/call_api.py +15 -4
  14. api_requester/core/common/api/ecoa_auth_api.py +40 -0
  15. api_requester/core/common/api/subject_auth_api.py +30 -0
  16. api_requester/core/common/dto/portal_login_t_ime.py +19 -0
  17. api_requester/core/common/service/ecoa_auth_service.py +45 -0
  18. api_requester/core/common/service/subject_auth_service.py +33 -0
  19. api_requester/http/authorize.py +7 -6
  20. api_requester/http/authorize_ecoa.py +94 -0
  21. api_requester/http/authorize_ediary.py +70 -0
  22. api_requester/utils/constant.py +7 -1
  23. api_requester/utils/logger.py +46 -0
  24. api_requester/utils/read_file.py +2 -3
  25. {eclinical_requester-1.0.10.dist-info → eclinical_requester-1.0.11.dist-info}/METADATA +1 -1
  26. {eclinical_requester-1.0.10.dist-info → eclinical_requester-1.0.11.dist-info}/RECORD +32 -18
  27. {eclinical_requester-1.0.10.dist-info → eclinical_requester-1.0.11.dist-info}/WHEEL +1 -1
  28. api_requester/dto/admin/__init__.py +0 -0
  29. api_requester/utils/log.py +0 -91
  30. /api_requester/{dto/admin → core/admin/dto}/cross_user_user_on_board_dto.py +0 -0
  31. /api_requester/{dto/admin → core/admin/dto}/jwt_authentication_request.py +0 -0
  32. /api_requester/{dto/admin → core/admin/dto}/user_on_board_dto.py +0 -0
  33. {eclinical_requester-1.0.10.dist-info → eclinical_requester-1.0.11.dist-info}/licenses/LICENSE +0 -0
  34. {eclinical_requester-1.0.10.dist-info → eclinical_requester-1.0.11.dist-info}/top_level.txt +0 -0
api_requester/__init__.py CHANGED
@@ -6,4 +6,4 @@
6
6
  @Description: Description
7
7
  @File: __init__.py.py
8
8
  """
9
- __version__ = "1.0.10"
9
+ __version__ = "1.0.11"
@@ -0,0 +1,36 @@
1
+ # !/usr/bin/python3
2
+ # -*- coding:utf-8 -*-
3
+ """
4
+ @Author: xiaodong.li
5
+ @Time: 2024/11/08 14:20
6
+ @Description: Description
7
+ @File: ecoa_auth_api.py
8
+ """
9
+ from api_requester.http.eclinical_requests import *
10
+
11
+
12
+ class AdminEcoaAuthApi:
13
+ """
14
+ The AdminEcoaAuthApi was generated by Generator from the EcoaAuthController.java file.
15
+ """
16
+
17
+ @post('/auth/ecoa/login')
18
+ def auth_ecoa_login_api(self, *args, **kwargs): ...
19
+
20
+ @post('/auth/ecoa/login/sso')
21
+ def create_authentication_token_from_sso_api(self, *args, **kwargs): ...
22
+
23
+ @get('/auth/ecoa/logout')
24
+ def auth_ecoa_logout_api(self, *args, **kwargs): ...
25
+
26
+ @get('/auth/ecoa/me')
27
+ def auth_ecoa_me_api(self, *args, **kwargs): ...
28
+
29
+ @post('/auth/ecoa/reset_password')
30
+ def reset_password_email_api(self, *args, **kwargs): ...
31
+
32
+ @post('/auth/ecoa/study-list')
33
+ def ecoa_study_list_api(self, *args, **kwargs): ...
34
+
35
+ @post('/auth/ecoa/onboard')
36
+ def onboard_api(self, *args, **kwargs): ...
@@ -0,0 +1,24 @@
1
+ # !/usr/bin/python3
2
+ # -*- coding:utf-8 -*-
3
+ """
4
+ @Author: xiaodong.li
5
+ @Time: 2024/04/24 20:45
6
+ @Description: Description
7
+ @File: subject_auth_api.py
8
+ """
9
+ from api_requester.http.eclinical_requests import *
10
+
11
+
12
+ class AdminSubjectAuthApi:
13
+ """
14
+ The AdminSubjectAuthApi was generated by Generator from the SubjectAuthController.java file.
15
+ """
16
+
17
+ @post('/auth/subject/login')
18
+ def auth_subject_login_api(self, *args, **kwargs): ...
19
+
20
+ @get('/auth/subject/logout')
21
+ def auth_subject_logout_api(self, *args, **kwargs): ...
22
+
23
+ @get('/auth/subject/me')
24
+ def auth_subject_me_api(self, *args, **kwargs): ...
@@ -0,0 +1,8 @@
1
+ # !/usr/bin/python3
2
+ # -*- coding:utf-8 -*-
3
+ """
4
+ @Author: xiaodong.li
5
+ @Time: 5/15/2025 2:07 PM
6
+ @Description: Description
7
+ @File: __init__.py.py
8
+ """
@@ -0,0 +1,21 @@
1
+ # !/usr/bin/python3
2
+ # -*- coding:utf-8 -*-
3
+ """
4
+ @Author: xiaodong.li
5
+ @Time: 2024/11/08 14:20
6
+ @Description: Description
7
+ @File: ecoa_onboard_request_dto.py
8
+ """
9
+ from dataclasses import dataclass
10
+
11
+ from api_requester.dto.base_dto import BaseDto
12
+
13
+
14
+ @dataclass
15
+ class EcoaOnboardRequestDto(BaseDto):
16
+ """
17
+ The EcoaOnboardRequestDto was generated by Generator from the EcoaOnboardRequestDto.java file.
18
+ """
19
+ sponsorId: int = None
20
+ studyId: int = None
21
+ envId: int = None
@@ -0,0 +1,19 @@
1
+ # !/usr/bin/python3
2
+ # -*- coding:utf-8 -*-
3
+ """
4
+ @Author: xiaodong.li
5
+ @Time: 2024/04/24 20:45
6
+ @Description: Description
7
+ @File: reset_password_email_request_dto.py
8
+ """
9
+ from dataclasses import dataclass
10
+
11
+ from api_requester.dto.base_dto import BaseDto
12
+
13
+
14
+ @dataclass
15
+ class ResetPasswordEmailRequestDto(BaseDto):
16
+ """
17
+ The ResetPasswordEmailRequestDto was generated by Generator from the ResetPasswordEmailRequestDto.java file.
18
+ """
19
+ userId: str = None
@@ -0,0 +1,20 @@
1
+ # !/usr/bin/python3
2
+ # -*- coding:utf-8 -*-
3
+ """
4
+ @Author: xiaodong.li
5
+ @Time: 2024/04/26 19:01
6
+ @Description: Description
7
+ @File: sso_token_retrieve_dto.py
8
+ """
9
+ from dataclasses import dataclass
10
+
11
+ from api_requester.dto.base_dto import BaseDto
12
+
13
+
14
+ @dataclass
15
+ class SsoTokenRetrieveDto(BaseDto):
16
+ """
17
+ The SsoTokenRetrieveDto was generated by Generator from the SsoTokenRetrieveDto.java file.
18
+ """
19
+ tokenId: str
20
+ userName: str
@@ -7,7 +7,7 @@
7
7
  @File: auth_service.py
8
8
  """
9
9
  from api_requester.core.admin.api.auth_api import AdminAuthApi
10
- from api_requester.dto.admin.jwt_authentication_request import JwtAuthenticationRequest
10
+ from api_requester.core.admin.dto.jwt_authentication_request import JwtAuthenticationRequest
11
11
 
12
12
 
13
13
  class AdminAuthService(AdminAuthApi):
@@ -0,0 +1,43 @@
1
+ # !/usr/bin/python3
2
+ # -*- coding:utf-8 -*-
3
+ """
4
+ @Author: xiaodong.li
5
+ @Time: 2024/11/08 14:20
6
+ @Description: Description
7
+ @File: ecoa_auth_service.py
8
+ """
9
+ from api_requester.core.admin.api.ecoa_auth_api import AdminEcoaAuthApi
10
+ from api_requester.core.admin.dto.ecoa_onboard_request_dto import EcoaOnboardRequestDto
11
+ from api_requester.core.admin.dto.jwt_authentication_request import JwtAuthenticationRequest
12
+ from api_requester.core.admin.dto.reset_password_email_request_dto import ResetPasswordEmailRequestDto
13
+ from api_requester.core.admin.dto.sso_token_retrieve_dto import SsoTokenRetrieveDto
14
+
15
+
16
+ class AdminEcoaAuthService(AdminEcoaAuthApi):
17
+ """
18
+ The AdminEcoaAuthService was generated by Generator from the EcoaAuthController.java file.
19
+ """
20
+
21
+ def __init__(self):
22
+ AdminEcoaAuthApi.__init__(self)
23
+
24
+ def auth_ecoa_login(self, authentication_request: JwtAuthenticationRequest = None):
25
+ return self.auth_ecoa_login_api(json=authentication_request)
26
+
27
+ def create_authentication_token_from_sso(self, token_retrieve: SsoTokenRetrieveDto = None):
28
+ return self.create_authentication_token_from_sso_api(json=token_retrieve)
29
+
30
+ def auth_ecoa_logout(self):
31
+ return self.auth_ecoa_logout_api()
32
+
33
+ def auth_ecoa_me(self):
34
+ return self.auth_ecoa_me_api()
35
+
36
+ def reset_password_email(self, reset_password_email_request_dto: ResetPasswordEmailRequestDto = None):
37
+ return self.reset_password_email_api(json=reset_password_email_request_dto)
38
+
39
+ def ecoa_study_list(self):
40
+ return self.ecoa_study_list_api()
41
+
42
+ def onboard(self, ecoa_onboard_dto: EcoaOnboardRequestDto = None):
43
+ return self.onboard_api(json=ecoa_onboard_dto)
@@ -6,9 +6,10 @@
6
6
  @Description: Description
7
7
  @File: system_env_service_impl.py
8
8
  """
9
+ from loguru import logger
10
+
9
11
  from api_requester.core.common.service.system_env_service import CommonSystemEnvService
10
12
  from api_requester.http.token_manager import apply_token_to_headers
11
- from api_requester.utils.log import Logger
12
13
 
13
14
 
14
15
  class CommonSystemEnvServiceImpl(CommonSystemEnvService):
@@ -35,5 +36,5 @@ class CommonSystemEnvServiceImpl(CommonSystemEnvService):
35
36
  if onboard_role:
36
37
  self.switch_env(env_id=item.get("id"), role_id=onboard_role.get("id"))
37
38
  apply_token_to_headers(self)
38
- Logger().info(f"The user switches the {role} role successfully.")
39
+ logger.info(f"The user switches the {role} role successfully.")
39
40
  return
@@ -0,0 +1,28 @@
1
+ # !/usr/bin/python3
2
+ # -*- coding:utf-8 -*-
3
+ """
4
+ @Author: xiaodong.li
5
+ @Time: 2024/09/06 14:41
6
+ @Description: Description
7
+ @File: subject_auth_service.py
8
+ """
9
+ from api_requester.core.admin.api.subject_auth_api import AdminSubjectAuthApi
10
+ from api_requester.core.admin.dto.jwt_authentication_request import JwtAuthenticationRequest
11
+
12
+
13
+ class AdminSubjectAuthService(AdminSubjectAuthApi):
14
+ """
15
+ The AdminSubjectAuthService was generated by Generator from the SubjectAuthController.java file.
16
+ """
17
+
18
+ def __init__(self):
19
+ AdminSubjectAuthApi.__init__(self)
20
+
21
+ def auth_subject_login(self, authentication_request: JwtAuthenticationRequest = None):
22
+ return self.auth_subject_login_api(json=authentication_request)
23
+
24
+ def auth_subject_logout(self):
25
+ return self.auth_subject_logout_api()
26
+
27
+ def auth_subject_me(self):
28
+ return self.auth_subject_me_api()
@@ -7,8 +7,8 @@
7
7
  @File: user_on_board_application_service.py
8
8
  """
9
9
  from api_requester.core.admin.api.user_on_board_application_api import AdminUserOnBoardApplicationApi
10
- from api_requester.dto.admin.cross_user_user_on_board_dto import CrossUserUserOnBoardDto
11
- from api_requester.dto.admin.user_on_board_dto import UserOnBoardDto
10
+ from api_requester.core.admin.dto.cross_user_user_on_board_dto import CrossUserUserOnBoardDto
11
+ from api_requester.core.admin.dto.user_on_board_dto import UserOnBoardDto
12
12
 
13
13
 
14
14
  class AdminUserOnBoardApplicationService(AdminUserOnBoardApplicationApi):
@@ -8,18 +8,21 @@
8
8
  """
9
9
  from requests import request
10
10
 
11
- from api_requester.dto.admin.user_on_board_dto import UserOnBoardDto
11
+ from api_requester.core.admin.dto.user_on_board_dto import UserOnBoardDto
12
12
  from api_requester.dto.biz_base import BizBase
13
13
  from api_requester.dto.user import EClinicalUser
14
14
  from api_requester.http.app_url import AppUrl
15
15
  from api_requester.http.authorize import Authorize
16
+ from api_requester.http.authorize_ecoa import AuthorizeEcoa
17
+ from api_requester.http.authorize_ediary import AuthorizeEdiary
18
+ from api_requester.utils.constant import EdcAppType
16
19
  from api_requester.utils.placeholder_replacer import PlaceholderReplacer
17
20
 
18
21
 
19
22
  class ApiRequester(BizBase):
20
23
 
21
24
  def __init__(self, username=None, password=None, sponsor=None, study=None, test_env=None, app_env=None, app=None,
22
- company=None, role=None, external=False):
25
+ company=None, role=None, external=False, ttype=None):
23
26
  u = EClinicalUser(
24
27
  username=username,
25
28
  password=password,
@@ -35,9 +38,17 @@ class ApiRequester(BizBase):
35
38
  super().__init__(u)
36
39
  self.headers = dict()
37
40
  self.user_onboard_dto: UserOnBoardDto = UserOnBoardDto(-1)
41
+ self.ttype = ttype
38
42
 
39
43
  def login(self):
40
- auth = Authorize(self.user)
44
+ auth = None
45
+ if isinstance(self.ttype, str):
46
+ if self.ttype.lower() == EdcAppType.ECOA.name.lower():
47
+ auth = AuthorizeEcoa(self.user)
48
+ elif self.ttype.lower() == EdcAppType.EDIARY.name.lower():
49
+ auth = AuthorizeEdiary(self.user)
50
+ if not auth:
51
+ auth = Authorize(self.user)
41
52
  auth.login()
42
53
  self.headers = auth.headers
43
54
  self.time_mills = auth.time_mills
@@ -49,7 +60,7 @@ class ApiRequester(BizBase):
49
60
  url = AppUrl(self.user.app, self.user.test_env).which_url(self.user.app)(api)
50
61
  else:
51
62
  url = AppUrl(self.user.app, self.user.test_env).external_url(api)
52
- request(method, url, headers=self.headers, **kwargs)
63
+ return request(method, url, headers=self.headers, **kwargs)
53
64
 
54
65
  def user_replacer(self):
55
66
  return PlaceholderReplacer({
@@ -0,0 +1,40 @@
1
+ # !/usr/bin/python3
2
+ # -*- coding:utf-8 -*-
3
+ """
4
+ @Author: xiaodong.li
5
+ @Time: 2024/11/22 10:32
6
+ @Description: Description
7
+ @File: ecoa_auth_api.py
8
+ """
9
+ from api_requester.http.eclinical_requests import *
10
+
11
+
12
+ class CommonEcoaAuthApi:
13
+ """
14
+ The CommonEcoaAuthApi was generated by Generator from the EcoaAuthController.java file.
15
+ """
16
+
17
+ @post('/ecoa/auth/login')
18
+ def create_authentication_token_api(self, *args, **kwargs): ...
19
+
20
+ @get('/ecoa/refresh')
21
+ def refresh_and_get_authentication_token_api(self, *args, **kwargs): ...
22
+
23
+ @get('/ecoa/auth/logout')
24
+ def logout_api(self, *args, **kwargs): ...
25
+
26
+ @post('/ecoa/auth/all-study-logout')
27
+ def all_study_logout_api(self, *args, **kwargs): ...
28
+
29
+ @get('/ecoa/me')
30
+ def me_api(self, *args, **kwargs): ...
31
+
32
+ @get('/ecoa/name')
33
+ def ecoa_account_api(self, *args, **kwargs): ...
34
+
35
+ @post('/ecoa/change-password')
36
+ def change_password_api(self, *args, **kwargs): ...
37
+
38
+ @post('/ecoa/change-study-rel-language')
39
+ def change_study_language_api(self, *args, **kwargs): ...
40
+
@@ -0,0 +1,30 @@
1
+ # !/usr/bin/python3
2
+ # -*- coding:utf-8 -*-
3
+ """
4
+ @Author: xiaodong.li
5
+ @Time: 2024/11/01 17:01
6
+ @Description: Description
7
+ @File: subject_auth_api.py
8
+ """
9
+ from api_requester.http.eclinical_requests import *
10
+
11
+
12
+ class CommonSubjectAuthApi:
13
+ """
14
+ The CommonSubjectAuthApi was generated by Generator from the SubjectAuthController.java file.
15
+ """
16
+
17
+ @post('/subject/auth/login')
18
+ def subject_auth_login_api(self, *args, **kwargs): ...
19
+
20
+ @get('/subject/refresh')
21
+ def subject_refresh_api(self, *args, **kwargs): ...
22
+
23
+ @get('/subject/auth/logout')
24
+ def subject_auth_logout_api(self, *args, **kwargs): ...
25
+
26
+ @get('/subject/me')
27
+ def subject_me_api(self, *args, **kwargs): ...
28
+
29
+ @get('/subject/name')
30
+ def subject_api(self, *args, **kwargs): ...
@@ -0,0 +1,19 @@
1
+ # !/usr/bin/python3
2
+ # -*- coding:utf-8 -*-
3
+ """
4
+ @Author: xiaodong.li
5
+ @Time: 2024/11/22 10:32
6
+ @Description: Description
7
+ @File: portal_login_t_ime.py
8
+ """
9
+ from dataclasses import dataclass
10
+
11
+ from api_requester.dto.base_dto import BaseDto
12
+
13
+
14
+ @dataclass
15
+ class PortalLoginTIme(BaseDto):
16
+ """
17
+ The PortalLoginTIme was generated by Generator from the PortalLoginTIme.java file.
18
+ """
19
+ portalLoginTime: int = None
@@ -0,0 +1,45 @@
1
+ # !/usr/bin/python3
2
+ # -*- coding:utf-8 -*-
3
+ """
4
+ @Author: xiaodong.li
5
+ @Time: 2024/11/22 10:32
6
+ @Description: Description
7
+ @File: ecoa_auth_service.py
8
+ """
9
+ from typing import List
10
+
11
+ from api_requester.core.common.api.ecoa_auth_api import CommonEcoaAuthApi
12
+ from api_requester.core.common.dto.portal_login_t_ime import PortalLoginTIme
13
+
14
+
15
+ class CommonEcoaAuthService(CommonEcoaAuthApi):
16
+ """
17
+ The CommonEcoaAuthService was generated by Generator from the EcoaAuthController.java file.
18
+ """
19
+
20
+ def __init__(self):
21
+ CommonEcoaAuthApi.__init__(self)
22
+
23
+ def create_authentication_token(self, portal_login_time: PortalLoginTIme = None):
24
+ return self.create_authentication_token_api(json=portal_login_time)
25
+
26
+ def refresh_and_get_authentication_token(self):
27
+ return self.refresh_and_get_authentication_token_api()
28
+
29
+ def logout(self):
30
+ return self.logout_api()
31
+
32
+ def all_study_logout(self, tokens: List[str] = None):
33
+ return self.all_study_logout_api(json=tokens)
34
+
35
+ def me(self):
36
+ return self.me_api()
37
+
38
+ def ecoa_account(self):
39
+ return self.ecoa_account_api()
40
+
41
+ def change_password(self, ecoa_account_change_password_dto=None):
42
+ return self.change_password_api(json=ecoa_account_change_password_dto)
43
+
44
+ def change_study_language(self, language_id: int = None):
45
+ return self.change_study_language_api(params=dict(languageId=language_id))
@@ -0,0 +1,33 @@
1
+ # !/usr/bin/python3
2
+ # -*- coding:utf-8 -*-
3
+ """
4
+ @Author: xiaodong.li
5
+ @Time: 2024/11/01 17:01
6
+ @Description: Description
7
+ @File: subject_auth_service.py
8
+ """
9
+ from api_requester.core.common.api.subject_auth_api import CommonSubjectAuthApi
10
+
11
+
12
+ class CommonSubjectAuthService(CommonSubjectAuthApi):
13
+ """
14
+ The CommonSubjectAuthService was generated by Generator from the SubjectAuthController.java file.
15
+ """
16
+
17
+ def __init__(self):
18
+ CommonSubjectAuthApi.__init__(self)
19
+
20
+ def subject_auth_login(self):
21
+ return self.subject_auth_login_api()
22
+
23
+ def subject_refresh(self):
24
+ return self.subject_refresh_api()
25
+
26
+ def subject_auth_logout(self):
27
+ return self.subject_auth_logout_api()
28
+
29
+ def subject_me(self):
30
+ return self.subject_me_api()
31
+
32
+ def subject(self):
33
+ return self.subject_api()
@@ -8,18 +8,19 @@
8
8
  """
9
9
  import time
10
10
 
11
+ from loguru import logger
12
+
13
+ from api_requester.core.admin.dto.cross_user_user_on_board_dto import CrossUserUserOnBoardDto
14
+ from api_requester.core.admin.dto.jwt_authentication_request import JwtAuthenticationRequest
15
+ from api_requester.core.admin.dto.user_on_board_dto import UserOnBoardDto
11
16
  from api_requester.core.admin.service.auth_service import AdminAuthService
12
17
  from api_requester.core.admin.service.impl.system_env_service_impl import CommonSystemEnvServiceImpl
13
18
  from api_requester.core.admin.service.impl.user_on_board_application_service_impl import \
14
19
  AdminUserOnBoardApplicationServiceImpl
15
- from api_requester.dto.admin.cross_user_user_on_board_dto import CrossUserUserOnBoardDto
16
- from api_requester.dto.admin.jwt_authentication_request import JwtAuthenticationRequest
17
- from api_requester.dto.admin.user_on_board_dto import UserOnBoardDto
18
20
  from api_requester.dto.biz_base import BizBase
19
21
  from api_requester.dto.user import EClinicalUser
20
22
  from api_requester.http.sample_headers import SampleHeaders
21
23
  from api_requester.utils.constant import AppEnum, UserType
22
- from api_requester.utils.log import Logger
23
24
  from api_requester.utils.rsa import encrypt_password
24
25
 
25
26
 
@@ -57,10 +58,10 @@ class Authorize(BizBase, AdminAuthService, AdminUserOnBoardApplicationServiceImp
57
58
  self.switch_role(self.user.role)
58
59
  else:
59
60
  self._entry_portal(company_id)
60
- Logger().info("The user({0}) logs in to {1} successfully.".format(self.user, login_app_tip))
61
+ logger.info("The user({0}) logs in to {1} successfully.".format(self.user, login_app_tip))
61
62
  except Exception as e:
62
63
  tip = "The user({0}) failed to log in to {1}.".format(self.user, login_app_tip)
63
- Logger().error(tip)
64
+ logger.error(tip)
64
65
  raise Exception(f"{tip} Authorize Failed: {e}")
65
66
 
66
67
  def _auth(self):
@@ -0,0 +1,94 @@
1
+ # !/usr/bin/python3
2
+ # -*- coding:utf-8 -*-
3
+ """
4
+ @Author: xiaodong.li
5
+ @Time: 5/15/2024 2:30 PM
6
+ @Description: Description
7
+ @File: authorize_ecoa.py
8
+ """
9
+ import time
10
+
11
+ from loguru import logger
12
+
13
+ from api_requester.core.admin.dto.ecoa_onboard_request_dto import EcoaOnboardRequestDto
14
+ from api_requester.core.admin.dto.jwt_authentication_request import JwtAuthenticationRequest
15
+ from api_requester.core.admin.dto.user_on_board_dto import UserOnBoardDto
16
+ from api_requester.core.admin.service.ecoa_auth_service import AdminEcoaAuthService
17
+ from api_requester.core.common.service.ecoa_auth_service import CommonEcoaAuthService
18
+ from api_requester.dto.biz_base import BizBase
19
+ from api_requester.dto.user import EClinicalUser
20
+ from api_requester.http.sample_headers import SampleHeaders
21
+ from api_requester.utils.constant import AppEnum
22
+ from api_requester.utils.rsa import encrypt_password
23
+
24
+
25
+ class AuthorizeEcoa(BizBase, AdminEcoaAuthService, CommonEcoaAuthService):
26
+
27
+ def __init__(self, user: EClinicalUser):
28
+ BizBase.__init__(self, user)
29
+ self.login_app = self.user.app
30
+ self.user.app = AppEnum.ADMIN.code
31
+ self.time_mills = time.time()
32
+ self.user_onboard_dto = UserOnBoardDto(-1)
33
+ AdminEcoaAuthService.__init__(self)
34
+ CommonEcoaAuthService.__init__(self)
35
+
36
+ def login(self):
37
+ try:
38
+ self._auth()
39
+ self._onboard()
40
+ self.user.app = self.login_app
41
+ self._app_auth()
42
+ logger.info("{0} logs in to {1} eCOA successfully.".format(self.user, self.login_app))
43
+ except Exception as e:
44
+ logger.error("{0} failed to log in to {1} eCOA.".format(self.user, self.login_app))
45
+ raise Exception(f"Authorize Failed: {e}")
46
+
47
+ def _auth(self):
48
+ self.time_mills = time.time()
49
+ encrypt_pwd = encrypt_password(self.user.password)
50
+ jwt_authentication_request_dto = JwtAuthenticationRequest(self.user.username, encrypt_pwd)
51
+ response = self.auth_ecoa_login(jwt_authentication_request_dto)
52
+ if response is None:
53
+ raise Exception(f"LoginParams failed: {response}")
54
+ if response.get("jwtAuthenticationResponse") is None:
55
+ token = response.get("token")
56
+ else:
57
+ token = response.get("jwtAuthenticationResponse").get("token")
58
+ self.headers = SampleHeaders().add_authorization(token).to_h()
59
+ self.time_mills = time.time()
60
+ return response
61
+
62
+ def _onboard(self):
63
+ payload = self.ecoa_study_list()
64
+ dto = EcoaOnboardRequestDto()
65
+ for sponsor_dto in payload:
66
+ dto.sponsorId = sponsor_dto["sponsorId"]
67
+ dto.envId = sponsor_dto["envId"]
68
+ self.user.sponsor = sponsor_dto["sponsorName"]
69
+ self.user.app_env = sponsor_dto["envName"]
70
+ for study_dto in sponsor_dto["ecoaStudyList"]:
71
+ if sponsor_dto["envId"] == study_dto["envId"]:
72
+ dto.studyId = study_dto["studyId"]
73
+ self.user.study = study_dto["studyName"]
74
+ break
75
+ break
76
+ self.user_onboard_dto.studyId = dto.studyId
77
+ self.user_onboard_dto.sponsorId = dto.sponsorId
78
+ self.user_onboard_dto.envId = dto.envId
79
+ response = self.onboard(dto)
80
+ token = response.get("token")
81
+ self.headers = SampleHeaders().add_authorization(token).to_h()
82
+ self.time_mills = time.time()
83
+
84
+ def _app_auth(self):
85
+ response = self.create_authentication_token()
86
+ if response.get("jwtAuthenticationResponse") is None:
87
+ token = response.get("token")
88
+ else:
89
+ token = response.get("jwtAuthenticationResponse").get("token")
90
+ self.headers = SampleHeaders().add_authorization(token).to_h()
91
+ self.time_mills = time.time()
92
+
93
+ def admin_auth(self):
94
+ return self._auth()
@@ -0,0 +1,70 @@
1
+ # !/usr/bin/python3
2
+ # -*- coding:utf-8 -*-
3
+ """
4
+ @Author: xiaodong.li
5
+ @Time: 5/15/2024 2:30 PM
6
+ @Description: Description
7
+ @File: authorize_ecoa.py
8
+ """
9
+ import time
10
+
11
+ from loguru import logger
12
+
13
+ from api_requester.core.admin.dto.jwt_authentication_request import JwtAuthenticationRequest
14
+ from api_requester.core.admin.dto.user_on_board_dto import UserOnBoardDto
15
+ from api_requester.core.admin.service.subject_auth_service import AdminSubjectAuthService
16
+ from api_requester.core.common.service.subject_auth_service import CommonSubjectAuthService
17
+ from api_requester.dto.biz_base import BizBase
18
+ from api_requester.dto.user import EClinicalUser
19
+ from api_requester.http.sample_headers import SampleHeaders
20
+ from api_requester.utils.constant import AppEnum
21
+ from api_requester.utils.rsa import encrypt_password
22
+
23
+
24
+ class AuthorizeEdiary(BizBase, AdminSubjectAuthService, CommonSubjectAuthService):
25
+
26
+ def __init__(self, user: EClinicalUser):
27
+ BizBase.__init__(self, user)
28
+ self.login_app = self.user.app
29
+ self.user.app = AppEnum.ADMIN.code
30
+ self.time_mills = time.time()
31
+ self.user_onboard_dto = UserOnBoardDto(-1)
32
+ AdminSubjectAuthService.__init__(self)
33
+ CommonSubjectAuthService.__init__(self)
34
+
35
+ def login(self):
36
+ try:
37
+ self._auth()
38
+ self.user.app = self.login_app
39
+ self._app_auth()
40
+ logger.info("{0} logs in to {1} eDiary successfully.".format(self.user, self.login_app))
41
+ except Exception as e:
42
+ logger.error("{0} failed to log in to {1} eDiary.".format(self.user, self.login_app))
43
+ raise Exception(f"Authorize Failed: {e}")
44
+
45
+ def _auth(self):
46
+ self.time_mills = time.time()
47
+ encrypt_pwd = encrypt_password(self.user.password)
48
+ jwt_authentication_request_dto = JwtAuthenticationRequest(self.user.username, encrypt_pwd)
49
+ response = self.auth_subject_login(jwt_authentication_request_dto)
50
+ if response is None:
51
+ raise Exception(f"LoginParams failed: {response}")
52
+ if response.get("jwtAuthenticationResponse") is None:
53
+ token = response.get("token")
54
+ else:
55
+ token = response.get("jwtAuthenticationResponse").get("token")
56
+ self.headers = SampleHeaders().add_authorization(token).to_h()
57
+ self.time_mills = time.time()
58
+ return response
59
+
60
+ def _app_auth(self):
61
+ response = self.subject_auth_login()
62
+ if response.get("jwtAuthenticationResponse") is None:
63
+ token = response.get("token")
64
+ else:
65
+ token = response.get("jwtAuthenticationResponse").get("token")
66
+ self.headers = SampleHeaders().add_authorization(token).to_h()
67
+ self.time_mills = time.time()
68
+
69
+ def admin_auth(self):
70
+ return self._auth()
@@ -6,7 +6,7 @@
6
6
  @Description: Description
7
7
  @File: constant.py
8
8
  """
9
- from enum import Enum, unique
9
+ from enum import Enum, unique, auto
10
10
 
11
11
 
12
12
  @unique
@@ -106,3 +106,9 @@ class RoleEnum(Enum):
106
106
  DM = ("DM",)
107
107
  CRC = ("CRC",)
108
108
  CRA = ("CRA",)
109
+
110
+
111
+ @unique
112
+ class EdcAppType(Enum):
113
+ EDIARY = auto()
114
+ ECOA = auto()
@@ -0,0 +1,46 @@
1
+ """
2
+ Created on Mar 17, 2020
3
+
4
+ @author: xiaodong.li
5
+ """
6
+ import os
7
+ import sys
8
+
9
+ from loguru import logger
10
+
11
+ from api_requester.utils.path import root
12
+
13
+
14
+ def init_logger(log_filename="app.log", level="DEBUG"):
15
+ log_dir = get_writable_log_path()
16
+ logger.remove()
17
+ logger.add(
18
+ sys.stdout,
19
+ level=level,
20
+ format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
21
+ "<level>{level}</level> | "
22
+ "<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - "
23
+ "<level>{message}</level>"
24
+ )
25
+ logger.add(
26
+ os.path.join(log_dir, log_filename),
27
+ level=level,
28
+ rotation="00:00", # 每天午夜切分
29
+ retention="7 days", # 最多保留7天
30
+ compression="zip", # 自动压缩旧日志
31
+ encoding="utf-8",
32
+ enqueue=True # 支持多进程
33
+ )
34
+
35
+
36
+ def get_writable_log_path():
37
+ if getattr(sys, 'frozen', False):
38
+ base_path = os.path.dirname(sys.executable)
39
+ else:
40
+ base_path = root()
41
+ logs_dir = os.path.join(base_path, 'logs')
42
+ os.makedirs(logs_dir, exist_ok=True)
43
+ return logs_dir
44
+
45
+
46
+ init_logger()
@@ -10,10 +10,9 @@ import json
10
10
  import os
11
11
 
12
12
  import yaml
13
+ from loguru import logger
13
14
  from lxml import etree
14
15
 
15
- from api_requester.utils.log import Logger
16
-
17
16
 
18
17
  class JSONConnector:
19
18
  def __init__(self, filepath):
@@ -87,7 +86,7 @@ def connect_to(filepath, ignore_error=False):
87
86
  try:
88
87
  return connection_factory(filepath)
89
88
  except ValueError as ve:
90
- Logger().error(ve)
89
+ logger.error(ve)
91
90
  if ignore_error is False:
92
91
  raise Exception(ve)
93
92
  else:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: eclinical_requester
3
- Version: 1.0.10
3
+ Version: 1.0.11
4
4
  Summary: edetek api requester
5
5
  Home-page: http://example.com
6
6
  Author: xiaodong.li
@@ -1,50 +1,64 @@
1
- api_requester/__init__.py,sha256=voa3D8SKm31uC9BTkWCyrTNfZn2UOBxVTt2I_kSBPMY,176
1
+ api_requester/__init__.py,sha256=-AufKHQP-o4XT_zMkWKvoiqeJNt021Kbcw0hS2sf_-U,176
2
2
  api_requester/core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
- api_requester/core/call_api.py,sha256=OumIPQSNv5JJeghv_r2tDVzmQCDEsvTS4CEmQNz5dO8,2158
3
+ api_requester/core/call_api.py,sha256=a756aZbaG42MWj-VLHJpfWJ5zvBpA615HDogQCdnEUw,2717
4
4
  api_requester/core/admin/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
5
  api_requester/core/admin/api/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
6
  api_requester/core/admin/api/auth_api.py,sha256=dKKOuEnXIKgzhUfaNfSzNm24LREUcvPZ28xxTZisY6w,861
7
+ api_requester/core/admin/api/ecoa_auth_api.py,sha256=kbDB-3dbVEs_XMe95yUNJHLpe0hZCmpIvycATajJCxE,1008
8
+ api_requester/core/admin/api/subject_auth_api.py,sha256=QQHIrFfuSy9hTOO4Zo1K1rBtvFO-qF4iJH9JiUuPpoM,640
7
9
  api_requester/core/admin/api/user_on_board_application_api.py,sha256=gb7w8NYWsXgnWOla9JMyAyb5gVz3NmLiEb9H7ZlkyQs,897
10
+ api_requester/core/admin/dto/__init__.py,sha256=qzyXEU5lClt3eyhOuVr-XxwcB6XR4I_1Ez26ioFLuV4,153
11
+ api_requester/core/admin/dto/cross_user_user_on_board_dto.py,sha256=VSsihBthlfJORhhVeumDQxuF0lUj5vsLUcF3131Wi1s,489
12
+ api_requester/core/admin/dto/ecoa_onboard_request_dto.py,sha256=SL1NDa1wtvhfJqLvbnmDD-HyR0vKGtoUUybSYiZ7jT8,498
13
+ api_requester/core/admin/dto/jwt_authentication_request.py,sha256=Wo9xTzpVIUJjkr4nYdnXsB7Q2nu3oFpy36K2aUUSy90,472
14
+ api_requester/core/admin/dto/reset_password_email_request_dto.py,sha256=5n6LYwGxibmmtAbrLSrokd2FHWGk6w4pV0bQ31BFWII,476
15
+ api_requester/core/admin/dto/sso_token_retrieve_dto.py,sha256=kvdUik1Pvar2AlI_o0i62KnTCkxqZNqk1tHORXeOmI0,452
16
+ api_requester/core/admin/dto/user_on_board_dto.py,sha256=zsCKgnEttnsCkFlXKfuzkXWbvIhfiWv3jEj8jY6GRbQ,692
8
17
  api_requester/core/admin/service/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
- api_requester/core/admin/service/auth_service.py,sha256=RiXY5fgZd63KKKNqkDmqvjl9iVVvIs1PhadJt6Fxbis,1057
10
- api_requester/core/admin/service/user_on_board_application_service.py,sha256=qxevIpe26wXtGdBFqPsEdj7nir8MOQ3wAZsO_jxccF0,1353
18
+ api_requester/core/admin/service/auth_service.py,sha256=foo5XJyDNcg9W9bigcjhKxhVNwUA4LKykOqir_ZfPq4,1062
19
+ api_requester/core/admin/service/ecoa_auth_service.py,sha256=elwTj49MY-5Dgh6XNstbtmH-Cwh2-ykxE_y_z8Gd41I,1731
20
+ api_requester/core/admin/service/subject_auth_service.py,sha256=PCvl2zmKwMOS0zbJEmI6QUZhHKr7cGSYqkxxnXGPsyA,911
21
+ api_requester/core/admin/service/user_on_board_application_service.py,sha256=gh3-sQU9JkH_10QQ4vbejqGlFQr9u5oNgHbiQ7ulKcU,1363
11
22
  api_requester/core/admin/service/impl/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
- api_requester/core/admin/service/impl/system_env_service_impl.py,sha256=zoR1HZm6tSoaJFe_bNe2cDLV_GfhZrf2qevKt2UmTQc,1519
23
+ api_requester/core/admin/service/impl/system_env_service_impl.py,sha256=cWea0iOpUivR3bpiyQNE_1aQKtr2_5_xN9sABV6TBUs,1502
13
24
  api_requester/core/admin/service/impl/user_on_board_application_service_impl.py,sha256=2vNFD3dFZDI5ssvrSgzg2AT-yA3nlty_eCRWRd_5-nc,694
14
25
  api_requester/core/common/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
15
26
  api_requester/core/common/api/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
27
+ api_requester/core/common/api/ecoa_auth_api.py,sha256=0rOd95nPnZ4xI-0jtg_ACpLzPWjoNUlxQriPaU_5rCc,1130
28
+ api_requester/core/common/api/subject_auth_api.py,sha256=5Bq67Cdta_CtF5BpkIXYpDbE5Cj-ZD60Iizbq6xo2Ew,799
16
29
  api_requester/core/common/api/system_env_api.py,sha256=Ogrc2vSpbjQBUIr_cUfz-TLX4DD_9eg_PabX433VQuA,634
30
+ api_requester/core/common/dto/portal_login_t_ime.py,sha256=dVhfU7L56vBeTRnZDU7hQzqzimgWZ4ktVJCmJfsRIK4,432
17
31
  api_requester/core/common/service/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
32
+ api_requester/core/common/service/ecoa_auth_service.py,sha256=MyBqf76IcD0Aj7sjN1DmdJccoSeCjRONxrySuR3nW30,1459
33
+ api_requester/core/common/service/subject_auth_service.py,sha256=jSoCH9m4f_UKUcTWbXb1J0I2WcSCgSiEA_EnCqglJog,867
18
34
  api_requester/core/common/service/system_env_service.py,sha256=SIKQCG2tIf1s4dUsSiFvOMAgai6JQ-xRP5HJ8MKF3Ts,787
19
35
  api_requester/docs/application.yaml,sha256=_yTaHYLd9CdGfM4ua3qbYOO3x36cWjFEcJmPGDB_Pak,1151
20
36
  api_requester/dto/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
21
37
  api_requester/dto/base_dto.py,sha256=nyBGidUblKE6MSK-yvcVSlYDyqPbIEdXiTOvymcFZp0,6904
22
38
  api_requester/dto/biz_base.py,sha256=4lzf6FoXCNBr0OlxbFryf-7v16wfP3Hv6c7WUh-DgNs,874
23
39
  api_requester/dto/user.py,sha256=zY3vPS-xaKyFzstig55kwmyfPUuMm5vKRhQQKLDTKf0,1299
24
- api_requester/dto/admin/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
25
- api_requester/dto/admin/cross_user_user_on_board_dto.py,sha256=VSsihBthlfJORhhVeumDQxuF0lUj5vsLUcF3131Wi1s,489
26
- api_requester/dto/admin/jwt_authentication_request.py,sha256=Wo9xTzpVIUJjkr4nYdnXsB7Q2nu3oFpy36K2aUUSy90,472
27
- api_requester/dto/admin/user_on_board_dto.py,sha256=zsCKgnEttnsCkFlXKfuzkXWbvIhfiWv3jEj8jY6GRbQ,692
28
40
  api_requester/http/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
29
41
  api_requester/http/app_url.py,sha256=jTGwxPcu_yts4WZbE9wDl6HqGOB48BdEIGw4Otmo1F0,939
30
- api_requester/http/authorize.py,sha256=3MWW2n6SEeDz1E_gp3w6yRmcyoJZKsR2ffHiCWwrlvY,7612
42
+ api_requester/http/authorize.py,sha256=NEGPWmTwIlCK8ClCNXVrkajvz2utJEHiQbVrDVzF6p8,7608
43
+ api_requester/http/authorize_ecoa.py,sha256=7KPjkCLp4xjMFybT66GSqhIeelGtn2sP-DokydBuQiI,3940
44
+ api_requester/http/authorize_ediary.py,sha256=o-4PFHuI--HLLAq1df-12QBVavWVITUVVE3TWGlEdAA,2854
31
45
  api_requester/http/eclinical_requests.py,sha256=JUQ7EPeAvspz-J6GvpoVM2kA4hyGN4AP166hyQlmeFM,9640
32
46
  api_requester/http/exceptions.py,sha256=BG0eSOkYT50Qib_PpjSrNWkcv9p9gBanOvXOWrP7Jz8,2685
33
47
  api_requester/http/gateway.py,sha256=Ntnj-3OtyKLoGpkcCczMWQ0-0XrS5J_qkEcc9jB5AUk,3676
34
48
  api_requester/http/sample_headers.py,sha256=4a2z0ITPvnNhXiX0xZHfcexiRQdkIvEv8YUK-eGRwDU,705
35
49
  api_requester/http/token_manager.py,sha256=sQO4zd3smA0XfmEndBwXs-8rRCmjkEU48S8jWxxXevA,919
36
50
  api_requester/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
37
- api_requester/utils/constant.py,sha256=l1qhn0TkRhX6YAgJUEpiymtTmi1rSmzJVKdPVpayROs,2422
51
+ api_requester/utils/constant.py,sha256=SpHGa9pwLsqUMWc64U6HuDq2K1S-Q1z9hRxW1cq896k,2506
38
52
  api_requester/utils/json_utils.py,sha256=0fTXGESYxuiHv4ezx_iIrmXv2qwb3Ozd1qRIXenToOQ,2614
39
53
  api_requester/utils/lib.py,sha256=wOXU4hB1tFPHT2X96zmVUfEb7hulowDVO8BN5kV9OpA,14429
40
- api_requester/utils/log.py,sha256=McGW0GE_8mt6GlYHT1B9cW_xGVXV3e1mkez_QJe6DkE,2885
54
+ api_requester/utils/logger.py,sha256=JDRvNV47sCi9cPQ2oAVDuMboCJX1quS8R9wljAdqUU4,1185
41
55
  api_requester/utils/path.py,sha256=65N-cWfKylYMaP3R4_Ajzx1TlQnAD-_8RY7Dm6Ps4vQ,451
42
56
  api_requester/utils/placeholder_replacer.py,sha256=umNGeA1KCQ6Ewzsv1xEehLmHtKkgvSrfBf6mwvn7EdM,606
43
- api_requester/utils/read_file.py,sha256=QRE2hZaPaCo7tyY0fW7c6PLXSmc7Zp1HA-SsN_B-2gs,2325
57
+ api_requester/utils/read_file.py,sha256=M4oskAPDzefeVXfhhNmLe4GOjBKNi9WJLF4k474CTlo,2304
44
58
  api_requester/utils/rsa.py,sha256=dr2m3a2M_LYu1Us97EEiMcjZAkpn1yWKIOjyYYubZeg,1202
45
59
  api_requester/utils/time_utils.py,sha256=u1hHO0POBhdXaTQ7VGY_2YjaYxwlrWXGb5phP8iLfOY,579
46
- eclinical_requester-1.0.10.dist-info/licenses/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
47
- eclinical_requester-1.0.10.dist-info/METADATA,sha256=QkApRVkLEqaBAk2NIiySwE3LoHLI1kz_Hicrp-MTkOc,719
48
- eclinical_requester-1.0.10.dist-info/WHEEL,sha256=1tXe9gY0PYatrMPMDd6jXqjfpz_B-Wqm32CPfRC58XU,91
49
- eclinical_requester-1.0.10.dist-info/top_level.txt,sha256=nM6wDThrKcbLCcBGrTBPPtstqV97VyITV-YiKLoUG0U,14
50
- eclinical_requester-1.0.10.dist-info/RECORD,,
60
+ eclinical_requester-1.0.11.dist-info/licenses/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
61
+ eclinical_requester-1.0.11.dist-info/METADATA,sha256=jjUSJWXe9GyIrcZKbIcLojJgy7LzfQJ7ol5Ql-RzUZ4,719
62
+ eclinical_requester-1.0.11.dist-info/WHEEL,sha256=Nw36Djuh_5VDukK0H78QzOX-_FQEo6V37m3nkm96gtU,91
63
+ eclinical_requester-1.0.11.dist-info/top_level.txt,sha256=nM6wDThrKcbLCcBGrTBPPtstqV97VyITV-YiKLoUG0U,14
64
+ eclinical_requester-1.0.11.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (77.0.3)
2
+ Generator: setuptools (80.7.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
File without changes
@@ -1,91 +0,0 @@
1
- # !/usr/bin/python3
2
- # -*- coding:utf-8 -*-
3
- """
4
- @Author: xiaodong.li
5
- @Time: 3/18/2024 4:59 PM
6
- @Description: Description
7
- @File: log.py
8
- """
9
- import logging
10
- import os
11
- import re
12
- import time
13
-
14
- from api_requester.utils.path import root
15
- from api_requester.utils.time_utils import now_yyyy_mm_dd
16
-
17
-
18
- class Logger:
19
- """
20
- define log
21
- """
22
-
23
- def __init__(self, path=None, c_level=logging.INFO, f_level=logging.DEBUG):
24
- if path is None:
25
- path = os.path.join(root(), "logs", f"{now_yyyy_mm_dd(time.time())}.log")
26
- self.path = path
27
- self.sh = None
28
- self.fh = None
29
- # create log file in case of no file
30
- if not os.path.exists(path):
31
- path_dir = os.path.dirname(path)
32
- if not os.path.exists(path_dir):
33
- os.makedirs(path_dir)
34
- file = open(path, "w", encoding="utf-8")
35
- file.close()
36
- # create logger
37
- self.logger = logging.getLogger(path)
38
- self.logger.setLevel(logging.DEBUG)
39
- # in case of creating multi-logger object
40
- if not self.logger.handlers:
41
- # set log format
42
- fmt = logging.Formatter("[%(asctime)s.%(msecs)03d] [thread:%(thread)d] [threadName:%(threadName)s] "
43
- "[%(levelname)s %(message)s]", datefmt=r"%Y-%m-%d %H:%M:%S")
44
- # %(filename)s [line:%(lineno)d]
45
- # set CMD log
46
- self.sh = logging.StreamHandler()
47
- self.sh.setFormatter(fmt)
48
- self.sh.setLevel(c_level)
49
- # set log in file
50
- self.fh = logging.FileHandler(path, encoding="utf-8")
51
- self.fh.setFormatter(fmt)
52
- self.fh.setLevel(f_level)
53
- self.logger.addHandler(self.sh)
54
- self.logger.addHandler(self.fh)
55
-
56
- def __del__(self):
57
- try:
58
- if self.sh is not None:
59
- self.logger.removeHandler(self.sh)
60
- if self.fh is not None:
61
- self.logger.removeHandler(self.fh)
62
- finally:
63
- pass
64
-
65
- def debug(self, message):
66
- self.logger.debug(self._msg(message))
67
-
68
- def info(self, message):
69
- self.logger.info(self._msg(message))
70
-
71
- def warn(self, message):
72
- self.logger.warning(self._msg(message))
73
-
74
- def error(self, message):
75
- self.logger.error(self._msg(message))
76
-
77
- def cri(self, message):
78
- self.logger.critical(self._msg(message))
79
-
80
- @staticmethod
81
- def _msg(message):
82
- try:
83
- if "SQL>>>" in message:
84
- b = re.compile(r"\s{2,}")
85
- message = b.sub(" ", message)
86
- return message
87
- elif re.search(r"\n\n", message):
88
- message = re.sub(r"\n\n", r"\n", message)
89
- return message
90
- finally:
91
- return message