80 Commits

Author SHA1 Message Date
Brandur
3fc5e5b351 Introduce new IdempotencyError type
A few weeks back a new error type `idempotency_error` was introduced in
the API. I put it in to respond to #503, but then forgot to add support
for it in this library. This patch introduces a new exception class that
represents it.
2017-12-07 17:39:14 -08:00
Brandur
2bc471d501 Fix encoding of arrays that are sent in query strings
As discussed in #608, we currently have a problem where Faraday
deconstructs a query string that we encode and strips out any of the
array index numbers that we added. It's not too clear on why it does
this, but it appears to be built in at a pretty low level and hard to
change.

I spent a little time on this and it turns out that we can avoid the bad
code by depending on Faraday's `params` accessor on a request instead of
trying to do the encoding ourselves. Webmock has a pretty hard time
detecting the difference, but you can see some before and after encoding
here.

Before:

```
I, [2017-12-06T17:41:23.083942 #36737]  INFO -- : get
http://localhost:12111/v1/invoices/upcoming?customer=cus_123&subscription_items%5B%5D%5Bplan%5D=gold&subscription_items%5B%5D%5Bquantity%5D=2
```

After:

```
I, [2017-12-06T17:42:12.727752 #37158]  INFO -- : get
http://localhost:12111/v1/invoices/upcoming?customer=cus_123&subscription_items%5B0%5D%5Bplan%5D=gold&subscription_items%5B0%5D%5Bquantity%5D=2
```

Honestly, some of this can still use a lot of cleanup: it's weird that
we manually encode parameters ourselves for bodies, but not for queries;
but I think this'll fix the problem for now.

Fixes #608.
2017-12-07 10:51:07 -08:00
Olivier Bellone
a210c5cd76
Ensure that each thread has its own client 2017-10-12 18:20:13 +02:00
Brandur
8ea866616b Log query string as well as body on log debug
This patch modifies the debugging-level logging logic slightly so that
if it's a `GET` request that includes a query string, we log that string
just like we would've for a request body on a `POST` or like.

This especially comes in handy when looking when trying to resolve
something like a problem with the upcoming invoices endpoint like we saw
in #576, but will be useful in a number of situations.
2017-10-04 13:00:35 -07:00
Brandur
cb198baaa3 Remove Rubocop TODO around guard clauses
Removes Rubocop TODO around guard clauses and fixes the outstanding
offenses.

This is starting to get into territory that feels of more dubious value
to me, but at least it did get me writing a couple more tests, so let's
see how it goes by keeping this on.
2017-09-28 09:32:44 -07:00
Brandur
7f85eea3ee Fix low hanging Rubocop TODOs
I wanted to see what fixing Rubocop TODOs was like, so I tried to
eliminate all the easy ones. Most of these were pretty easy, and the
changes required are relatively minimal.

Some of the stuff left is harder. Pretty much everything under
`Metrics/*` is going to be a pretty big yak shave. A few of the others
are just going to need a little more work (e.g. `Style/ClassVars` and
`Style/GuardClause`). Going to stop here for now.
2017-09-27 15:07:18 -07:00
Olivier Bellone
e02ff7f849
Start using RuboCop for linting 2017-09-27 21:28:25 +02:00
Brandur
1ca67cb954 Correct behavior for error logging
A few changes:

* Add a new `Util.log_error` method which will forward to the equivalent
  of `#error` on a logger.
* Move errors produced by `StripeClient` to use `Util.log_error`.
* Change standard stdout logging behavior to log to stderr in the case
  of `Util.log_error.
* Change `Stripe.log_level` values to be an enum in a similar fashion as
  the standard library's built in `Logger`.
2017-08-11 11:45:43 -07:00
Brandur
eb3671b067 Log Stripe-Account values with STRIPE_LOG
Hopefully the last tweak in a while, but a discussion on [1] tipped me
off that this was missing. Here we add a `Stripe-Account` for a request
and response to logging. Follows #566 and #567.

[1] https://github.com/stripe/stripe-node/issues/364
2017-08-03 14:02:06 -07:00
Brandur
441331bd82 Log num_retries with STRIPE_LOG
This one is minor, but I realized after shipping #566 that it would be
nice if the number of retries was also logged for every request. This
patch follows up #566 by adding that in.

I also renamed `retry_count` to `num_retries` because I subjectively
think this name is a little better.
2017-08-03 13:50:00 -07:00
Brandur
ce69d749e1 Implement STRIPE_LOG for stripe-ruby
Adds logging support for stripe-ruby in a similar way that we did it for
stripe-python [1], with the idea that users you can optionally get some
additional low-cost-to-configure logging for operational visibility or
debugging.

I made a few tweaks from the Python implementation (which I'll try to
contribute back to there):

* Added an elapsed parameter to responses so you can tell how long they
  lasted.
* Mixed in idempotency_key to all lines that users have a way to
  aggregate logs related to a request from start to finish.
* Standardized naming between different log lines as much as possible.
* Detect a TTY and produce output that's colorized and formatted.

[1] https://github.com/stripe/stripe-python/pull/269
2017-08-03 13:39:15 -07:00
Brandur
1417cb5bd1 Allow empty strings in API invocation parameters
Currently, with a normal API resource, you can unset fields by
specifying a `nil` to that field's setter:

``` ruby
c = Charge.retrieve('ch_123')
c.customer = nil
c.save
```

This actually gets serialized as the form `customer=` (i.e. an empty
string), but we had to use the empty string to handle unsets because
form encoding has no concept of a `nil`/`null`.

To try and prevent usage errors, we actually prevent you from setting
fields with an empty string:

``` ruby
c = Charge.retrieve('ch_123')
c.customer = '' # error! use nil instead
```

When specifying parameters though, this doesn't work anywhere nearly as
well because usage patterns like this are very common in Ruby:

``` ruby
charge_opts = {
  params[:amount],
  params[:currency],
  params[:customer],
}
charge = Charge.create(charge_opts)
```

Each one of `params` above may or may not be `nil`, so we've
traditionally filtered those fields out during the invocation of
`Charge.create`.

Recently, I suggested to Slava that we may be able to change this
behavior, and we ended up putting in a patch as part of #557. Users
brought to my attention that this would be far too disruptive of a
change in #560 though, and having thought about it more carefully, I
agree. There's also an argument that filtered `nil` values are just a
better API, especially in Ruby where patterns like the one above are
frequently in effect.

So the best thing I can think of currently is to leave things as they
were before #557, and just require that users use an explicit empty
string when passes in parameter hashes:

``` ruby
Charge.update(customer: '') # will try to unset customer
```

Empty strings will continue to error for `StripeObject` fields like they
always have.

I don't think this is a perfect solution by any means (the different
between values on `StripeObject` versus using parameters is weird), but
it's the least disruptive thing that I can think of right now that gets
us the functionality that we need for endpoints like
`/v1/invoices/upcoming`.

Fixes #560.
2017-07-27 13:47:25 -07:00
Brandur
24a1704f05 Improve error handling safety in the event of unrecognized OAuth error
It was brought up in #562 that in case we receive an OAuth error that we
don't know about, `specific_oauth_error` will fall through with a `nil`,
then picked up by `specific_api_error` which will always try to handle
the error as if it were a `Hash` (even if we know it's not!) and thus
lead to typing problems at runtime.

This patch throws a generic `OAuthError` in cases where a code comes
back that we don't recognize. I'm still crazy about the fact that we
don't have a better way of recognizing an OAuth error in particular, but
it should do the trick.
2017-07-27 09:01:53 -07:00
Yossef Mendelssohn
21036261fc handle invalid_client error code from deauth
Actually handle the error code and make the right error, instead of
sending it through the `specific_api_error`.
2017-07-26 13:19:50 -04:00
Slava Akhmechet
78178f72d0 Following up with Brandur's fix suggestion 2017-07-10 13:59:23 -07:00
Olivier Bellone
a645a78cd0 Add OAuth methods 2017-05-18 11:29:59 +02:00
Brandur
cdccdbe4e2 Remove private modifier
Ruby 2.0 doesn't seem to like this syntax unfortunately.
2017-04-18 05:07:39 -07:00
Brandur
06992ef370 Add app info to User-Agent as well 2017-04-17 12:57:16 -07:00
Brandur
6acd21ac48 Support "app info" for plugins in Ruby
Adds support for "app info" (a mechanism that allows a plugin's author
to identify that plugin) in Ruby. This is already supported in PHP and
we're adding it elsewhere.
2017-04-14 14:37:01 -07:00
Jay Hayes
20553e7422 Replace invalid message Faraday::Response#code with valid #status
The Faraday::Response object does not respond to #code. The correct
message is #status. It seems this case was previously unhandled by the
test suite as there was no case for the server responding "success" with
an invalid JSON body.
2017-03-16 06:24:45 -05:00
Brandur
77c8535fa3 Properly encode file uploads
As described in #506, file uploads were broken on the way over to
Faraday and unfortunately I didn't catch it because the file upload
creation test wasn't using a matcher that was strict enough to really
catch anything.

Here we add the multipart middleware to our Faraday connection, add a
compatibility layer to `FileUpload` so that we can support `File` like
the rest-client based version always did (Faraday generally expects an
`UploadIO` object), and then tighten up our tests so that we'd be able
to catch future regressions.

Fixes #506.
2017-02-21 17:35:21 -08:00
Brandur
d8f3609f25 Don't shadow outer variable 2017-02-14 12:19:16 -08:00
Brandur
4debb6ba5d Use one default connect per thread 2017-02-14 12:17:37 -08:00
Brandur
d4bcfd9f78 Refactor errors interfaces 2017-02-14 12:17:37 -08:00
Brandur
2ade248e32 All tests working! 2017-02-14 12:17:37 -08:00
Brandur
31020c4596 Move profiling information to SystemProfiler + merge tests 2017-02-14 12:17:37 -08:00
Brandur
de08a9b986 Lots of broken tests, but working implementation! 2017-02-14 12:17:37 -08:00
Brandur
41f58e3cde Functional and tested StripeClient 2017-02-14 12:17:37 -08:00
Brandur
1c780e2b3f Working test suite! 2017-02-14 12:17:37 -08:00
Brandur
1886d9a625 Move to an alternative system based on StripeClient 2017-02-14 12:17:37 -08:00