2021-02-01 17:43:15 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-06-24 12:36:16 +02:00
|
|
|
# module definition
|
|
|
|
module OpenAPI::V1; end
|
|
|
|
|
2021-02-01 17:43:15 +01:00
|
|
|
# Parameters for OpenAPI endpoints
|
2022-11-23 17:35:39 +01:00
|
|
|
class OpenAPI::V1::BaseController < ActionController::Base # rubocop:disable Rails/ApplicationController
|
|
|
|
include ApplicationHelper
|
|
|
|
|
2016-05-04 11:59:51 +02:00
|
|
|
protect_from_forgery with: :null_session
|
2021-02-01 17:43:15 +01:00
|
|
|
skip_before_action :verify_authenticity_token
|
2016-05-04 11:59:51 +02:00
|
|
|
before_action :authenticate
|
|
|
|
before_action :increment_calls_count
|
|
|
|
|
|
|
|
rescue_from ActiveRecord::RecordNotFound, with: :not_found
|
|
|
|
rescue_from OpenAPI::ParameterError, with: :bad_request
|
|
|
|
rescue_from ActionController::ParameterMissing, with: :bad_request
|
|
|
|
|
2021-02-01 17:43:15 +01:00
|
|
|
rescue_from TypeError, with: :server_error
|
|
|
|
rescue_from NoMethodError, with: :server_error
|
|
|
|
rescue_from ArgumentError, with: :server_error
|
|
|
|
|
2016-05-04 11:59:51 +02:00
|
|
|
helper_method :current_api_client
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2021-02-01 17:43:15 +01:00
|
|
|
def not_found(exception)
|
2021-02-02 11:28:31 +01:00
|
|
|
render json: { errors: ['Not found', exception] }, status: :not_found
|
2021-02-01 17:43:15 +01:00
|
|
|
end
|
2016-05-04 11:59:51 +02:00
|
|
|
|
2021-02-01 17:43:15 +01:00
|
|
|
def bad_request(exception)
|
2021-02-02 11:28:31 +01:00
|
|
|
render json: { errors: ['Bad request', exception] }, status: :bad_request
|
2021-02-01 17:43:15 +01:00
|
|
|
end
|
2016-05-04 11:59:51 +02:00
|
|
|
|
2021-02-01 17:43:15 +01:00
|
|
|
def server_error(exception)
|
2021-02-02 11:28:31 +01:00
|
|
|
render json: { error: ['Server error', exception] }, status: :internal_server_error
|
2021-02-01 17:43:15 +01:00
|
|
|
end
|
2016-05-04 11:59:51 +02:00
|
|
|
|
2021-02-01 17:43:15 +01:00
|
|
|
def authenticate
|
|
|
|
authenticate_token || render_unauthorized
|
|
|
|
end
|
2016-05-04 11:59:51 +02:00
|
|
|
|
2021-02-01 17:43:15 +01:00
|
|
|
def authenticate_token
|
|
|
|
authenticate_with_http_token do |token, _options|
|
|
|
|
@open_api_client = OpenAPI::Client.find_by(token: token)
|
2016-05-04 11:59:51 +02:00
|
|
|
end
|
2021-02-01 17:43:15 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def current_api_client
|
|
|
|
@open_api_client
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_unauthorized
|
2022-11-23 17:35:39 +01:00
|
|
|
render json: { errors: ['Bad credentials'] }, status: :unauthorized
|
2021-02-01 17:43:15 +01:00
|
|
|
end
|
2016-05-04 11:59:51 +02:00
|
|
|
|
|
|
|
private
|
2021-02-01 17:43:15 +01:00
|
|
|
|
|
|
|
def increment_calls_count
|
|
|
|
@open_api_client.increment_calls_count
|
|
|
|
end
|
2016-05-04 11:59:51 +02:00
|
|
|
end
|