Sha256: 90a6aba0afb299c7509a4bedc57a3fa2c8ba30c96e3dd25ca25e829613b53f7c

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 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)
        0.step(data.totalItems, 500) { |offset|
          options[:offset] = offset
          d = send(method, path, options)
          items.concat(d.items)
        }

        items
      end

      private

      def request(method, path, 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)
        end

        return response.data.data if response.status == 200 || response.data.error == 0
        
        error_message = response.data.message
        raise Wazuh::Api::Errors::WazuhError.new(error_message, response)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
wazuh-ruby-client-0.2.6 lib/wazuh/sawyer/request.rb
wazuh-ruby-client-0.2.5 lib/wazuh/sawyer/request.rb