Skip to content

Commit 494b96d

Browse files
committed
Update typing in hooks
1 parent ea55097 commit 494b96d

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

src/unstructured_client/_hooks/custom/clean_server_url_hook.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from typing import Tuple
22
from urllib.parse import ParseResult, urlparse, urlunparse
33

4-
import requests
5-
64
from unstructured_client._hooks.types import SDKInitHook
5+
from unstructured_client.httpclient import HttpClient
76

87

98
class CleanServerUrlSDKInitHook(SDKInitHook):
@@ -26,8 +25,8 @@ def clean_server_url(self, base_url) -> str:
2625
return urlunparse(parsed_url._replace(path=""))
2726

2827
def sdk_init(
29-
self, base_url: str, client: requests.Session
30-
) -> Tuple[str, requests.Session]:
28+
self, base_url: str, client: HttpClient
29+
) -> Tuple[str, HttpClient]:
3130
"""Concrete implementation for SDKInitHook."""
3231
cleaned_url = self.clean_server_url(base_url)
3332

src/unstructured_client/_hooks/custom/logger_hook.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
from typing import Optional, Tuple, Union, DefaultDict
33

4-
import requests
4+
import httpx
55

66
from unstructured_client._hooks.custom.common import UNSTRUCTURED_CLIENT_LOGGER_NAME
77
from unstructured_client._hooks.types import (
@@ -11,6 +11,7 @@
1111
SDKInitHook,
1212
AfterSuccessHook,
1313
)
14+
from unstructured_client.httpclient import HttpClient
1415
from collections import defaultdict
1516

1617
logger = logging.getLogger(UNSTRUCTURED_CLIENT_LOGGER_NAME)
@@ -22,7 +23,7 @@ class LoggerHook(AfterErrorHook, AfterSuccessHook, SDKInitHook):
2223
def __init__(self) -> None:
2324
self.retries_counter: DefaultDict[str, int] = defaultdict(int)
2425

25-
def log_retries(self, response: Optional[requests.Response], error: Optional[Exception], operation_id: str,):
26+
def log_retries(self, response: Optional[httpx.Response], error: Optional[Exception], operation_id: str,):
2627
"""Log retries to give users visibility into requests."""
2728

2829
if response is not None and response.status_code // 100 == 5:
@@ -35,7 +36,7 @@ def log_retries(self, response: Optional[requests.Response], error: Optional[Ex
3536
if response.text:
3637
logger.info("Server message - %s", response.text)
3738

38-
elif error is not None and isinstance(error, requests.exceptions.ConnectionError):
39+
elif error is not None and isinstance(error, httpx.ConnectError):
3940
logger.info(
4041
"Failed to process a request due to connection error - %s. "
4142
"Attempting retry number %d after sleep.",
@@ -45,14 +46,14 @@ def log_retries(self, response: Optional[requests.Response], error: Optional[Ex
4546

4647

4748
def sdk_init(
48-
self, base_url: str, client: requests.Session
49-
) -> Tuple[str, requests.Session]:
49+
self, base_url: str, client: HttpClient
50+
) -> Tuple[str, HttpClient]:
5051
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
5152
return base_url, client
5253

5354
def after_success(
54-
self, hook_ctx: AfterSuccessContext, response: requests.Response
55-
) -> Union[requests.Response, Exception]:
55+
self, hook_ctx: AfterSuccessContext, response: httpx.Response
56+
) -> Union[httpx.Response, Exception]:
5657
self.retries_counter.pop(hook_ctx.operation_id, None)
5758
# NOTE: In case of split page partition this means - at least one of the splits was partitioned successfully
5859
logger.info("Successfully partitioned the document.")
@@ -61,9 +62,9 @@ def after_success(
6162
def after_error(
6263
self,
6364
hook_ctx: AfterErrorContext,
64-
response: Optional[requests.Response],
65+
response: Optional[httpx.Response],
6566
error: Optional[Exception],
66-
) -> Union[Tuple[Optional[requests.Response], Optional[Exception]], Exception]:
67+
) -> Union[Tuple[Optional[httpx.Response], Optional[Exception]], Exception]:
6768
"""Concrete implementation for AfterErrorHook."""
6869
self.retries_counter[hook_ctx.operation_id] += 1
6970
self.log_retries(response, error, hook_ctx.operation_id)

src/unstructured_client/_hooks/custom/suggest_defining_url.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Optional, Tuple, Union
22

3+
import httpx
34
import logging
4-
import requests
55

66
from unstructured_client._hooks.custom.common import UNSTRUCTURED_CLIENT_LOGGER_NAME
77
from unstructured_client._hooks.types import AfterErrorContext, AfterErrorHook
@@ -12,7 +12,7 @@
1212
class SuggestDefiningUrlIf401AfterErrorHook(AfterErrorHook):
1313
"""Hook advising users to check that 'server_url' is defined if a 401 error is encountered."""
1414

15-
def warn_if_401(self, response: Optional[requests.Response]):
15+
def warn_if_401(self, response: Optional[httpx.Response]):
1616
"""If the paid API returns 401, warn the user in case they meant to use the free api."""
1717
if response is not None and response.status_code == 401:
1818
logger.warning(
@@ -22,9 +22,9 @@ def warn_if_401(self, response: Optional[requests.Response]):
2222
def after_error(
2323
self,
2424
hook_ctx: AfterErrorContext,
25-
response: Optional[requests.Response],
25+
response: Optional[httpx.Response],
2626
error: Optional[Exception],
27-
) -> Union[Tuple[Optional[requests.Response], Optional[Exception]], Exception]:
27+
) -> Union[Tuple[Optional[httpx.Response], Optional[Exception]], Exception]:
2828
"""Concrete implementation for AfterErrorHook."""
2929
self.warn_if_401(response)
3030
return response, error

0 commit comments

Comments
 (0)