Class: Openapi3Invoker::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(openapi, base_url: nil, connection_opts: {}) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
# File 'lib/openapi3_invoker/client.rb', line 12

def initialize(openapi, base_url: nil, connection_opts: {})
  @openapi = openapi
  @connection_opts = connection_opts
  @base_url = base_url || openapi.servers.first.url
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



10
11
12
# File 'lib/openapi3_invoker/client.rb', line 10

def base_url
  @base_url
end

#connection_optsObject (readonly)

Returns the value of attribute connection_opts.



9
10
11
# File 'lib/openapi3_invoker/client.rb', line 9

def connection_opts
  @connection_opts
end

#openapiObject (readonly)

Returns the value of attribute openapi.



8
9
10
# File 'lib/openapi3_invoker/client.rb', line 8

def openapi
  @openapi
end

Instance Method Details

#connectionObject



67
68
69
70
71
72
# File 'lib/openapi3_invoker/client.rb', line 67

def connection
  adapter = connection_opts.delete(:adapter)
  @connection ||= Faraday.new(connection_opts) do |faraday|
    faraday.adapter( *adapter || Faraday.default_adapter )
  end
end

#full_url(path_name, openapi_method, parameters: {}) ⇒ Object

We’re expect this method to called from invoke, so much of the error checking has already happened This would normally be a protected method, but we’re making it public for testing purposes



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/openapi3_invoker/client.rb', line 43

def full_url(path_name, openapi_method, parameters: {})
  path_name = path_name.dup
  query_params = {}
  openapi_method.parameters.each do |parameter|
    case parameter.in
    when "path"
      value = parameters[parameter.name]
      raise Error::MissingParameter unless value
      path_name.gsub!("{#{parameter.name}}", value.to_s)
    when "query"
      value = parameters[parameter.name]
      query_params[parameter.name] = value if value
      raise Error::MissingParameter if(parameter.required? && !value)
    end
  end

  url = [base_url.chomp('/'), path_name].join('')
  uri = URI(url)
  unless query_params.empty?
    uri.query = URI.encode_www_form(query_params)
  end
  uri.to_s
end

#invoke(path_name, method_name, parameters: {}, headers: {}, body: nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/openapi3_invoker/client.rb', line 18

def invoke(path_name, method_name, parameters: {}, headers: {}, body: nil)
  raise Error::ServerNotSpecified unless base_url
  raise Error::InvalidMethod unless %w[get delete put patch post head trace options].include?(method_name.to_s.downcase)
  path = openapi.paths[path_name]
  raise Error::InvalidPath unless path
  method = path.send(method_name.to_s.downcase)
  raise Error::InvalidMethodPathCombo unless method

  raise Error::NotYetImplemented.new("Request Body is not yet supported") if method.request_body&.required?
  method.parameters.each do |parameter|
    raise Error::MissingParameter if(parameter.required? && !parameters.key?(parameter.name))
  end

  response = case method_name
  when "get"
    resulting_url = full_url(path_name, method, parameters: parameters)
    connection.get(resulting_url, headers)
  when "delete", "put", "patch", "post", "head", "trace", "options"
    raise Error::NotYetImplemented.new("Only GET requests are currently supported")
  end
  response.body
end