MCPcopy Create free account
hub / github.com/ceph/ceph / http_req

Function http_req

src/ceph-node-proxy/ceph_node_proxy/util.py:173–213  ·  view source on GitHub ↗
(
    hostname: str = "",
    port: str = "443",
    method: Optional[str] = None,
    headers: MutableMapping[str, str] = {},
    data: Optional[str] = None,
    endpoint: str = "/",
    scheme: str = "https",
    ssl_verify: bool = False,
    timeout: Optional[int] = None,
    ssl_ctx: Optional[Any] = None,
)

Source from the content-addressed store, hash-verified

171
172
173def http_req(
174 hostname: str = "",
175 port: str = "443",
176 method: Optional[str] = None,
177 headers: MutableMapping[str, str] = {},
178 data: Optional[str] = None,
179 endpoint: str = "/",
180 scheme: str = "https",
181 ssl_verify: bool = False,
182 timeout: Optional[int] = None,
183 ssl_ctx: Optional[Any] = None,
184) -> Tuple[Any, Any, Any]:
185
186 if not ssl_ctx:
187 ssl_ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
188 if not ssl_verify:
189 ssl_ctx.check_hostname = False
190 ssl_ctx.verify_mode = ssl.CERT_NONE
191 else:
192 ssl_ctx.verify_mode = ssl.CERT_REQUIRED
193
194 url: str = f"{scheme}://{hostname}:{port}{endpoint}"
195 _data = bytes(data, "ascii") if data else None
196 _headers = headers
197 if data and not method:
198 method = "POST"
199 if not _headers.get("Content-Type") and method in ["POST", "PATCH"]:
200 _headers["Content-Type"] = "application/json"
201 try:
202 req = Request(url, _data, _headers, method=method)
203 with urlopen(req, context=ssl_ctx, timeout=timeout) as response:
204 response_str = response.read()
205 response_headers = response.headers
206 response_code = response.code
207 return response_headers, response_str.decode(), response_code
208 except (HTTPError, URLError) as e:
209 # Log level is debug only.
210 # We let whatever calls `http_req()` catching and printing the error
211 logger.debug(f"url={url} err={e}")
212 # handle error here if needed
213 raise
214
215
216def write_tmp_file(

Callers 3

do_reqMethod · 0.90
_send_with_retriesMethod · 0.90
fetch_oob_detailsMethod · 0.90

Calls 6

bytesClass · 0.85
RequestClass · 0.50
getMethod · 0.45
readMethod · 0.45
decodeMethod · 0.45
debugMethod · 0.45

Tested by

no test coverage detected