Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Better Access to Response Headers #370

Closed
sedouard opened this issue Nov 8, 2017 · 11 comments
Closed

Better Access to Response Headers #370

sedouard opened this issue Nov 8, 2017 · 11 comments

Comments

@sedouard
Copy link
Contributor

sedouard commented Nov 8, 2017

On a successful API request the Stripe Java SDK doesn't provide a way of accessing response headers. (At least not that I can tell).

This can be useful in the case you're interested in response metadata (such as Idempotency-Key and Request-ID).

For example in a GET /charges/:id request:

import stripe
stripe.api_key = "sk_test_..."

# list charges
stripe.Charge.list()

# retrieve single charge - no response headers
charge = stripe.Charge.retrieve("ch_1A2PUG2eZvKYlo2C4Rej1B9d")

It would be great if there was an optional way to grab the HTTP response metadata through some kind of bound method on the deserialized response:

charge.getResponse()

Or simply as a property:

charge.response

Because api_requestor returns only the deserialized version of the response body it isn't possible to get HTTP request-specific things at the moment. Does it make sense to add a response property here so that we mirror the approach discussed in the node sdk?

@sedouard
Copy link
Contributor Author

sedouard commented Nov 8, 2017

(feel free to assign this to me as I'm happy to do a PR here)

@brandur-stripe
Copy link
Contributor

@sedouard To be honest, a lot of the improvements come about when someone is interested enough to tackle them. If you are here, then I think you should take a shot at it — we can always use a few more people able to actively contribute to these libraries. (And it sounds like you already are, but maybe just coordinate with Olivier and co. first to make sure none of them are working on it.)

@jleclanche
Copy link
Contributor

Use case: I am creating a database entry in my app upon a particular event which results in a stripe API request. I want this entry to be as auditable as possible and want to include the request_id.

What I'm doing:

customer = Customer.retrieve(...)
customer.account_balance = 5
customer.save(idempotency_key)

What I'd like to do here is, as a result of the save() call, I would like to get access to the response headers.

@sedouard Have you worked on a PR for this? If you haven't and it's not on your plate I'll be happy to tackle it.

@ob-stripe
Copy link
Contributor

ob-stripe commented Dec 2, 2017

@jleclanche Yes, there is an open PR for this: #371.

Once it's merged, all StripeObjects will have a last_response property that you'll be able to use access the properties (status code, headers and body) of the last HTTP response for the object, e.g.:

customer.save(idempotency_key=idempotency_key)
request_id = customer.last_response.request_id

@jleclanche
Copy link
Contributor

Oh, cool, I missed the PR yeah. Just read the backlog and changes, looks great. You guys sure saved me a bunch of work :)

@ob-stripe
Copy link
Contributor

Fixed in #371 and released as 1.77.0.

@danielhao5
Copy link

It seems I cannot get either status_code, or response_code by following the same?

Code snippet:
customer = stripe.Customer.create(..skip......)
status_code = customer.last_response.status_code
However, I can get the request_id.

Using the latest stripe version and Python 3.7. Not sure what I'm missing here?

@ob-stripe
Copy link
Contributor

@danielhao5 The status code is accessible via the code attribute, not status_code:

@inai-suraj
Copy link

I am using python sdk for stripe. I am unable to find the last_response key in the stripe.PaymentIntent.create method for getting headers info. Is there a different way to get it ?

@pakrym-stripe
Copy link
Contributor

@inai-suraj can you share a code snippet that's not working for you?

I just tried the following code and it works as expected.

  resp = stripe.PaymentIntent.create(
            amount=1099,
            currency="eur",
            automatic_payment_methods={"enabled": True},
        )
        print(resp.last_response.headers)

@inai-suraj
Copy link

@pakrym-stripe My bad. It works. Thanks.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants