2021-05-07 01:56:31 +02:00
|
|
|
from typing import Any, Dict, Optional
|
|
|
|
|
|
|
|
import requests
|
|
|
|
from urllib3 import HTTPResponse
|
|
|
|
|
|
|
|
|
|
|
|
class OutgoingSession(requests.Session):
|
2021-05-07 01:57:46 +02:00
|
|
|
def __init__(self, role: str, timeout: int, headers: Optional[Dict[str, str]] = None) -> None:
|
2021-05-07 01:56:31 +02:00
|
|
|
super().__init__()
|
2021-05-07 01:57:30 +02:00
|
|
|
outgoing_adapter = OutgoingHTTPAdapter(role=role, timeout=timeout)
|
2021-05-07 01:56:31 +02:00
|
|
|
self.mount("http://", outgoing_adapter)
|
|
|
|
self.mount("https://", outgoing_adapter)
|
2021-05-07 01:57:46 +02:00
|
|
|
if headers:
|
|
|
|
self.headers.update(headers)
|
2021-05-07 01:56:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
class OutgoingHTTPAdapter(requests.adapters.HTTPAdapter):
|
2021-05-07 01:57:30 +02:00
|
|
|
role: str
|
2021-05-07 01:56:31 +02:00
|
|
|
timeout: int
|
|
|
|
|
2021-05-07 01:57:30 +02:00
|
|
|
def __init__(self, role: str, timeout: int, *args: Any, **kwargs: Any) -> None:
|
|
|
|
self.role = role
|
2021-05-07 01:56:31 +02:00
|
|
|
self.timeout = timeout
|
|
|
|
super().__init__(*args, *kwargs)
|
|
|
|
|
|
|
|
def send(self, *args: Any, **kwargs: Any) -> HTTPResponse:
|
|
|
|
if kwargs.get("timeout") is None:
|
|
|
|
kwargs["timeout"] = self.timeout
|
|
|
|
return super().send(*args, **kwargs)
|
2021-05-07 01:57:30 +02:00
|
|
|
|
|
|
|
def proxy_headers(self, proxy: str) -> Dict[str, str]:
|
|
|
|
return {"X-Smokescreen-Role": self.role}
|