lib/wcc/contentful/simple_client/response.rb in wcc-contentful-0.4.0.pre.rc vs lib/wcc/contentful/simple_client/response.rb in wcc-contentful-1.0.0.pre.rc1

- old
+ new

@@ -1,14 +1,19 @@ # frozen_string_literal: true +require_relative '../instrumentation' + class WCC::Contentful::SimpleClient class Response + include ::WCC::Contentful::Instrumentation + attr_reader :raw_response attr_reader :client attr_reader :request - delegate :code, to: :raw_response + delegate :status, to: :raw_response + alias_method :code, :status delegate :headers, to: :raw_response def body @body ||= raw_response.body.to_s end @@ -17,46 +22,62 @@ @raw ||= JSON.parse(body) end alias_method :to_json, :raw def error_message - raw.dig('message') || "#{code}: #{raw_response.message}" + parsed_message = + begin + raw.dig('message') + rescue JSON::ParserError + nil + end + parsed_message || "#{code}: #{raw_response.body}" end + def skip + raw['skip'] + end + + def total + raw['total'] + end + def next_page? return unless raw.key? 'items' - raw['items'].length + raw['skip'] < raw['total'] + page_items.length + skip < total end def next_page return unless next_page? + return @next_page if @next_page - @next_page ||= @client.get( - @request[:url], - (@request[:query] || {}).merge({ - skip: raw['items'].length + raw['skip'] - }) - ) - @next_page.assert_ok! + query = (@request[:query] || {}).merge({ + skip: page_items.length + skip + }) + np = + _instrument 'page', url: @request[:url], query: query do + @client.get(@request[:url], query) + end + @next_page = np.assert_ok! end def initialize(client, request, raw_response) @client = client @request = request @raw_response = raw_response @body = raw_response.body.to_s end def assert_ok! - return self if code >= 200 && code < 300 + return self if status >= 200 && status < 300 - raise ApiError[code], self + raise ApiError[status], self end def each_page(&block) - raise ArgumentError, 'Not a collection response' unless raw['items'] + raise ArgumentError, 'Not a collection response' unless page_items ret = Enumerator.new do |y| y << self @@ -73,23 +94,25 @@ ret.lazy end end def items - each_page.flat_map do |page| - page.raw['items'] - end + each_page.flat_map(&:page_items) end + def page_items + raw['items'] + end + def count - raw['total'] + total end def first - raise ArgumentError, 'Not a collection response' unless raw['items'] + raise ArgumentError, 'Not a collection response' unless page_items - raw['items'].first + page_items.first end def includes @includes ||= raw.dig('includes')&.each_with_object({}) do |(_t, entries), h| @@ -113,11 +136,17 @@ end def next_page return unless next_page? - @next_page ||= SyncResponse.new(@client.get(raw['nextPageUrl'])) + url = raw['nextPageUrl'] + next_page = + _instrument 'page', url: url do + @client.get(url) + end + + @next_page ||= SyncResponse.new(next_page) @next_page.assert_ok! end def next_sync_token # If we haven't grabbed the next page yet, then our next "sync" will be getting @@ -127,11 +156,11 @@ raw['nextPageUrl'] || raw['nextSyncUrl'] ) end def each_page - raise ArgumentError, 'Not a collection response' unless raw['items'] + raise ArgumentError, 'Not a collection response' unless page_items ret = Enumerator.new do |y| y << self @@ -166,10 +195,14 @@ def self.[](code) case code when 404 NotFoundError + when 401 + UnauthorizedError + when 429 + RateLimitError else ApiError end end @@ -178,7 +211,13 @@ super(response.error_message) end end class NotFoundError < ApiError + end + + class UnauthorizedError < ApiError + end + + class RateLimitError < ApiError end end