utg-base 1.1.1__tar.gz → 1.2.0__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.
Files changed (26) hide show
  1. {utg_base-1.1.1 → utg_base-1.2.0}/PKG-INFO +1 -1
  2. {utg_base-1.1.1 → utg_base-1.2.0}/pyproject.toml +1 -1
  3. utg_base-1.2.0/src/utg_base/services/__init__.py +1 -0
  4. utg_base-1.2.0/src/utg_base/services/base_api.py +128 -0
  5. utg_base-1.2.0/src/utg_base/utils/__init__.py +1 -0
  6. utg_base-1.2.0/src/utg_base/utils/response_processors.py +27 -0
  7. {utg_base-1.1.1 → utg_base-1.2.0}/README.md +0 -0
  8. {utg_base-1.1.1 → utg_base-1.2.0}/src/utg_base/__init__.py +0 -0
  9. {utg_base-1.1.1 → utg_base-1.2.0}/src/utg_base/api/__init__.py +0 -0
  10. {utg_base-1.1.1 → utg_base-1.2.0}/src/utg_base/api/base.py +0 -0
  11. {utg_base-1.1.1 → utg_base-1.2.0}/src/utg_base/api/pagination.py +0 -0
  12. {utg_base-1.1.1 → utg_base-1.2.0}/src/utg_base/api/routers.py +0 -0
  13. {utg_base-1.1.1 → utg_base-1.2.0}/src/utg_base/api/views.py +0 -0
  14. {utg_base-1.1.1 → utg_base-1.2.0}/src/utg_base/authentications/__init__.py +0 -0
  15. {utg_base-1.1.1 → utg_base-1.2.0}/src/utg_base/authentications/microservice_authentication.py +0 -0
  16. {utg_base-1.1.1 → utg_base-1.2.0}/src/utg_base/authentications/models.py +0 -0
  17. {utg_base-1.1.1 → utg_base-1.2.0}/src/utg_base/logging.py +0 -0
  18. {utg_base-1.1.1 → utg_base-1.2.0}/src/utg_base/models/__init__.py +0 -0
  19. {utg_base-1.1.1 → utg_base-1.2.0}/src/utg_base/models/jwt_user.py +0 -0
  20. {utg_base-1.1.1 → utg_base-1.2.0}/src/utg_base/references_api/__init__.py +0 -0
  21. {utg_base-1.1.1 → utg_base-1.2.0}/src/utg_base/references_api/admin.py +0 -0
  22. {utg_base-1.1.1 → utg_base-1.2.0}/src/utg_base/references_api/apps.py +0 -0
  23. {utg_base-1.1.1 → utg_base-1.2.0}/src/utg_base/references_api/migrations/__init__.py +0 -0
  24. {utg_base-1.1.1 → utg_base-1.2.0}/src/utg_base/references_api/models.py +0 -0
  25. {utg_base-1.1.1 → utg_base-1.2.0}/src/utg_base/references_api/urls.py +0 -0
  26. {utg_base-1.1.1 → utg_base-1.2.0}/src/utg_base/references_api/utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: utg-base
3
- Version: 1.1.1
3
+ Version: 1.2.0
4
4
  Summary: UTG Base Package
5
5
  Author: Rovshen
6
6
  Author-email: rovshenashirov1619@gmail.com
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "utg-base"
3
- version = "1.1.1"
3
+ version = "1.2.0"
4
4
  description = "UTG Base Package"
5
5
  authors = ["Rovshen <rovshenashirov1619@gmail.com>"]
6
6
  readme = "README.md"
@@ -0,0 +1 @@
1
+ from .base_api import BaseServiceAPI
@@ -0,0 +1,128 @@
1
+ from typing import Callable, Any
2
+
3
+ import requests
4
+ import urllib3
5
+ from requests import Response
6
+
7
+ from utg_base.utils import call_processor
8
+
9
+ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
10
+
11
+
12
+ class BaseServiceAPI:
13
+ name: str = None
14
+ base_url: str = None
15
+ default_response_processor: Callable[[Response], Any] = None
16
+
17
+ def request(self, method, path, data=None, json=None, params=None, headers=None,
18
+ authenticator: Callable[[dict], dict] | None = 'default',
19
+ response_processor: Callable[[Response], Any] | None = 'default',
20
+ **kwargs):
21
+ """
22
+ Make an HTTP request using the requests' library.
23
+
24
+ Args:
25
+ method: The HTTP method (GET, POST, PUT, DELETE, etc.).
26
+ path: The API endpoint path.
27
+ data: Dictionary or bytes to be sent in the body of the request (for form data).
28
+ json: JSON serializable data to be sent in the body of the request (for JSON data).
29
+ params: Query parameters as a dictionary.
30
+ headers: Headers to be included in the request.
31
+ authenticator: Optional authenticator function to handle authentication with headers.
32
+ This function should modify the headers for authentication purposes.
33
+ Example authenticator function:
34
+ def my_authenticator(headers):
35
+ headers['Authorization'] = 'Bearer YOUR_ACCESS_TOKEN'
36
+ return headers
37
+
38
+ response_processor: Optional function to process the response data.
39
+ This function should take the requests.Response object as input and return processed data.
40
+ Example response processor function:
41
+ def process_response(response):
42
+ return response.json()
43
+ Kwargs:
44
+ auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
45
+
46
+ Returns:
47
+ The response data, processed if a response processor is provided.
48
+ """
49
+
50
+ if headers is None:
51
+ headers = {}
52
+
53
+ if callable(authenticator):
54
+ headers = authenticator(headers)
55
+
56
+ if authenticator == 'default' and hasattr(self, 'authenticate') and callable(self.authenticate):
57
+ headers = self.authenticate(headers)
58
+
59
+ response = requests.request(
60
+ method=method,
61
+ url=self.base_url + path,
62
+ data=data,
63
+ json=json,
64
+ params=params,
65
+ headers=headers,
66
+ verify=False,
67
+ **kwargs
68
+ )
69
+
70
+ if response_processor == 'default':
71
+ if hasattr(self, 'default_response_processor') and callable(self.default_response_processor):
72
+ return self.default_response_processor(response)
73
+ return response
74
+
75
+ if response_processor is not None:
76
+ return response_processor(response)
77
+
78
+ return response
79
+
80
+ def call(self, method, path, data=None, json=None, params=None, headers=None):
81
+ data = self.request(
82
+ method=method,
83
+ path=path,
84
+ data=data,
85
+ json=json,
86
+ params=params,
87
+ headers=headers,
88
+ response_processor=call_processor
89
+ )
90
+ return {
91
+ 'path': self.base_url + path,
92
+ 'method': method,
93
+ 'status': data['status'],
94
+ 'reason': data['reason'],
95
+ 'request': params if method == 'get' else json,
96
+ 'response': data['response'],
97
+ }
98
+
99
+ def authenticate(self, headers: dict) -> dict:
100
+ """
101
+ Example authenticate method:
102
+ authenticate(self, headers):
103
+ headers['Authorization'] = 'Bearer YOUR_ACCESS_TOKEN'
104
+ return headers
105
+ :param headers:
106
+ :return:
107
+ """
108
+ return headers
109
+
110
+ def get(self, path, data=None, json=None, params=None, headers=None,
111
+ authenticator: Callable[[dict], dict] | None = 'default',
112
+ response_processor: Callable[[Response], Any] | None = 'default'):
113
+ return self.request('get', path, data, json, params, headers, authenticator, response_processor)
114
+
115
+ def post(self, path, data=None, json=None, params=None, headers=None,
116
+ authenticator: Callable[[dict], dict] | None = 'default',
117
+ response_processor: Callable[[Response], Any] | None = 'default'):
118
+ return self.request('post', path, data, json, params, headers, authenticator, response_processor)
119
+
120
+ def put(self, path, data=None, json=None, params=None, headers=None,
121
+ authenticator: Callable[[dict], dict] | None = 'default',
122
+ response_processor: Callable[[Response], Any] | None = 'default'):
123
+ return self.request('put', path, data, json, params, headers, authenticator, response_processor)
124
+
125
+ def patch(self, path, data=None, json=None, params=None, headers=None,
126
+ authenticator: Callable[[dict], dict] | None = 'default',
127
+ response_processor: Callable[[Response], Any] | None = 'default'):
128
+ return self.request('patch', path, data, json, params, headers, authenticator, response_processor)
@@ -0,0 +1 @@
1
+ from .response_processors import call_processor, data_processor
@@ -0,0 +1,27 @@
1
+ from requests import Response
2
+
3
+
4
+ def call_processor(response: Response):
5
+ try:
6
+ data = response.json()
7
+ except Exception as e:
8
+ data = response.text
9
+ return {
10
+ 'status': response.status_code,
11
+ 'reason': response.reason,
12
+ 'response': data,
13
+ }
14
+
15
+
16
+ def data_processor(response: Response):
17
+ try:
18
+ data = response.json()
19
+ except Exception as e:
20
+ raise Exception({
21
+ 'status': response.status_code,
22
+ 'reason': response.reason,
23
+ 'response': response.text,
24
+ 'exception': str(e)
25
+ })
26
+
27
+ return data
File without changes