Sha256: 1ea058073a58653d7455930f6df946d5a2cc2daa74f5257d0529bc7703fc6c8a

Contents?: true

Size: 1.41 KB

Versions: 4

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

require 'faraday'
require 'faraday/multipart'

module GenAI
  module Api
    class Client
      def initialize(url:, token:, headers: {})
        @url = url
        @token = token
        @headers = headers
      end

      def post(path, body, options = {})
        multipart = options.delete(:multipart) || false
        payload = multipart ? body : JSON.generate(body)

        handle_response do
          connection(multipart: multipart).post(path, payload)
        end
      end

      def get(path, options)
        handle_response do
          connection.get(path, options)
        end
      end

      private

      attr_reader :url, :token, :headers

      def connection(multipart: false)
        Faraday.new(url: url, headers: build_headers(token, headers, multipart)) do |conn|
          conn.request :multipart if multipart
          conn.request :url_encoded
        end
      end

      def build_headers(token, headers, multipart)
        hash = {
          'Accept' => 'application/json',
          'Content-Type' => multipart ? 'multipart/form-data' : 'application/json'
        }
        hash['Authorization'] = "Bearer #{token}" if token
        hash.merge(headers)
      end

      def handle_response
        response = yield

        raise GenAI::ApiError, "API error: #{JSON.parse(response.body)}" unless response.success?

        JSON.parse(response.body)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gen-ai-0.4.3 lib/gen_ai/api/client.rb
gen-ai-0.4.2 lib/gen_ai/api/client.rb
gen-ai-0.4.1 lib/gen_ai/api/client.rb
gen-ai-0.4.0 lib/gen_ai/api/client.rb