encode / httpx

A next generation HTTP client for Python. 🦋
https://www.python-httpx.org/
BSD 3-Clause "New" or "Revised" License
12.7k stars 811 forks source link

pydantic initial integration #3143

Closed 07pepa closed 3 months ago

07pepa commented 3 months ago

Summary

i wanted to use URL to be usable in pydantic setting directly see https://github.com/pydantic/pydantic-extra-types/pull/156 this is mainly to entice discussion and is in no way shape or form final (if this may be accepted)

Checklist

tomchristie commented 3 months ago

Can you describe from a docs pov what you're trying to do?

07pepa commented 3 months ago

I am tryiing to acheave compatibility so url can be in setting class (pulled from .env files or enviroment or other sources)

from pydantic_settings import BaseSettings
from httpx import URL

class Settings(BaseSettings):
    url: URL 

CONFIG = Settings()
AsyncClient = AsyncClient(base_url=CONFIG.url)

alternative is to support AnyUrl from pydantic

tomchristie commented 3 months ago

Okay, can you provide an example of what the above does and then show why httpx would need to be modified in order to accomodate it?

07pepa commented 3 months ago

Okay, can you provide an example of what the above does and then show why httpx would need to be modified in order to accomodate it?

Trick is it does not need to modified... it just would be nice to do it Here (and can be done other way around (extending URL from HTTPX in pydantic) it is mostly for better convinience

demo without change

import os

from pydantic_settings import BaseSettings
from pydantic import AnyUrl
from httpx import AsyncClient

class Settings(BaseSettings):
    url: AnyUrl

# assume this is in .env file
os.environ["url"] = "http://localhost"

CONFIG = Settings()

client = AsyncClient(base_url=str(CONFIG.url))

here is demo with change

import os

from pydantic_settings import BaseSettings
from httpx import URL, AsyncClient

class Settings(BaseSettings):
    url: URL

# assume this is in .env file
os.environ["url"] = "http://localhost"

CONFIG = Settings()

client = AsyncClient(base_url=CONFIG.url)