9
9
from .models .request import RequestOptionBuilder
10
10
from ..logging import get_logger
11
11
import logging
12
+ from urllib .parse import urlencode
12
13
from ..exceptions import AuthError , BadResponse , NoResultsError
13
14
14
15
logger = get_logger ("RESTClient" )
@@ -29,6 +30,7 @@ def __init__(
29
30
retries : int ,
30
31
base : str ,
31
32
verbose : bool ,
33
+ trace : bool ,
32
34
custom_json : Optional [Any ] = None ,
33
35
):
34
36
if api_key is None :
@@ -58,6 +60,7 @@ def __init__(
58
60
self .retries = retries
59
61
if verbose :
60
62
logger .setLevel (logging .DEBUG )
63
+ self .trace = trace
61
64
if custom_json :
62
65
self .json = custom_json
63
66
else :
@@ -77,14 +80,32 @@ def _get(
77
80
) -> Any :
78
81
option = options if options is not None else RequestOptionBuilder ()
79
82
83
+ headers = self ._concat_headers (option .headers )
84
+
85
+ if self .trace :
86
+ full_url = f"{ self .BASE } { path } "
87
+ if params :
88
+ full_url += f"?{ urlencode (params )} "
89
+ print_headers = headers .copy ()
90
+ if "Authorization" in print_headers :
91
+ print_headers ["Authorization" ] = print_headers ["Authorization" ].replace (
92
+ self .API_KEY , "REDACTED"
93
+ )
94
+ print (f"Request URL: { full_url } " )
95
+ print (f"Request Headers: { print_headers } " )
96
+
80
97
resp = self .client .request (
81
98
"GET" ,
82
99
self .BASE + path ,
83
100
fields = params ,
84
101
retries = self .retries ,
85
- headers = self . _concat_headers ( option . headers ) ,
102
+ headers = headers ,
86
103
)
87
104
105
+ if self .trace :
106
+ resp_headers_dict = dict (resp .headers .items ())
107
+ print (f"Response Headers: { resp_headers_dict } " )
108
+
88
109
if resp .status != 200 :
89
110
raise BadResponse (resp .data .decode ("utf-8" ))
90
111
0 commit comments