class BusinessInsightApiClient::Helpers::RESTClient

RESTClient helper, used as helper to interact with the API endpoints.

Constants

API_VERSION_PATH
DEFAULT_OPTIONS

Attributes

authorization[R]
base_uri[R]

@return [URI] the uri of the api.

content_type[R]

@return [String] the content type set

Public Class Methods

new(authorization, options = {}) click to toggle source

Creates a new RESTClient helper

@param [Authorization] auhtorization the authorization helper used to create requests. @param [Hash] options the options to create the RESTClient Helper with. @option options [String] :api_url ('api.nedap-bi.com') the api url. @option options [String] :content_type ('application/json') the content type of requests. @raise [URI::InvalidURIError] when the api_url given can not be parsed into an uri.

# File lib/business_insight_api_client/helpers/restclient.rb, line 34
def initialize(authorization, options = {})
  @authorization = authorization
  @base_uri = URI(options[:api_url] || DEFAULT_OPTIONS[:api_url])
  @content_type = options[:content_type] || DEFAULT_OPTIONS[:default_content_type]
end

Public Instance Methods

delete(path) click to toggle source
# File lib/business_insight_api_client/helpers/restclient.rb, line 68
def delete(path)
  build_path(path)
  execute do
    client.delete(@base_uri.to_s, header: access_token_header)
  end
end
get(path, *args, &block) click to toggle source
# File lib/business_insight_api_client/helpers/restclient.rb, line 40
def get(path, *args, &block)
  build_path(path)
  begin
    execute do
      arguments = argument_to_hash(args, :query, :body, :header, :follow_redirect) || {}
      arguments[:header] = access_token_header.merge(arguments[:header] || {})
      client.request(:get, @base_uri.to_s, arguments, &block)
    end
  rescue HTTPClient::TimeoutError
    raise ::BusinessInsightApiClient::Errors::RequestTimedOutError
  end

end
post(path, body = '') click to toggle source
# File lib/business_insight_api_client/helpers/restclient.rb, line 61
def post(path, body = '')
  build_path(path)
  execute do
    client.post(@base_uri.to_s, body: body, header: { 'Content-Type' => content_type, 'Accept' => content_type }.merge(access_token_header))
  end
end
put(path, body = '') click to toggle source
# File lib/business_insight_api_client/helpers/restclient.rb, line 54
def put(path, body = '')
  build_path(path)
  execute do
    client.put(@base_uri.to_s, body: body, header: { 'Content-Type' => content_type, 'Accept' => content_type }.merge(access_token_header))
  end
end

Private Instance Methods

access_token_header() click to toggle source
# File lib/business_insight_api_client/helpers/restclient.rb, line 97
def access_token_header
  authorization.current_token_headers
end
build_path(path) click to toggle source
# File lib/business_insight_api_client/helpers/restclient.rb, line 101
def build_path(path)
  @base_uri.path = "#{API_VERSION_PATH}#{path}"
end
client() click to toggle source
# File lib/business_insight_api_client/helpers/restclient.rb, line 105
def client
  @client ||= HTTPClient.new
end
execute() { || ... } click to toggle source

Executes an request and retries when an token could be expired. It refreshes the token before retrying.

@return [String] the response. @raise [::BusinessInsightApiClient::Errors] on request errors.

# File lib/business_insight_api_client/helpers/restclient.rb, line 82
def execute
  retries ||= 1
  response = yield
  raise_on_response_errors(response)
rescue ::BusinessInsightApiClient::Errors::UnauthorizedError => e
  if (retries -= 1) >= 0
    authorization.current_token = authorization.current_token.refresh!
    retry
  else
    raise
  end
else
  response.body
end
raise_on_response_errors(response) click to toggle source
# File lib/business_insight_api_client/helpers/restclient.rb, line 109
def raise_on_response_errors(response)
  case response.status
    when 400
      response_body = BusinessInsightApiClient::Mash.from_json(response.body)
      raise ::BusinessInsightApiClient::Errors::BadRequestError.new(response.status, response_body), "(#{response.status}): #{[*response_body.messages].join(', ')}"
    when 401
      response_body = BusinessInsightApiClient::Mash.from_json(response.body)
      raise ::BusinessInsightApiClient::Errors::UnauthorizedError.new(response.status, response_body), "(#{response.status}): #{[*response_body.messages].join(', ')}"
    when 403
      response_body = BusinessInsightApiClient::Mash.from_json(response.body)
      raise ::BusinessInsightApiClient::Errors::ForbiddenError.new(response.status, response_body), "(#{response.status}): #{[*response_body.messages].join(', ')}"
    when 404
      response_body = BusinessInsightApiClient::Mash.from_json(response.body)
      raise ::BusinessInsightApiClient::Errors::NotFoundError.new(response.status, response_body), "(#{response.status}): #{[*response_body.messages].join(', ')}"
    when 500
      raise ::BusinessInsightApiClient::Errors::InternalError.new(response.status, response.reason), "#{response.code} : #{response.reason}"
    when 501..503
      raise ::BusinessInsightApiClient::Errors::UnavailableError.new(response.status, response.reason), "#{response.code} : #{response.reason}"
  end
end