-
Notifications
You must be signed in to change notification settings - Fork 229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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-calls
flag, 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-calls
Output 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.