Sha256: 4d008d09643cdef6b77f55c143f4f10d8d27b95461c62e10fb360330565c434c

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

require 'json'

module Cloudflare
  module KV
    class RESTWrapper < Async::REST::Wrapper::Generic
      APPLICATION_OCTET_STREAM = 'application/octet-stream'
      APPLICATION_JSON = 'application/json'
			ACCEPT_HEADER = "#{APPLICATION_JSON}, #{APPLICATION_OCTET_STREAM}"

      def prepare_request(payload, headers)
        headers['accept'] ||= ACCEPT_HEADER

        if payload
          headers['content-type'] = APPLICATION_OCTET_STREAM
          ::Protocol::HTTP::Body::Buffered.new([payload.to_s])
        end
      end

      def parser_for(response)
        if response.headers['content-type'].start_with?(APPLICATION_OCTET_STREAM)
          OctetParser
        elsif response.headers['content-type'].start_with?(APPLICATION_JSON)
          JsonParser
        else
          Async::REST::Wrapper::Generic::Unsupported
        end
      end

      class OctetParser < ::Protocol::HTTP::Body::Wrapper
        def join
          super
        end
      end

      class JsonParser < ::Protocol::HTTP::Body::Wrapper
        def join
          JSON.parse(super, symbolize_names: true)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cloudflare-4.3.0 lib/cloudflare/kv/rest_wrapper.rb