0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-16 06:40:29 +01:00
discourse-custom-wizard/lib/custom_wizard/api/endpoint.rb

114 Zeilen
3,1 KiB
Ruby

2021-03-11 07:30:15 +01:00
# frozen_string_literal: true
2019-05-31 09:54:11 +02:00
class CustomWizard::Api::Endpoint
include ActiveModel::SerializerSupport
attr_accessor :id,
:name,
:api_name,
2019-05-31 09:54:11 +02:00
:method,
:url,
:content_type,
:success_codes
2019-05-31 09:54:11 +02:00
2021-03-11 07:30:15 +01:00
def initialize(api_name, data = {})
@api_name = api_name
data.each do |k, v|
self.send "#{k}=", v if self.respond_to?(k)
2019-05-31 09:54:11 +02:00
end
end
def self.set(api_name, new_data)
2019-06-03 09:09:24 +02:00
if new_data['id']
data = self.get(api_name, new_data['id'], data_only: true)
endpoint_id = new_data['id']
else
data = {}
endpoint_id = SecureRandom.hex(3)
end
2019-05-31 09:54:11 +02:00
new_data.each do |k, v|
data[k.to_sym] = v
2019-05-31 09:54:11 +02:00
end
PluginStore.set("custom_wizard_api_#{api_name}", "endpoint_#{endpoint_id}", data)
2019-05-31 09:54:11 +02:00
self.get(api_name, endpoint_id)
2019-05-31 09:54:11 +02:00
end
2021-03-11 07:30:15 +01:00
def self.get(api_name, endpoint_id, opts = {})
2019-05-31 09:54:11 +02:00
return nil if !endpoint_id
if data = PluginStore.get("custom_wizard_api_#{api_name}", "endpoint_#{endpoint_id}")
if opts[:data_only]
data
else
2019-06-03 09:09:24 +02:00
data[:id] = endpoint_id
self.new(api_name, data)
end
else
nil
end
end
def self.remove(api_name)
PluginStoreRow.where("plugin_name = 'custom_wizard_api_#{api_name}' AND key LIKE 'endpoint_%'").destroy_all
2019-05-31 09:54:11 +02:00
end
2019-06-03 09:09:24 +02:00
def self.list(api_name)
PluginStoreRow.where("plugin_name LIKE 'custom_wizard_api_#{api_name}' AND key LIKE 'endpoint_%'")
2019-05-31 09:54:11 +02:00
.map do |record|
api_name = record['plugin_name'].sub("custom_wizard_api_", "")
2019-05-31 09:54:11 +02:00
data = ::JSON.parse(record['value'])
data[:id] = record['key'].split('_').last
self.new(api_name, data)
2019-05-31 09:54:11 +02:00
end
end
2019-06-03 09:09:24 +02:00
def self.request(user = Discourse.system_user, api_name, endpoint_id, body)
2019-06-03 09:09:24 +02:00
endpoint = self.get(api_name, endpoint_id)
auth_string = CustomWizard::Api::Authorization.authorization_string(api_name)
content_type = endpoint.content_type
2021-03-11 07:30:15 +01:00
headers = {}
headers["Authorization"] = auth_string if auth_string
headers["Content-Type"] = content_type if content_type
2019-06-03 09:09:24 +02:00
connection = Excon.new(UrlHelper.encode_and_parse(endpoint.url), headers: headers)
2019-06-03 09:09:24 +02:00
params = { method: endpoint.method }
2019-06-03 09:09:24 +02:00
if body
if content_type === "application/json"
body = JSON.generate(body)
elsif content_type === "application/x-www-form-urlencoded"
body = URI.encode_www_form(body)
2021-03-11 07:30:15 +01:00
end
2019-06-03 09:09:24 +02:00
params[:body] = body
end
response = connection.request(params)
2021-03-11 07:30:15 +01:00
if endpoint.success_codes.include?(response.status)
begin
result = JSON.parse(response.body)
rescue JSON::ParserError
result = response.body
end
CustomWizard::Api::LogEntry.set(api_name, log_params(user, 'SUCCESS', endpoint.url))
2021-03-11 07:30:15 +01:00
result
else
message = "API request failed"
CustomWizard::Api::LogEntry.set(api_name, log_params(user, 'FAIL', endpoint.url, message))
{ error: message }
end
2019-06-03 09:09:24 +02:00
end
2021-03-11 07:30:15 +01:00
def self.log_params(user, status, url, message = "")
{ time: Time.now, user_id: user.id, status: status, url: url, error: message }
end
2019-05-31 09:54:11 +02:00
end