|
1 | 1 | import logging
|
2 | 2 | from pathlib import PurePath
|
3 |
| - |
| 3 | +from typing import Optional |
| 4 | +from .config import CoreConfig |
| 5 | +from .utils import encode_key, do_request, socket_globs |
4 | 6 | import requests
|
5 | 7 | from urllib.parse import urlencode
|
6 | 8 | import base64
|
|
25 | 27 | import platform
|
26 | 28 | from glob import glob
|
27 | 29 | import time
|
| 30 | +import sys |
28 | 31 |
|
29 | 32 | __all__ = [
|
30 | 33 | "Core",
|
|
49 | 52 | log = logging.getLogger("socketdev")
|
50 | 53 | log.addHandler(logging.NullHandler())
|
51 | 54 |
|
52 |
| -socket_globs = { |
53 |
| - "spdx": { |
54 |
| - "spdx.json": { |
55 |
| - "pattern": "*[-.]spdx.json" |
56 |
| - } |
57 |
| - }, |
58 |
| - "cdx": { |
59 |
| - "cyclonedx.json": { |
60 |
| - "pattern": "{bom,*[-.]c{yclone,}dx}.json" |
61 |
| - }, |
62 |
| - "xml": { |
63 |
| - "pattern": "{bom,*[-.]c{yclone,}dx}.xml" |
64 |
| - } |
65 |
| - }, |
66 |
| - "npm": { |
67 |
| - "package.json": { |
68 |
| - "pattern": "package.json" |
69 |
| - }, |
70 |
| - "package-lock.json": { |
71 |
| - "pattern": "package-lock.json" |
72 |
| - }, |
73 |
| - "npm-shrinkwrap.json": { |
74 |
| - "pattern": "npm-shrinkwrap.json" |
75 |
| - }, |
76 |
| - "yarn.lock": { |
77 |
| - "pattern": "yarn.lock" |
78 |
| - }, |
79 |
| - "pnpm-lock.yaml": { |
80 |
| - "pattern": "pnpm-lock.yaml" |
81 |
| - }, |
82 |
| - "pnpm-lock.yml": { |
83 |
| - "pattern": "pnpm-lock.yml" |
84 |
| - }, |
85 |
| - "pnpm-workspace.yaml": { |
86 |
| - "pattern": "pnpm-workspace.yaml" |
87 |
| - }, |
88 |
| - "pnpm-workspace.yml": { |
89 |
| - "pattern": "pnpm-workspace.yml" |
90 |
| - } |
91 |
| - }, |
92 |
| - "pypi": { |
93 |
| - "pipfile": { |
94 |
| - "pattern": "pipfile" |
95 |
| - }, |
96 |
| - "pyproject.toml": { |
97 |
| - "pattern": "pyproject.toml" |
98 |
| - }, |
99 |
| - "poetry.lock": { |
100 |
| - "pattern": "poetry.lock" |
101 |
| - }, |
102 |
| - "requirements.txt": { |
103 |
| - "pattern": "*requirements.txt" |
104 |
| - }, |
105 |
| - "requirements": { |
106 |
| - "pattern": "requirements/*.txt" |
107 |
| - }, |
108 |
| - "requirements-*.txt": { |
109 |
| - "pattern": "requirements-*.txt" |
110 |
| - }, |
111 |
| - "requirements_*.txt": { |
112 |
| - "pattern": "requirements_*.txt" |
113 |
| - }, |
114 |
| - "requirements.frozen": { |
115 |
| - "pattern": "requirements.frozen" |
116 |
| - }, |
117 |
| - "setup.py": { |
118 |
| - "pattern": "setup.py" |
119 |
| - } |
120 |
| - }, |
121 |
| - "golang": { |
122 |
| - "go.mod": { |
123 |
| - "pattern": "go.mod" |
124 |
| - }, |
125 |
| - "go.sum": { |
126 |
| - "pattern": "go.sum" |
127 |
| - } |
128 |
| - }, |
129 |
| - "java": { |
130 |
| - "pom.xml": { |
131 |
| - "pattern": "pom.xml" |
132 |
| - } |
133 |
| - } |
134 |
| -} |
135 |
| - |
136 |
| - |
137 |
| -def encode_key(token: str) -> None: |
138 |
| - """ |
139 |
| - encode_key takes passed token string and does a base64 encoding. It sets this as a global variable |
140 |
| - :param token: str of the Socket API Security Token |
141 |
| - :return: |
142 |
| - """ |
143 |
| - global encoded_key |
144 |
| - encoded_key = base64.b64encode(token.encode()).decode('ascii') |
145 |
| - |
146 |
| - |
147 |
| -def do_request( |
148 |
| - path: str, |
149 |
| - headers: dict = None, |
150 |
| - payload: [dict, str] = None, |
151 |
| - files: list = None, |
152 |
| - method: str = "GET", |
153 |
| - base_url: str = None, |
154 |
| -) -> requests.request: |
155 |
| - """ |
156 |
| - do_requests is the shared function for making HTTP calls |
157 |
| - :param base_url: |
158 |
| - :param path: Required path for the request |
159 |
| - :param headers: Optional dictionary of headers. If not set will use a default set |
160 |
| - :param payload: Optional dictionary or string of the payload to pass |
161 |
| - :param files: Optional list of files to upload |
162 |
| - :param method: Optional method to use, defaults to GET |
163 |
| - :return: |
164 |
| - """ |
165 |
| - |
166 |
| - if base_url is not None: |
167 |
| - url = f"{base_url}/{path}" |
168 |
| - else: |
169 |
| - if encoded_key is None or encoded_key == "": |
170 |
| - raise APIKeyMissing |
171 |
| - url = f"{api_url}/{path}" |
172 |
| - |
173 |
| - if headers is None: |
174 |
| - headers = { |
175 |
| - 'Authorization': f"Basic {encoded_key}", |
176 |
| - 'User-Agent': f'SocketPythonCLI/{__version__}', |
177 |
| - "accept": "application/json" |
178 |
| - } |
179 |
| - verify = True |
180 |
| - if allow_unverified_ssl: |
181 |
| - verify = False |
182 |
| - response = requests.request( |
183 |
| - method.upper(), |
184 |
| - url, |
185 |
| - headers=headers, |
186 |
| - data=payload, |
187 |
| - files=files, |
188 |
| - timeout=timeout, |
189 |
| - verify=verify |
190 |
| - ) |
191 |
| - output_headers = headers.copy() |
192 |
| - output_headers['Authorization'] = "API_KEY_REDACTED" |
193 |
| - output = { |
194 |
| - "url": url, |
195 |
| - "headers": output_headers, |
196 |
| - "status_code": response.status_code, |
197 |
| - "body": response.text, |
198 |
| - "payload": payload, |
199 |
| - "files": files, |
200 |
| - "timeout": timeout |
201 |
| - } |
202 |
| - log.debug(output) |
203 |
| - if response.status_code <= 399: |
204 |
| - return response |
205 |
| - elif response.status_code == 400: |
206 |
| - raise APIFailure(output) |
207 |
| - elif response.status_code == 401: |
208 |
| - raise APIAccessDenied("Unauthorized") |
209 |
| - elif response.status_code == 403: |
210 |
| - raise APIInsufficientQuota("Insufficient max_quota for API method") |
211 |
| - elif response.status_code == 404: |
212 |
| - raise APIResourceNotFound(f"Path not found {path}") |
213 |
| - elif response.status_code == 429: |
214 |
| - raise APIInsufficientQuota("Insufficient quota for API route") |
215 |
| - elif response.status_code == 524: |
216 |
| - raise APICloudflareError(response.text) |
217 |
| - else: |
218 |
| - msg = { |
219 |
| - "status_code": response.status_code, |
220 |
| - "UnexpectedError": "There was an unexpected error using the API", |
221 |
| - "error": response.text, |
222 |
| - "payload": payload, |
223 |
| - "url": url |
224 |
| - } |
225 |
| - raise APIFailure(msg) |
226 |
| - |
227 |
| - |
228 | 55 | class Core:
|
229 | 56 | token: str
|
230 | 57 | base_api_url: str
|
@@ -291,7 +118,11 @@ def set_timeout(request_timeout: int):
|
291 | 118 | :return:
|
292 | 119 | """
|
293 | 120 | global timeout
|
| 121 | + print(f"Setting timeout in module {__name__} at {id(sys.modules[__name__])}") |
| 122 | + print(f"Current timeout value: {timeout}") |
| 123 | + print(f"Setting to: {request_timeout}") |
294 | 124 | timeout = request_timeout
|
| 125 | + print(f"New timeout value: {timeout}") |
295 | 126 |
|
296 | 127 | @staticmethod
|
297 | 128 | def get_org_id_slug() -> (str, str):
|
|
0 commit comments