0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-25 02:30:28 +01:00
discourse-custom-wizard/app/controllers/custom_wizard/admin/api.rb

156 Zeilen
3,8 KiB
Ruby

2021-03-11 07:30:15 +01:00
# frozen_string_literal: true
2020-04-13 14:17:22 +02:00
class CustomWizard::AdminApiController < CustomWizard::AdminController
2019-05-31 09:54:11 +02:00
skip_before_action :check_xhr, only: [:redirect]
2024-10-16 14:18:23 +02:00
requires_plugin "discourse-custom-wizard"
2019-05-31 09:54:11 +02:00
def list
2024-10-16 13:52:03 +02:00
serializer =
ActiveModel::ArraySerializer.new(
CustomWizard::Api.list,
each_serializer: CustomWizard::BasicApiSerializer,
)
2019-05-31 09:54:11 +02:00
render json: MultiJson.dump(serializer)
end
def find
2024-10-16 13:52:03 +02:00
render_serialized(
CustomWizard::Api.get(api_params[:name]),
CustomWizard::ApiSerializer,
root: false,
)
2019-05-31 09:54:11 +02:00
end
def save
current = CustomWizard::Api.get(api_params[:name])
2019-05-31 09:54:11 +02:00
if api_params[:new] && current
2024-10-16 13:52:03 +02:00
raise Discourse::InvalidParameters,
"An API with that name already exists: '#{current.title || current.name}'"
2019-06-01 01:06:30 +02:00
end
2019-05-31 09:54:11 +02:00
unless subscription.includes?(:api, :all)
raise Discourse::InvalidParameters, "Your subscription doesn't include API features."
end
PluginStoreRow.transaction do
CustomWizard::Api.set(api_params[:name], title: api_params[:title])
if auth_data.present?
2024-10-16 13:52:03 +02:00
auth_data["auth_params"] = auth_data["auth_params"] || []
CustomWizard::Api::Authorization.set(api_params[:name], auth_data)
end
if api_params[:endpoints].is_a? String
begin
endpoints = JSON.parse(api_params[:endpoints])
2024-10-16 13:52:03 +02:00
endpoints.each { |endpoint| CustomWizard::Api::Endpoint.set(api_params[:name], endpoint) }
rescue => e
puts e
end
2019-06-01 01:06:30 +02:00
end
2019-05-31 09:54:11 +02:00
end
2024-10-16 13:52:03 +02:00
render json:
success_json.merge(
api:
CustomWizard::ApiSerializer.new(
CustomWizard::Api.get(api_params[:name]),
root: false,
),
)
2019-05-31 09:54:11 +02:00
end
def remove
PluginStoreRow.transaction do
CustomWizard::Api.remove(api_params[:name])
CustomWizard::Api::Authorization.remove(api_params[:name])
CustomWizard::Api::Endpoint.remove(api_params[:name])
2019-06-06 18:10:13 +02:00
CustomWizard::Api::LogEntry.clear(api_params[:name])
end
2019-05-31 09:54:11 +02:00
render json: success_json
2019-06-06 18:10:13 +02:00
end
def authorize
result = CustomWizard::Api::Authorization.get_token(api_params[:name])
if result.instance_variable_defined?(:@error)
2024-10-16 13:52:03 +02:00
render json: failed_json.merge(message: result["error_description"] || result["error"])
else
2024-10-16 13:52:03 +02:00
render json:
success_json.merge(
api:
CustomWizard::ApiSerializer.new(
CustomWizard::Api.get(api_params[:name]),
root: false,
),
)
end
end
2019-06-06 18:10:13 +02:00
def clearlogs
CustomWizard::Api::LogEntry.clear(api_params[:name])
render json: success_json
end
def redirect
params.require(:name)
params.require(:code)
2019-05-31 09:54:11 +02:00
CustomWizard::Api::Authorization.set(params[:name], code: params[:code])
CustomWizard::Api::Authorization.get_token(params[:name])
2019-05-31 09:54:11 +02:00
2024-10-16 13:52:03 +02:00
redirect_to path("/admin/wizards/apis/" + params[:name])
2019-05-31 09:54:11 +02:00
end
private
def api_params
params.require(:name)
2024-10-16 13:52:03 +02:00
data =
params.permit(
:name,
:title,
:auth_type,
:auth_url,
:token_url,
:client_id,
:client_secret,
:username,
:password,
:auth_params,
:endpoints,
:new,
).to_h
data[:name] = data[:name].underscore
@api_params ||= data
2019-05-31 09:54:11 +02:00
end
def auth_data
2024-10-16 13:52:03 +02:00
auth_data =
api_params.slice(
:auth_type,
:auth_url,
:token_url,
:client_id,
:client_secret,
:username,
:password,
:auth_params,
)
2024-10-16 13:52:03 +02:00
auth_data[:auth_params] = JSON.parse(auth_data[:auth_params]) if auth_data[
:auth_params
].present?
@auth_data ||= auth_data
2019-05-31 09:54:11 +02:00
end
def subscription
@subscription ||= CustomWizard::Subscription.new
end
2019-05-31 09:54:11 +02:00
end