From 948aa518b66db7312b859e3841194f9242dd7d42 Mon Sep 17 00:00:00 2001 From: Andreas Tiefenthaler Date: Wed, 28 Jan 2015 17:10:14 +0100 Subject: [PATCH 1/2] Add RateLimitExceeded(429) error response --- lib/contentful/error.rb | 5 ++ spec/error_requests_spec.rb | 11 +++- spec/fixtures/vcr_cassettes/ratelimit.yml | 64 +++++++++++++++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 spec/fixtures/vcr_cassettes/ratelimit.yml diff --git a/lib/contentful/error.rb b/lib/contentful/error.rb index 022682d..14c6dcd 100644 --- a/lib/contentful/error.rb +++ b/lib/contentful/error.rb @@ -21,6 +21,8 @@ def self.[](error_status_code) AccessDenied when 401 Unauthorized + when 429 + RateLimitExceeded when 500 ServerError when 503 @@ -43,6 +45,9 @@ class AccessDenied < Error; end # 401 class Unauthorized < Error; end + # 429 + class RateLimitExceeded < Error; end + # 500 class ServerError < Error; end diff --git a/spec/error_requests_spec.rb b/spec/error_requests_spec.rb index 33adc27..89f2547 100644 --- a/spec/error_requests_spec.rb +++ b/spec/error_requests_spec.rb @@ -21,7 +21,7 @@ client = Contentful::Client.new(space: 'wrong', access_token: 'credentials') expect_vcr('unauthorized'){ - client.entry 'nyancat' + client.entry('nyancat') }.to raise_error(Contentful::Unauthorized) end @@ -29,11 +29,18 @@ skip end + it 'will return a 429 if the ratelimit is hit' do + client = Contentful::Client.new(space: 'wrong', access_token: 'credentials') + expect_vcr('ratelimit') { + client.entry('nyancat') + }.to raise_error(Contentful::RateLimitExceeded) + end + it 'will return 503 (ServiceUnavailable) if connection time out' do client = Contentful::Client.new(space: 'wrong', access_token: 'credentials') expect_vcr('unavailable'){ - client.entry 'nyancat' + client.entry('nyancat') }.to raise_error(Contentful::ServiceUnavailable) end end diff --git a/spec/fixtures/vcr_cassettes/ratelimit.yml b/spec/fixtures/vcr_cassettes/ratelimit.yml new file mode 100644 index 0000000..c626b9a --- /dev/null +++ b/spec/fixtures/vcr_cassettes/ratelimit.yml @@ -0,0 +1,64 @@ +--- +http_interactions: +- request: + method: get + uri: https://cdn.contentful.com/spaces/wrong/entries/nyancat + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - RubyContentfulGem/0.1.1 + Authorization: + - Bearer credentials + Content-Type: + - application/vnd.contentful.delivery.v1+json + Host: + - cdn.contentful.com + response: + status: + code: 429 + message: ServiceUnavailable + headers: + Date: + - Wed, 16 Apr 2014 09:00:24 GMT + Server: + - nginx/1.1.19 + Access-Control-Allow-Headers: + - Accept,Accept-Language,Authorization,Cache-Control,Content-Length,Content-Range,Content-Type,DNT,Destination,Expires,If-Match,If-Modified-Since,If-None-Match,Keep-Alive,Last-Modified,Origin,Pragma,Range,User-Agent,X-Http-Method-Override,X-Mx-ReqToken,X-Requested-With,X-Contentful-Version,X-Contentful-Content-Type,X-Contentful-Organization + Access-Control-Allow-Methods: + - GET,HEAD,OPTIONS + Access-Control-Allow-Origin: + - "*" + Access-Control-Max-Age: + - '86400' + Cache-Control: + - max-age=0 + Content-Type: + - application/vnd.contentful.delivery.v1+json + Content-Length: + - '179' + Accept-Ranges: + - bytes + Via: + - 1.1 varnish + X-Served-By: + - cache-am72-AMS + X-Cache: + - MISS + X-Cache-Hits: + - '0' + body: + encoding: UTF-8 + string: | + { + "sys": { + "type": "Error", + "id": "RateLimitExceeded" + }, + "message": "Ratelimit Exceeded", + "requestId": "85f-649640521" + } + http_version: + recorded_at: Wed, 16 Apr 2014 09:00:24 GMT +recorded_with: VCR 2.9.0 From 6453ec7b35f869cea2802d78c2dfa3fce86bc3ca Mon Sep 17 00:00:00 2001 From: Andreas Tiefenthaler Date: Wed, 28 Jan 2015 17:56:27 +0100 Subject: [PATCH 2/2] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd31047..1a41ee4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased ### Fixed * Better handling of 503 responses from the API [#48](https://github.com/contentful/contentful-management.rb/pull/48) +* Better handling of 429 responses from the API [#51](https://github.com/contentful/contentful-management.rb/pull/51) ### Added * `focus` and `fit` to image handling parameters [#44](https://github.com/contentful/contentful.rb/pull/44)