Class: Utcp::Providers::HttpStreamProvider

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

Overview

Simple HTTP chunked transfer streaming

Instance Attribute Summary

Attributes inherited from BaseProvider

#auth, #name, #type

Instance Method Summary collapse

Constructor Details

#initialize(name:, auth: nil) ⇒ HttpStreamProvider

Returns a new instance of HttpStreamProvider.



12
13
14
# File 'lib/utcp/providers/http_stream_provider.rb', line 12

def initialize(name:, auth: nil)
  super(name: name, provider_type: "http_stream", auth: auth)
end

Instance Method Details

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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/utcp/providers/http_stream_provider.rb', line 20

def call_tool(tool, arguments = {}, &block)
  p = tool.provider
  url = Utils::Subst.apply(p["url"])
  method = (p["http_method"] || "GET").upcase
  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)
  end

  req = Net::HTTP.const_get(method.capitalize).new(uri)
  headers = { "Accept" => "application/json" }
  @auth&.apply_query(uri) if @auth&.respond_to?(:apply_query)
  @auth&.apply_headers(headers)
  headers.each { |k, v| req[k] = v }

  http = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https")
  begin
    http.request(req) do |res|
      res.read_body do |chunk|
        yield chunk if block_given?
      end
    end
    nil
  ensure
    http.finish if http.active?
  end
end

#discover_tools!Object

Raises:



16
17
18
# File 'lib/utcp/providers/http_stream_provider.rb', line 16

def discover_tools!
  raise ProviderError, "HTTP stream is an execution provider only"
end