856 Commits

Author SHA1 Message Date
Olivier Bellone
cddd3db2b2
Override API key with client_secret in OAuth.token (#890) 2020-01-06 15:30:32 -08:00
Olivier Bellone
e23ae43850
Bump version to 5.12.0 2020-01-02 15:12:11 -08:00
Olivier Bellone
c5be2f492b
[codegen] Add support for retrieve source transaction API method (#889)
* Codegen for openapi ba4dcd0

* Add tests
2020-01-02 15:11:08 -08:00
Olivier Bellone
824d1f9efe
Bump version to 5.11.0 2019-11-26 13:46:14 -08:00
Olivier Bellone
7aeae42fdc
Add support for CreditNote preview (#885) 2019-11-26 13:44:56 -08:00
Alex Rattray
75b1797771 Bump version to 5.10.0 2019-11-08 16:42:40 -08:00
Alex Rattray (Stripe)
2ded14efe3
Add list_usage_record_summaries and list_source_transactions (#882)
* Add list_usage_record_summaries and list_source_transactions

* Fix lint
2019-11-08 16:24:38 -08:00
Brandur
455d1c163c Bump version to 5.9.0 2019-11-07 14:19:36 -08:00
Bart
26a0964e56 Add simple instrumentation callback (#870)
* Add simple instrumentation callback

We used to insert Faraday::Request::Instrumentation into our Faraday middleware
stack to be able to instrument Stripe calls with StatsD. With Faraday being
removed in version 5, this required some rework. This commit implements a simple
callback system that can be used with any kind of instrumentation system.

* Add a topic to Stripe::Instrumentation notifications

... and a :request topic to subscribe to

* Use a RequestEvent value object instead of positional args in callback

This way the RequestLogContext object doesn't get exposed externally. Since the
same value object can be received by multiple subscribers it is frozen to
prevent accidental mutations across threads.

* Relocate tests for instrumentation and add more tests
2019-11-07 14:18:11 -08:00
Remi Jannel
8d72722cc7 Bump version to 5.8.0 2019-11-05 19:41:29 -08:00
remi-stripe
6b4a0343b3
Add support for Mandate (#879) 2019-11-05 19:36:41 -08:00
Joel Taylor
299e9ea0ab Raise an error when requests params are invalid (#874)
There are two kinds of API operations: collection and element specific.
The signature between the two is slightly different:
  - **collection**: (params, opts)
  - **element specific**: (id, params, opts)

If a user doesn't realize the difference, they may attempt to use the
collection signature when performing an element specific operation like:
```
Stripe::PaymentIntent.cancel('pi_1234', 'sk_test_key')
 # Results in an error: NoMethodError: undefined method `key?' for "sk_test"
```

The resulting error message isn't very useful for debugging.

Instead,this PR adds a message letting the user know what it's expecting:
`request params should be either a Hash or nil (was a String)`
2019-10-31 09:53:19 -07:00
Olivier Bellone
16c05468bc
Bump version to 5.7.1 2019-10-15 17:09:44 -07:00
tmaxwell-stripe
c4a0735232 s/connection_base/connect_base/ (#869) 2019-10-15 17:08:42 -07:00
Brandur
00b60afbf5 Add some documentation on ordering direction 2019-10-10 10:36:32 -07:00
Brandur
15f8304e48 Bump version to 5.7.0 2019-10-10 10:11:58 -07:00
Brandur
e3cc91ded2 Support backwards pagination with list's #auto_paging_each (#865)
* Support backwards pagination with list's `#auto_paging_each`

Previously, `#auto_paging_each` would always try to paginate forward,
even if it was clear based on the list's current filters that the user
had been intending to iterate backwards by specifying an `ending_before`
filter exclusively.

Here we implement backwards iteration by detecting this condition,
reversing the current list data, and making new requests for the
previous page (instead of the next one) as needed, which allows the user
to handle elements in reverse logical order.

Reversing the current page's list is intended as a minor user feature,
but may possibly be contentious. For background, when backwards
iterating in the API, results are still returned in "normal" order. So
if I specifying `ending_before=7`, the next page would look like `[4, 5,
6`] instead of `[6, 5, 4]`. In `#auto_paging_each` I reverse it to `[6,
5, 4]` so it feels to the user like they're handling elements in the
order they're iterating, which I think is okay. The reason it might be
contentious though is that it could be a tad confusing to someone who
already understands the normal `ending_before` ordering in the API.

Fixes #864.

* Allow `ending_before` and `starting_after` to remain in hydrated list object
2019-10-10 10:11:12 -07:00
Brandur
c206a3cc09 Bump version to 5.6.0 2019-10-04 13:16:35 -07:00
Brandur
bbb585a7c3 Nicer error when specifying non-nil non-string opt value (#861)
Previously, if you specified a non-nil non-string opt value, like a
symbol for `idempotency_key`, you'd get a pretty user-unfriendly error
from `Net::HTTP`:

```
/Users/brandur/.rbenv/versions/2.4.5/lib/ruby/2.4.0/net/http/header.rb:21:in `block in initialize_http_header': undefined method `strip' for :foo:Symbol (NoMethodError)
```

Here, we introduce a new argument error that makes it a little easier
for someone to read. The impetus for the change is that we had an
internal product quality report where someone ran into this and was
confused.

I'm pretty sure this change is backward compatible because `Net::HTTP`
would call `strip` on anything that was passed in as a value, and
generally just strings would support that. There may be some other less
common data type that was accidentally compatible that someone was
using, but that case should be quite unusual.
2019-10-04 13:16:03 -07:00
Brandur
7e5698e77d Bump version to 5.5.0 2019-10-03 13:09:57 -07:00
Michael Elfassy
ba7ee5c5f4 User-friendly messages and retries for EOFError and a few more network errors (#859) 2019-10-03 13:08:11 -07:00
Brandur
27718e0e47 Drop Timecop dependency (#858)
If #857 comes in, it turns out that we don't need Timecop anymore (it
doesn't freeze the monotic clock, so I had to find another way) -- here
we remove all mentions of it and drop the dependency.

I don't find it causes too much trouble so I'm not against bringing it
back in the future if we need it again, but it seems good for project
cleanliness to take it out for now.
2019-10-01 10:10:39 -07:00
Olivier Bellone
8bab04e2bc
Bump version to 5.4.1 2019-10-01 10:07:32 -07:00
Brandur
e52bb95995 Bump version to 5.4.0 2019-10-01 09:59:36 -07:00
Brandur
480303c446 Move to monotonic time for duration calculations (#857)
Drops the use of `Time.now` in favor of using the system's monotonic
clock for various operations that calculate and use elapsed duration.
The latter is preferable because in some cases `Time.now` can be
unstable, like if it's set manually by a system administrator or an NTP
daemon.

I don't expect that the previous code would actually have caused trouble
in the vast majority of normal situations, so I'm not going to backport
anything, but this seems like good hygiene.

For better or worse I had to wrap the monotonic time calls in a new
`Util` function because (1) the normal invocation is long enough to have
caused a lot of overruns on our 80 character line lengths, and (2)
Timecop doesn't stub the monotonic clock, so the `Util` method gives us
a nice place that we can stub on where necessary.
2019-10-01 09:58:50 -07:00
Brandur
f2b1fa150c Bump version to 5.3.0 2019-10-01 09:05:49 -07:00
Brandur
0544105fb8 Support Stripe-Should-Retry header (#853)
As seen in stripe-node, adds support for the `Stripe-Should-Retry`
header which is sent by the API when it explicitly would like us to
either retry or _not_ retry.

I'll add `Retry-After` separately at some point, but I punted it on it
for now given that we're not using it yet.

See: https://github.com/stripe/stripe-node/pull/692
2019-10-01 09:05:00 -07:00
Vasu Adari
e61793eea2 Fix warnings and typo in NestedResource (#852)
* Fix typo in NestedResource

* Fix warnings and indentation in NestedResource
2019-09-25 10:03:53 -07:00
Brandur
3cb9ad7fca Bump version to 5.2.0 2019-09-19 23:45:54 -07:00
Brandur
cbf44035b8 Introduce system for garbage collecting connection managers (#851)
Introduces a system for garbage collecting connection managers in an
attempt to solve #850.

Previously, the number of connection managers (and by extension the
number of connections that they were holding) would stay stable if a
program used a stable number of threads. However, if threads were used
disposably, the number of active connection managers out there could
continue to grow unchecked, and each of those could be holding one or
more dead connections which are no longer open, but still holding a file
descriptor waiting to be unlinked in disposed of by Ruby's GC.

This PR introduces a connection manager garbage collector that runs
periodically whenever a new connection manager is created. Connection
managers get a timestamp to indicate when they were last used, and the
GC runs through each one and prunes any that haven't seen use within a
certain threshold (currently, 120 seconds). This should have the effect
of removing connection managers as they're not needed anymore, and thus
resolving the socket leakage seen in #850.

I had to make a couple implementation tweaks to get this working
correctly. Namely:

* The `StripeClient` class now tracks thread contexts instead of
  connection managers. This is so that when we're disposing of a
  connection manager, we can set `default_connection_manager` on its
  parent thread context to `nil` so that it's not still tracking a
  connection manager that we're trying to get rid of.

* `StripeClient` instances can still be instantiated as before, but no
  longer internalize a reference to their own connection manager,
  instead falling back to the one in the current thread context. The
  rationale is that when trying to dispose of a connection manager, we'd
  also have to dispose of its reference in any outstanding
  `StripeClient` instances that might still be tracking it, and that
  starts to get a little unwieldy. I've left `#connection_manager` in
  place for backwards compatibility, but marked it as deprecated.
2019-09-19 23:43:49 -07:00
Brandur
70be52cc2b Bump version to 5.1.1 2019-09-04 14:22:58 -07:00
Alica Moser
b7495eb2e4 Transfer the request_id from the http_headers to error. (#845)
When requesting error#request_id it returned nil although the request id was available in the request headers. With version 5 and switch to Net::HTTP this value is now a string, not a symbol which means that the request_id was not stored on the error correctly.
2019-09-04 14:13:53 -07:00
Brandur
44451725e4 Bump version to 5.1.0 2019-08-27 14:25:25 -07:00
Brandur
3e21fa977a Retry requests on a 429 that's a lock timeout (#841)
Tweaks the retry logic so that 429s which have the special status code
lock_timeout are retried automatically.

Similar to what was recently implemented in Go: stripe/stripe-go#935
2019-08-27 14:22:55 -07:00
Brandur
82b5e510b9 Extract SSL verify none warning to its own method (#837)
A tiny refactor where we extract the warning you get when setting SSL
verify mode to `VERIFY_NONE` into its own method. No real impetus for
this except that it extracts very nicely, and that gets us to a smaller
`create_connection` method.
2019-08-20 14:19:36 -07:00
Brandur
5b975f66ee Bump version to 5.0.1 2019-08-20 13:55:24 -07:00
Brandur
650114abca Increase connection keep alive timeout to 30 seconds (#836)
One thing I forgot to look into was how long Ruby will hold a connection
open by default. It turns out that the language default is very low at
only two seconds. Here we increase it to 30 seconds, which is a more
reasonable default. I took this number from Go's `DefaultTransport`,
which seems to have been working pretty well so far.

I tested with a script that keeps a connection idle to Stripe for a long
period of time before issuing a new request and everything seems to be
working well.
2019-08-20 13:54:34 -07:00
Brandur
1f0a3d3ec8 Tweak comment to call out .clear (new since it was written) 2019-08-20 13:52:03 -07:00
Olivier Bellone
b31632aec9
Bump version to 5.0.0 2019-08-20 12:09:17 -07:00
Olivier Bellone
6fc69414e2
[codegen] Run codegen for stripe-ruby v5 (#835)
* Codegen for openapi 63fe31a

* Fix test
2019-08-20 12:02:20 -07:00
Brandur
44766516d9 stripe-ruby V5 (#815)
* Convert library to use built-in `Net::HTTP`

Moves the library off of Faraday and over onto the standard library's
built-in `Net::HTTP` module. The upside of the transition is that we
break away from a few dependencies that have caused us a fair bit of
trouble in the past, the downside is that we need more of our own code
to do things (although surprisingly, not that much more).

The biggest new pieces are:

* `ConnectionManager`: A per-thread class that manages a connection to
  each Stripe infrastructure URL (like `api.stripe.com`,
  `connect.stripe.com`, etc.) so that we can reuse them between
  requests. It's also responsible for setting up and configuring new
  `Net::HTTP` connections, which is a little more heavyweight
  code-wise compared to other libraries. All of this could have lived in
  `StripeClient`, but I extracted it because that class has gotten so
  big.

* `MultipartEncoder`: A class that does multipart form encoding for file
  uploads. Unfortunately, Ruby doesn't bundle anything like this. I
  built this by referencing the Go implementation because the original
  RFC is not very detailed or well-written. I also made sure that it was
  behaving similarly to our other custom implementations like
  stripe-node, and that it can really upload a file outside the test
  suite.

There's some risk here in that it's easy to miss something across one of
these big transitions. I've tried to test out various error cases
through tests, but also by leaving scripts running as I terminate my
network connection and bring it back. That said, we'd certainly release
on a major version bump because some of the interface (like setting
`Stripe.default_client`) changes.

* Drop support for old versions of Ruby

Drops support for Ruby 2.1 (EOL March 31, 2017) and 2.2 (EOL March 31,
2018). They're removed from `.travis.yml` and the gemspec and RuboCop
configuration have also been updated to the new lower bound.

Most of the diff here are minor updates to styling as required by
RuboCop:

* String literals are frozen by default, so the `.freeze` we had
  everywhere is now considered redundant.

* We can now use Ruby 1.9 style hash syntax with string keys like `{
  "foo": "bar" }`.

* Converted a few heredocs over to use squiggly (leading whitespace
  removed) syntax.

As discussed in Slack, I didn't drop support for Ruby 2.3 (EOL March 31,
2019) as we still have quite a few users on it. As far as I know
dropping it doesn't get us access to any major syntax improvements or
anything, so it's probably not a big deal.

* Make `CardError`'s `code` parameter named instead of positional (#816)

Makes the `code` parameter on `CardError` named instead of positional.
This makes it more consistent with the rest of the constructor's
parameters and makes instantiating `CardError` from `StripeClient`
cleaner.

This is a minor breaking change so we're aiming to release it for the
next major version of stripe-ruby.

* Bump Rubocop to latest version (#818)

* Ruby minimum version increase followup (#819)

* Remove old deprecated methods (#820)

* Remove all alias for list methods (#823)

* Remove UsageRecord.create method (#826)

* Remove IssuerFraudRecord (#827)

* Add ErrorObject to StripeError exceptions (#811)

* Tweak retry logic to be a little more like stripe-node (#828)

Tweaks the retry logic to be a little more like stripe-node's. In
particular, we also retry under these conditions:

* If we receive a 500 on a non-`POST` request.
* If we receive a 503.

I made it slightly different from stripe-node which checks for a 500
with `>= 500`. I don't really like that -- if we want to retry specific
status codes we should be explicit about it.

We're actively re-examining ways on how to make it easier for clients to
figure out when to retry right now, but I figure V5 is a good time to
tweak this because the modifications change the method signature of
`should_retry?` slightly, and it's technically a public method.

* Fix inverted sign for 500 retries (#830)

I messed up in #828 by (1) accidentally flipping the comparison against
`:post` when checking whether to retry on 500, and (2) forgetting to
write new tests for the condition, which is how (1) got through.

This patch fixes both those problems.

* Remove a few more very old deprecated methods (#831)

I noticed that we had a couple of other deprecated methods on `Stripe`
and `StripeObject` that have been around for a long time. May as well
get rid of them too -- luckily they were using `Gem::Deprecate` so
they've been producing annoying deprecated warnings for quite a while
now.

* Remove extraneous slash at the end of the line

* Reset connections when connection-changing configuration changes (#829)

Adds a few basic features around connection and connection manager
management:

* `clear` on connection manager, which calls `finish` on each active
  connection and then disposes of it.

* A centralized cross-thread tracking system for connection managers in
  `StripeClient` and `clear_all_connection_managers` which clears all
  known connection managers across all threads in a thread-safe way.

The addition of these allow us to modify the implementation of some of
our configuration on `Stripe` so that it can reset all currently open
connections when its value changes.

This fixes a currently problem with the library whereby certain
configuration must be set before the first request or it remains fixed
on any open connections. For example, if `Stripe.proxy` is set after a
request is made from the library, it has no effect because the proxy
must have been set when the connection was originally being initialized.

The impetus for getting this out is that I noticed that we will need
this internally in a few places when we're upgrading to stripe-ruby V5.
Those spots used to be able to hack around the unavailability of this
feature by just accessing the Faraday connection directly and resetting
state on it, but in V5 `StripeClient#conn` is gone, and that's no longer
possible.

* Minor cleanup in `StripeClient` (#832)

I ended up having to relax the maximum method line length in a few
previous PRs, so I wanted to try one more cleanup pass in
`execute_request` to see if I could get it back at all.

The answer was "not by much" (without reducing clarity), but I found a
few places that could be tweaked. Unfortunately, ~50 lines is probably
the "right" length for this method in that you _could_ extract it
further, but you'd end up passing huge amounts of state all over the
place in method parameters, and it really wouldn't look that good.

* Do better bookkeeping when tracking state in `Thread.current` (#833)

This is largely just another cleanup patch, but does a couple main
things:

* Hoists the `last_response` value into thread state. This is a very
  minor nicety, but effectively makes `StripeClient` fully thread-safe,
  which seems like a minor nicety. Two calls to `#request` to the same
  `StripeObject` can now be executed on two different threads and their
  results won't interfere with each other.

* Moves state off one-off `Thread.current` keys and into a single one
  for the whole client which stores a new simple type of record called
  `ThreadContext`. Again, this doesn't change much, but adds some minor
  type safety and lets us document each field we expect to have in a
  thread's context.

* Add Invoice.list_upcoming_line_items method (#834)
2019-08-20 11:35:24 -07:00
Olivier Bellone
2a78934d15
Bump version to 4.24.0 2019-08-12 17:21:14 -07:00
Olivier Bellone
14d470cffc
[codegen] Update API Resources (#825)
* Codegen for openapi c806fa1

* Add tests
2019-08-12 17:15:29 -07:00
Alex Rattray
bc77404518 Bump version to 4.23.0 2019-08-09 17:32:44 -07:00
Alex Rattray (Stripe)
1272a3fc94
[codegen] Remove SubscriptionScheduleRevision (#824)
* Codegen for openapi c806fa1

* Remove tests and standalone resource for SubscriptionScheduleRevision

* Fix rubocop
2019-08-09 15:57:03 -07:00
Alex Rattray
8d4c744b97 Bump version to 4.22.1 2019-08-09 12:25:19 -07:00
Alex Rattray
69b5e6e1c3 Rename to request_stripe_object, use with codegen 2019-08-06 17:23:11 -07:00
Alex Rattray
5e8faa28ed wip 2019-08-02 12:34:53 -07:00
Alex Rattray
bb55352658 Fix issuing card details, which must have been broken prior to ea736eb 2019-08-02 12:34:53 -07:00
Alex Rattray
43756df814 Revert "Use Util.convert_to_stripe_object instead of initialize_from"
This reverts commit ea736eba1b8f33d8febbf0b1be0957f34f7b04db.
2019-08-02 12:34:40 -07:00