Sha256: 670bf828f20378d442b6cbc144491a188f089c4e76d4711b3ee711df76dfbd34

Contents?: true

Size: 1.88 KB

Versions: 2

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

require_relative 'rest_client'
require 'uploadcare/concern/error_handler'
require 'uploadcare/concern/throttle_handler'
require 'param/authentication_header'

module Uploadcare
  module Client
    # @abstract
    # General client for signed REST requests
    class RestClient < ApiStruct::Client
      include Uploadcare::Concerns::ErrorHandler
      include Uploadcare::Concerns::ThrottleHandler
      include Exception

      alias api_struct_delete delete
      alias api_struct_get get
      alias api_struct_post post
      alias api_struct_put put

      # Send request with authentication header
      #
      # Handle throttling as well
      def request(method: 'GET', uri:, **options)
        request_headers = Param::AuthenticationHeader.call(method: method.upcase, uri: uri,
                                                           content_type: headers[:'Content-type'], **options)
        handle_throttling do
          send('api_struct_' + method.downcase, path: remove_trailing_slash(uri),
                                                headers: request_headers, body: options[:content])
        end
      end

      def get(**options)
        request(method: 'GET', **options)
      end

      def post(**options)
        request(method: 'POST', **options)
      end

      def put(**options)
        request(method: 'PUT', **options)
      end

      def delete(**options)
        request(method: 'DELETE', **options)
      end

      def api_root
        Uploadcare.config.rest_api_root
      end

      def headers
        {
          'Content-type': 'application/json',
          'Accept': 'application/vnd.uploadcare-v0.5+json',
          'User-Agent': Uploadcare::Param::UserAgent.call
        }
      end

      private

      def remove_trailing_slash(str)
        str.gsub(%r{^\/}, '')
      end

      def default_params
        {}
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
uploadcare-ruby-3.0.5 lib/uploadcare/client/rest_client.rb
uploadcare-ruby-3.0.3 lib/uploadcare/client/rest_client.rb