pmateusz / meatie

Meatie is a Python metaprogramming library that eliminates the need for boilerplate code when integrating with REST APIs. Meatie abstracts away mechanics related to HTTP communication, such as building URLs, encoding query parameters, parsing, and dumping Pydantic models.
BSD 3-Clause "New" or "Revised" License
29 stars 0 forks source link

Allow passing a base URL in meatie.Client #68

Open DefiDebauchery opened 5 days ago

DefiDebauchery commented 5 days ago

I have sort of a unique situation. My use of meatie is only one small part of my overall application, and I would like to re-use my ClientSession, which has already been set up with various default options (like enabling env for proxy data, custom TCPConnector, timeouts, etc).

There's no easy way (that I could find) to quickly and generally apply these options to another ClientSession, and I wouldn't want to apply the base_url to this CS instance because that would of course apply to unrelated tasks.

I recognize that this is more an aiohttp limitation (not supporting base_url in .request() that I could pass into session_params=) than meatie, but perhaps there's room to increase flexibility by allowing for a local base variable that is combined in meatie.Client? This could also make Clients reusable for platforms that have a consistent API but available on different hostnames, utilizing the same ClientSession (thus potentially opening up strategies like asyncio.gather() across several instances at once).

pmateusz commented 3 days ago

Hi @DefiDebauchery,

If you use aiohttp library and want to use the same ClientSession for communication with different hosts, then I would not set base_url. This is an entirely optional convenience parameter.

Otherwise, if you would like to access some advanced options of aiohttp library I would consider implementing a custom client class following this example: https://github.com/pmateusz/meatie/blob/master/src/meatie_aiohttp/client.py). Meatie codebase contains analog implementations for httpx and requests. By doing it you will have full control over the way HTTP requests are being dispatched.

We had a similar challenge in my other project (i.e., large API is spread across 5 different hosts). We ended up implementing 5 meatie clients with independent Client Sessions. We considered it best tradeoff because each of these hosts behaved differently in terms of error handling and rate limit policies.