Sha256: 815f583d2629a2c3cc07079abd1c17f5018b288d567cd6efcc2fadce71b75044

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

require "uri"
require "net/http"
require "openssl"
require "json"

module AI21
  module HTTP
    include AI21::Helper

    def get(path)
      fetch(path, :get)
    end

    def post(path, body)
      fetch(path, :post, body)
    end

    def delete(path)
      fetch(path, :delete)
    end

    def fetch(path, method, body = nil)
      url = url(path)
      http = http(url)
      request = request(url, method)

      request.body = body.to_json if body

      body = http.request(request).read_body
      camel_to_snake ::JSON.parse(body)
    end

    def request(url, method)
      request = Object.const_get("Net::HTTP::#{method.capitalize}").new(url)
      request["accept"] = content_type
      request["content-type"] = content_type
      request["Authorization"] = authorization
      request
    end

    def url(path)
      URI("#{AI21.configuration.uri_base}#{AI21.configuration.api_version}#{path}")
    end

    def http(url)
      http = Net::HTTP.new(url.host, url.port)
      http.use_ssl = true
      http
    end

    def authorization
      "Bearer #{AI21.configuration.access_token}"
    end

    def content_type
      "application/json"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ai21-0.2.1 lib/ai21/http.rb
ai21-0.2.0 lib/ai21/http.rb