Class: Openapi3Invoker::Client
- Inherits:
-
Object
- Object
- Openapi3Invoker::Client
- Defined in:
- lib/openapi3_invoker/client.rb
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#connection_opts ⇒ Object
readonly
Returns the value of attribute connection_opts.
-
#openapi ⇒ Object
readonly
Returns the value of attribute openapi.
Instance Method Summary collapse
- #connection ⇒ Object
-
#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.
-
#initialize(openapi, base_url: nil, connection_opts: {}) ⇒ Client
constructor
A new instance of Client.
- #invoke(path_name, method_name, parameters: {}, headers: {}, body: nil) ⇒ Object
- #supported?(path_name, method_name) ⇒ Boolean
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_url ⇒ Object (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_opts ⇒ Object (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 |
#openapi ⇒ Object (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
#connection ⇒ Object
80 81 82 83 84 85 |
# File 'lib/openapi3_invoker/client.rb', line 80 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
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/openapi3_invoker/client.rb', line 56 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
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/openapi3_invoker/client.rb', line 27 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? # parser.components.schemas["Smiles"].properties.each { |name, prop| puts "#{name}, #{prop.type}" } 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 "post" when "delete", "put", "patch", "head", "trace", "options" raise Error::NotYetImplemented.new("Only GET requests are currently supported") end [response.status, response.headers.to_h.transform_keys(&:downcase), response.body] end |
#supported?(path_name, method_name) ⇒ Boolean
18 19 20 21 22 23 24 25 |
# File 'lib/openapi3_invoker/client.rb', line 18 def supported?(path_name, method_name) 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? end |