1
1
import logging
2
2
from typing import Optional , Tuple , Union , DefaultDict
3
3
4
- import requests
4
+ import httpx
5
5
6
6
from unstructured_client ._hooks .custom .common import UNSTRUCTURED_CLIENT_LOGGER_NAME
7
7
from unstructured_client ._hooks .types import (
11
11
SDKInitHook ,
12
12
AfterSuccessHook ,
13
13
)
14
+ from unstructured_client .httpclient import HttpClient
14
15
from collections import defaultdict
15
16
16
17
logger = logging .getLogger (UNSTRUCTURED_CLIENT_LOGGER_NAME )
@@ -22,7 +23,7 @@ class LoggerHook(AfterErrorHook, AfterSuccessHook, SDKInitHook):
22
23
def __init__ (self ) -> None :
23
24
self .retries_counter : DefaultDict [str , int ] = defaultdict (int )
24
25
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 ,):
26
27
"""Log retries to give users visibility into requests."""
27
28
28
29
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
35
36
if response .text :
36
37
logger .info ("Server message - %s" , response .text )
37
38
38
- elif error is not None and isinstance (error , requests . exceptions . ConnectionError ):
39
+ elif error is not None and isinstance (error , httpx . ConnectError ):
39
40
logger .info (
40
41
"Failed to process a request due to connection error - %s. "
41
42
"Attempting retry number %d after sleep." ,
@@ -45,14 +46,14 @@ def log_retries(self, response: Optional[requests.Response], error: Optional[Ex
45
46
46
47
47
48
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 ]:
50
51
logging .basicConfig (level = logging .INFO , format = "%(levelname)s: %(message)s" )
51
52
return base_url , client
52
53
53
54
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 ]:
56
57
self .retries_counter .pop (hook_ctx .operation_id , None )
57
58
# NOTE: In case of split page partition this means - at least one of the splits was partitioned successfully
58
59
logger .info ("Successfully partitioned the document." )
@@ -61,9 +62,9 @@ def after_success(
61
62
def after_error (
62
63
self ,
63
64
hook_ctx : AfterErrorContext ,
64
- response : Optional [requests .Response ],
65
+ response : Optional [httpx .Response ],
65
66
error : Optional [Exception ],
66
- ) -> Union [Tuple [Optional [requests .Response ], Optional [Exception ]], Exception ]:
67
+ ) -> Union [Tuple [Optional [httpx .Response ], Optional [Exception ]], Exception ]:
67
68
"""Concrete implementation for AfterErrorHook."""
68
69
self .retries_counter [hook_ctx .operation_id ] += 1
69
70
self .log_retries (response , error , hook_ctx .operation_id )
0 commit comments