Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions httpcore/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
class AsyncClient:
def __init__(
self,
base_url: URLTypes = None,
ssl: SSLConfig = DEFAULT_SSL_CONFIG,
timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,
pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
Expand All @@ -46,6 +47,8 @@ def __init__(
self.max_redirects = max_redirects
self.dispatch = dispatch

self.base_url = None if base_url is None else URL(base_url)

async def get(
self,
url: URLTypes,
Expand Down Expand Up @@ -221,6 +224,10 @@ async def request(
ssl: SSLConfig = None,
timeout: TimeoutConfig = None,
) -> Response:

if self.base_url is not None:
url = URL(url, allow_relative=True).resolve_with(self.base_url)

request = Request(
method, url, data=data, query_params=query_params, headers=headers
)
Expand Down Expand Up @@ -375,15 +382,19 @@ async def __aexit__(
class Client:
def __init__(
self,
base_url: URLTypes = None,
ssl: SSLConfig = DEFAULT_SSL_CONFIG,
timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,
pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
max_redirects: int = DEFAULT_MAX_REDIRECTS,
dispatch: Dispatcher = None,
backend: ConcurrencyBackend = None,
) -> None:
self.base_url = None if base_url is None else URL(base_url)

self._client = AsyncClient(
ssl=ssl,
base_url=base_url,
timeout=timeout,
pool_limits=pool_limits,
max_redirects=max_redirects,
Expand All @@ -405,6 +416,10 @@ def request(
ssl: SSLConfig = None,
timeout: TimeoutConfig = None,
) -> SyncResponse:

if self.base_url is not None:
url = URL(url, allow_relative=True).resolve_with(self.base_url)

request = Request(
method, url, data=data, query_params=query_params, headers=headers
)
Expand Down
10 changes: 10 additions & 0 deletions tests/client/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ async def test_get(server):
assert repr(response) == "<Response(200, 'OK')>"


@pytest.mark.asyncio
async def test_get_base_url(server):
base_url = "http://127.0.0.1:8000/"
async with httpcore.AsyncClient(base_url=base_url) as client:
response = await client.get("/hello")
assert response.status_code == 200
assert response.text == "Hello, world!"
assert str(response.url) == "http://127.0.0.1:8000/hello"


@pytest.mark.asyncio
async def test_post(server):
url = "http://127.0.0.1:8000/"
Expand Down
10 changes: 10 additions & 0 deletions tests/client/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ def test_get(server):
assert repr(response) == "<Response(200, 'OK')>"


@threadpool
def test_get_base_url(server):
base_url = "http://127.0.0.1:8000/"
with httpcore.Client(base_url=base_url) as client:
response = client.get("/hello")
assert response.status_code == 200
assert response.text == "Hello, world!"
assert str(response.url) == "http://127.0.0.1:8000/hello"


@threadpool
def test_post(server):
with httpcore.Client() as http:
Expand Down