Class: VtApi::ApiProvider
- Inherits:
-
Object
- Object
- VtApi::ApiProvider
- Defined in:
- lib/vt_api/internal/api_provider.rb
Overview
API interaction class.
Uses [Faraday] for HTTP interaction.
Instance Method Summary collapse
-
#initialize(api_version = Versions::DEFAULT, adapter = :net_http) ⇒ Object
constructor
Create new ApiProvider.
-
#request(method_name, params = {}) ⇒ OpenStruct
Performs a request to given API method.
Constructor Details
#initialize(api_version = Versions::DEFAULT, adapter = :net_http) ⇒ Object
Create new ApiProvider.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/vt_api/internal/api_provider.rb', line 21 def initialize(api_version = Versions::DEFAULT, adapter = :net_http) unless api_version.is_a? ApiVersion raise ArgumentError, "Invalid API interface supplied! Must be subclass of 'ApiVersion', got #{api_version.class} instead" end @api = api_version @connection = Faraday.new @api.base_url do |conn| conn.request :multipart conn.request :url_encoded conn.adapter adapter end end |
Instance Method Details
#request(method_name, params = {}) ⇒ OpenStruct
Performs a request to given API method. Requested method must be described in API interface.
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/vt_api/internal/api_provider.rb', line 46 def request(method_name, params = {}) endpoint = endpoint(method_name, params) result = @connection.public_send(endpoint.method, endpoint.uri, params, {}) raise ApiError, "#{@api.version} error: #{@api.error(result.status)}" if @api.error? result.status # noinspection RubyResolve JSON.parse result.body, object_class: OpenStruct end |