Skip to content

Commit 45e561d

Browse files
committed
Do not make a real call for the BeforeRequestHook dummy request
1 parent a83432a commit 45e561d

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

_test_unstructured_client/unit/test_split_pdf_hook.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from collections import Counter
66
from typing import Coroutine
77

8+
import httpx
89
import pytest
910
import requests
1011
from requests_toolbelt import MultipartDecoder, MultipartEncoder
@@ -32,7 +33,7 @@ def test_unit_sdk_init():
3233
hook = SplitPdfHook()
3334
# This is a fake URL, test doesn't make an API call
3435
test_url = "http://localhost:5000"
35-
test_client = requests.Session()
36+
test_client = httpx.Client()
3637

3738
hook.sdk_init(test_url, test_client)
3839

src/unstructured_client/_hooks/custom/split_pdf_hook.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,38 @@ def sdk_init(
116116
) -> Tuple[str, HttpClient]:
117117
"""Initializes Split PDF Hook.
118118
119+
Adds a mock transport layer to the httpx client. This will return an
120+
empty 200 response whenever the specified "dummy host" is used. The before_request
121+
hook returns this request so the SDK always succeeds and jumps straight to
122+
after_success, where we can await the split results.
123+
119124
Args:
120125
base_url (str): URL of the API.
121126
client (HttpClient): HTTP Client.
122127
123128
Returns:
124-
Tuple[str, httpx.Session]: The initialized SDK options.
129+
Tuple[str, HttpClient]: The initialized SDK options.
125130
"""
126-
self.client = client
127-
return base_url, client
131+
class DummyTransport(httpx.BaseTransport):
132+
def __init__(self, base_transport: httpx.BaseTransport):
133+
self.base_transport = base_transport
134+
135+
def handle_request(self, request: httpx.Request) -> httpx.Response:
136+
# Return an empty 200 response if we send a request to this dummy host
137+
if request.method == "GET" and request.url.host == "no-op":
138+
return httpx.Response(status_code=200, content=b'')
139+
140+
# Otherwise, pass the request to the default transport
141+
return self.base_transport.handle_request(request)
142+
143+
# Explicit cast to httpx.Client to avoid a typing error
144+
httpx_client = cast(httpx.Client, client)
145+
146+
# pylint: disable=protected-access
147+
httpx_client._transport = DummyTransport(httpx_client._transport)
148+
149+
self.client = httpx_client
150+
return base_url, self.client
128151

129152
# pylint: disable=too-many-return-statements
130153
def before_request(
@@ -289,7 +312,7 @@ async def call_api_partial(page):
289312

290313
# Return a dummy request for the SDK to use
291314
# This allows us to skip right to the AfterRequestHook and await all the calls
292-
dummy_request = httpx.Request("GET", "https://httpbin.org/status/200")
315+
dummy_request = httpx.Request("GET", "http://no-op")
293316

294317
return dummy_request
295318

0 commit comments

Comments
 (0)