Class: Phaseo::Gen::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gen/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, headers: {}) ⇒ Client

Returns a new instance of Client.



28
29
30
31
# File 'lib/gen/client.rb', line 28

def initialize(base_url:, headers: {})
  @base_url = base_url.chomp("/")
  @headers = headers
end

Instance Method Details

#build_request(method:, path:, query: nil, headers: nil, body: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gen/client.rb', line 33

def build_request(method:, path:, query: nil, headers: nil, body: nil)
  uri = URI.join(@base_url + "/", path.sub(%r{^/}, ""))
  uri.query = URI.encode_www_form(query) if query && !query.empty?
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  request_class = Net::HTTP.const_get(method.capitalize)
  req = request_class.new(uri)
  (@headers || {}).merge(headers || {}).each { |k, v| req[k] = v }
  if body
    req["Content-Type"] = "application/json"
    req.body = JSON.dump(body)
  end
  [http, req]
end

#request(method:, path:, query: nil, headers: nil, body: nil) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/gen/client.rb', line 48

def request(method:, path:, query: nil, headers: nil, body: nil)
  http, req = build_request(method:, path:, query:, headers:, body:)
  response = http.request(req)
  unless response.is_a?(Net::HTTPSuccess)
    raise RequestError.new(status_code: response.code.to_i, response_body: response.body.to_s)
  end
  return nil if response.body.nil? || response.body.empty?
  JSON.parse(response.body)
end

#request_bytes(method:, path:, query: nil, headers: nil, body: nil) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/gen/client.rb', line 58

def request_bytes(method:, path:, query: nil, headers: nil, body: nil)
  http, req = build_request(method:, path:, query:, headers:, body:)
  response = http.request(req)
  unless response.is_a?(Net::HTTPSuccess)
    raise RequestError.new(status_code: response.code.to_i, response_body: response.body.to_s)
  end
  response.body.to_s.b
end

#request_stream(method:, path:, query: nil, headers: nil, body: nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/gen/client.rb', line 67

def request_stream(method:, path:, query: nil, headers: nil, body: nil)
  return enum_for(__method__, method:, path:, query:, headers:, body:) unless block_given?
  http, req = build_request(method:, path:, query:, headers: (headers || {}).merge("Accept" => "text/event-stream"), body:)
  http.request(req) do |response|
    unless response.is_a?(Net::HTTPSuccess)
      raise RequestError.new(status_code: response.code.to_i, response_body: response.body.to_s)
    end
    buffer = String.new
    response.read_body do |chunk|
      buffer << chunk
      while (index = buffer.index("\n"))
        yield buffer.slice!(0, index + 1).sub(/\r?\n\z/, "")
      end
    end
    yield buffer unless buffer.empty?
  end
end