|
| 1 | +""" |
| 2 | +HTTP Client abstractions |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +import requests |
| 7 | +from aiohttp import ( |
| 8 | + ClientSession, |
| 9 | + ClientTimeout, |
| 10 | + TCPConnector, |
| 11 | +) |
| 12 | +from .tracer import ( |
| 13 | + create_aiohttp_tracer, |
| 14 | + create_request_tracer, |
| 15 | +) |
| 16 | +from .cli.groups.config.functions import get_credentials |
| 17 | +from .user_agent import USER_AGENT |
| 18 | + |
| 19 | + |
| 20 | +def get_auth_header(): |
| 21 | + """ |
| 22 | + Produce a header dict with the `Authorization` key derived from |
| 23 | + credentials.get("api_key") OR os.getenv('RUNPOD_AI_API_KEY') |
| 24 | + """ |
| 25 | + if credentials := get_credentials(): |
| 26 | + auth = credentials.get("api_key", "") |
| 27 | + else: |
| 28 | + auth = os.getenv("RUNPOD_AI_API_KEY", "") |
| 29 | + |
| 30 | + return { |
| 31 | + "Content-Type": "application/json", |
| 32 | + "Authorization": auth, |
| 33 | + "User-Agent": USER_AGENT, |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | +def AsyncClientSession(*args, **kwargs): # pylint: disable=invalid-name |
| 38 | + """ |
| 39 | + Deprecation from aiohttp.ClientSession forbids inheritance. |
| 40 | + This is now a factory method |
| 41 | + TODO: use httpx |
| 42 | + """ |
| 43 | + return ClientSession( |
| 44 | + connector=TCPConnector(limit=0), |
| 45 | + headers=get_auth_header(), |
| 46 | + timeout=ClientTimeout(600, ceil_threshold=400), |
| 47 | + trace_configs=[create_aiohttp_tracer()], |
| 48 | + *args, |
| 49 | + **kwargs, |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +class SyncClientSession(requests.Session): |
| 54 | + """ |
| 55 | + Inherits requests.Session to override `request()` method for tracing |
| 56 | + TODO: use httpx |
| 57 | + """ |
| 58 | + |
| 59 | + def request(self, method, url, **kwargs): # pylint: disable=arguments-differ |
| 60 | + """ |
| 61 | + Override for tracing. Not using super().request() |
| 62 | + to capture metrics for connection and transfer times |
| 63 | + """ |
| 64 | + with create_request_tracer() as tracer: |
| 65 | + # Separate out the kwargs that are not applicable to `requests.Request` |
| 66 | + request_kwargs = { |
| 67 | + k: v |
| 68 | + for k, v in kwargs.items() |
| 69 | + # contains the names of the arguments |
| 70 | + if k in requests.Request.__init__.__code__.co_varnames |
| 71 | + } |
| 72 | + |
| 73 | + # Separate out the kwargs that are applicable to `requests.Request` |
| 74 | + send_kwargs = {k: v for k, v in kwargs.items() if k not in request_kwargs} |
| 75 | + |
| 76 | + # Create a PreparedRequest object to hold the request details |
| 77 | + req = requests.Request(method, url, **request_kwargs) |
| 78 | + prepped = self.prepare_request(req) |
| 79 | + tracer.request = prepped # Assign the request to the tracer |
| 80 | + |
| 81 | + # Merge environment settings |
| 82 | + settings = self.merge_environment_settings( |
| 83 | + prepped.url, |
| 84 | + send_kwargs.get("proxies"), |
| 85 | + send_kwargs.get("stream"), |
| 86 | + send_kwargs.get("verify"), |
| 87 | + send_kwargs.get("cert"), |
| 88 | + ) |
| 89 | + send_kwargs.update(settings) |
| 90 | + |
| 91 | + # Send the request |
| 92 | + response = self.send(prepped, **send_kwargs) |
| 93 | + tracer.response = response # Assign the response to the tracer |
| 94 | + |
| 95 | + return response |
0 commit comments