Class: Utcp::Providers::HttpProvider

Inherits:
BaseProvider show all
Defined in:
lib/utcp/providers/http_provider.rb

Instance Attribute Summary

Attributes inherited from BaseProvider

#auth, #name, #type

Instance Method Summary collapse

Constructor Details

#initialize(name:, url:, http_method: "GET", content_type: "application/json", headers: {}, manual: false, auth: nil, body_field: nil) ⇒ HttpProvider



14
15
16
17
18
19
20
21
22
# File 'lib/utcp/providers/http_provider.rb', line 14

def initialize(name:, url:, http_method: "GET", content_type: "application/json", headers: {}, manual: false, auth: nil, body_field: nil)
  super(name: name, provider_type: manual ? "http_manual" : "http", auth: auth)
  @url = Utils::Subst.apply(url)
  @http_method = http_method.upcase
  @content_type = content_type
  @headers = Utils::Subst.apply(headers || {})
  @manual = manual
  @body_field = body_field
end

Instance Method Details

#call_tool(tool, arguments = {}, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/utcp/providers/http_provider.rb', line 43

def call_tool(tool, arguments = {}, &block)
  # tool.provider is a hash containing execution provider details
  p = tool.provider
  url = Utils::Subst.apply(p["url"] || @url)
  method = (p["http_method"] || @http_method || "GET").upcase
  content_type = p["content_type"] || @content_type || "application/json"
  headers = Utils::Subst.apply(p["headers"] || {}).merge(default_headers)
  body_field = p["body_field"] || @body_field

  uri = URI(url)
  args = Utils::Subst.apply(arguments || {})
  if %w[GET DELETE].include?(method)
    q = URI.decode_www_form(uri.query || "") + args.to_a
    uri.query = URI.encode_www_form(q)
    req = Net::HTTP.const_get(method.capitalize).new(uri)
  else
    req = Net::HTTP.const_get(method.capitalize).new(uri)
    if body_field
      payload = { body_field => args }
    else
      payload = args
    end
    if content_type.include?("json")
      req.body = JSON.dump(payload)
      req["Content-Type"] = "application/json"
    else
      req.body = URI.encode_www_form(payload)
      req["Content-Type"] = "application/x-www-form-urlencoded"
    end
  end

  # auth
  headers = headers.transform_keys(&:to_s)
  apply_auth!(uri, headers)
  req["Cookie"] = [req["Cookie"], @auth&.apply_cookies].compact.join("; ") if @auth.respond_to?(:apply_cookies)
  headers.each { |k, v| req[k] = v }

  http = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https")
  begin
    res = http.request(req)
    # try to parse as JSON; fall back to raw string
    begin
      JSON.parse(res.body)
    rescue
      res.body
    end
  ensure
    http.finish if http.active?
  end
end

#discover_tools!Object

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/utcp/providers/http_provider.rb', line 24

def discover_tools!
  raise ProviderError, "Not a manual provider" unless @manual
  uri = URI(@url)
  req = Net::HTTP.const_get(@http_method.capitalize).new(uri)
  headers = default_headers
  apply_auth!(uri, headers)
  headers.each { |k, v| req[k] = v }

  http = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https")
  begin
    res = http.request(req)
    raise ProviderError, "Manual fetch failed: #{res.code}" unless res.is_a?(Net::HTTPSuccess)
    manual = JSON.parse(res.body)
    to_tools(manual)
  ensure
    http.finish if http.active?
  end
end