Sha256: 96ab651bd73aca0d942e53554541df169380ee6f65cb953159471b94296056e1

Contents?: true

Size: 1.4 KB

Versions: 3

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true
module Wazuh
  module Sawyer
    module Request
      def get(path, options = {})
        request(:get, path, options)
      end

      def post(path, options = {})
        request(:post, path, options)
      end

      def put(path, options = {})
        request(:put, path, options)
      end

      def delete(path, options = {})
        request(:delete, path, options)
      end

      def offset_request(method, path, options = {})
        items = []
        data = send(method, path, options)
        total_items = api_version == 3 ? data.totalItems : data.total_affected_items
        0.step(total_items, 500) { |offset|
          options[:offset] = offset
          d = send(method, path, options)
          _items = api_version == 3 ? data.items : d.affected_items
          items.concat(_items)
        }

        items
      end


      private

      def request(method, path, options, query_options={})
        response = case method
        when :get, :delete
          connection.call(method, URI::Parser.new.escape(path), nil, {query: options})
        when :post, :put
          data = options unless options.empty?
          connection.call(method, URI::Parser.new.escape(path), data, {query: query_options})
        end

        return response.data.data if response.status == 200

        raise Wazuh::Api::Errors::WazuhError.new(response.body, response)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wazuh-ruby-client-0.3.4 lib/wazuh/sawyer/request.rb
wazuh-ruby-client-0.3.3 lib/wazuh/sawyer/request.rb
wazuh-ruby-client-0.3.2 lib/wazuh/sawyer/request.rb