Add trace option for better API call debugging#432
Conversation
|
Was thinking about this a bit over the weekend and we could probably do the same thing by adding something to |
mmoghaddam385
left a comment
There was a problem hiding this comment.
Was thinking about this a bit over the weekend and we could probably do the same thing by adding something to client = RESTClient(). Maybe client = RESTClient(trace=true) or something like that. I thought it was nice to just turn this on and off via a command flag without a code change but I'm open to whatever you folks suggest.
A client library exposing a command line flag feels a bit odd, is this something you've seen in other Python libraries before? I would feel better with something like you're suggesting here where it's a property you pass when creating a RESTClient. A property in the client is more flexible and doesn't make any assumptions about how users are using this library. The question then becomes should we have a more generalized log_level attribute for RESTClient? If log_level=trace then the client would log API calls like this. I can't think off the top of my head what other logging would be useful in the library though so maybe a more pointed trace=true attribute will do just as well.
Either way, I don't think we should be logging API keys, even if it's opt-in. We should redact the Authorization header in the logs like you did in your example in the PR description (I assume that's something you did by hand before posting the PR?).
|
Hey, your feedback here makes sense. I really like the idea of using |
|
Hey @mmoghaddam385, updated this PR to remove the Furthermore, this updated PR adds functionality to redact the API key in the Authorization header when printing request and response headers to the console for debugging purposes. Also, the response headers are now included in the trace output, providing additional insight into the API communication, along with the Example script: from polygon import RESTClient
client = RESTClient(trace=True) # POLYGON_API_KEY environment variable is used
options_chain = []
for o in client.list_snapshot_options_chain(
"HCP",
params={
"expiration_date.gte": "2024-03-16",
"strike_price.gte": 20,
},
):
options_chain.append(o)
print(options_chain)Output (not modified): |
This commit introduces the
--trace-api-callsflag, which enables users to see the full URL, query parameters, and headers for every API request made by the library. This functionality is particularly useful when working with additional parameters like.lt,.lte,.gt, and.gte.With this flag, users can easily debug the API calls, compare them to the web UI or make manual calls to the API, and uncover any potential mistakes in the usage of the library.
Example script:
Example useage:
python3 example.py --trace-api-callsOutput with the flag:
This feature empowers both client library developers and end users to better understand what's happening under the hood and solve problems more efficiently.