Module: Foobara::HttpApiCommand

Includes:
Concern, Concerns::Url
Included in:
Ai::AnthropicApi::BaseCommand, Ai::OllamaApi::BaseCommand, Ai::OpenAiApi::BaseCommand
Defined in:
foobara-http-api-command-0.0.9/src/foobara/concerns/url.rb,
foobara-http-api-command-0.0.9/src/foobara/http_api_command.rb

Defined Under Namespace

Modules: Concerns

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::Url

#api_uri_object, #api_url, #net_http

Methods included from Concern

foobara_class_methods_module_for, foobara_concern?, included

Instance Attribute Details

#request_bodyObject

Returns the value of attribute request_body.



18
19
20
# File 'foobara-http-api-command-0.0.9/src/foobara/http_api_command.rb', line 18

def request_body
  @request_body
end

#request_headersObject

Returns the value of attribute request_headers.



18
19
20
# File 'foobara-http-api-command-0.0.9/src/foobara/http_api_command.rb', line 18

def request_headers
  @request_headers
end

#responseObject

Returns the value of attribute response.



18
19
20
# File 'foobara-http-api-command-0.0.9/src/foobara/http_api_command.rb', line 18

def response
  @response
end

#response_bodyObject

Returns the value of attribute response_body.



18
19
20
# File 'foobara-http-api-command-0.0.9/src/foobara/http_api_command.rb', line 18

def response_body
  @response_body
end

Instance Method Details

#build_request_bodyObject



20
21
22
# File 'foobara-http-api-command-0.0.9/src/foobara/http_api_command.rb', line 20

def build_request_body
  self.request_body = {}
end

#build_request_headersObject



24
25
26
27
28
# File 'foobara-http-api-command-0.0.9/src/foobara/http_api_command.rb', line 24

def build_request_headers
  self.request_headers = {
    "Content-Type" => "application/json"
  }
end

#build_resultObject



55
56
57
# File 'foobara-http-api-command-0.0.9/src/foobara/http_api_command.rb', line 55

def build_result
  response_body
end

#executeObject



10
11
12
13
14
15
16
# File 'foobara-http-api-command-0.0.9/src/foobara/http_api_command.rb', line 10

def execute
  build_request_body
  build_request_headers
  issue_http_request
  parse_response
  build_result
end

#issue_http_requestObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'foobara-http-api-command-0.0.9/src/foobara/http_api_command.rb', line 30

def issue_http_request
  request = case self.class.http_method
            when :get
              uri = if request_body.empty?
                      api_uri_object
                    else
                      api_uri_object.dup.tap do |new_uri|
                        new_uri.query = URI.encode_www_form(request_body)
                      end
                    end

              Net::HTTP::Get.new(uri.request_uri, request_headers)
            when :post
              Net::HTTP::Post.new(api_uri_object.request_uri, request_headers).tap do |post|
                post.body = JSON.generate(request_body)
              end
            else
              # :nocov:
              raise "Unknown http method #{self.class.http_method}"
              # :nocov:
            end

  self.response = net_http.request(request)
end

#parse_responseObject



59
60
61
62
63
64
65
66
67
68
69
70
# File 'foobara-http-api-command-0.0.9/src/foobara/http_api_command.rb', line 59

def parse_response
  json = if response.is_a?(Net::HTTPSuccess)
           response.body
         else
           # :nocov:
           raise "Could not successfully hit #{api_url}: " \
                 "#{response.code} #{response.message}"
           # :nocov:
         end

  self.response_body = JSON.parse(json)
end