2019-05-31 09:54:11 +02:00
|
|
|
class CustomWizard::Api::Endpoint
|
|
|
|
include ActiveModel::SerializerSupport
|
|
|
|
|
|
|
|
attr_accessor :id,
|
2019-06-03 04:49:54 +02:00
|
|
|
:name,
|
|
|
|
:api_name,
|
2019-05-31 09:54:11 +02:00
|
|
|
:method,
|
|
|
|
:url
|
|
|
|
|
2019-06-03 04:49:54 +02: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
|
|
|
|
|
2019-06-03 04:49:54 +02:00
|
|
|
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
|
|
|
|
2019-06-03 04:49:54 +02:00
|
|
|
new_data.each do |k, v|
|
|
|
|
data[k.to_sym] = v
|
2019-05-31 09:54:11 +02:00
|
|
|
end
|
|
|
|
|
2019-06-03 04:49:54 +02:00
|
|
|
PluginStore.set("custom_wizard_api_#{api_name}", "endpoint_#{endpoint_id}", data)
|
2019-05-31 09:54:11 +02:00
|
|
|
|
2019-06-03 04:49:54 +02:00
|
|
|
self.get(api_name, endpoint_id)
|
2019-05-31 09:54:11 +02:00
|
|
|
end
|
|
|
|
|
2019-06-03 04:49:54 +02:00
|
|
|
def self.get(api_name, endpoint_id, opts={})
|
2019-05-31 09:54:11 +02:00
|
|
|
return nil if !endpoint_id
|
2019-06-03 04:49:54 +02:00
|
|
|
|
|
|
|
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
|
2019-06-03 04:49:54 +02:00
|
|
|
self.new(api_name, data)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2019-06-02 12:54:31 +02:00
|
|
|
end
|
|
|
|
|
2019-06-03 04:49:54 +02:00
|
|
|
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|
|
2019-06-03 04:49:54 +02:00
|
|
|
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
|
2019-06-03 04:49:54 +02:00
|
|
|
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(api_name, endpoint_id, body)
|
|
|
|
endpoint = self.get(api_name, endpoint_id)
|
|
|
|
auth = CustomWizard::Api::Authorization.get_header_authorization_string(api_name)
|
|
|
|
|
|
|
|
connection = Excon.new(
|
|
|
|
URI.parse(URI.encode(endpoint.url)).to_s,
|
|
|
|
:headers => {
|
|
|
|
"Authorization" => auth,
|
|
|
|
"Accept" => "application/json, */*",
|
|
|
|
"Content-Type" => "application/json"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
params = {
|
|
|
|
method: endpoint.method
|
|
|
|
}
|
|
|
|
|
|
|
|
if body
|
|
|
|
body = JSON.generate(body)
|
|
|
|
body.delete! '\\'
|
|
|
|
params[:body] = body
|
|
|
|
end
|
|
|
|
|
2019-06-05 23:23:15 +02:00
|
|
|
begin
|
|
|
|
response = connection.request(params)
|
|
|
|
return JSON.parse(response.body)
|
|
|
|
rescue
|
|
|
|
return JSON.parse "[{\"error\":\"API request failed\"}]"
|
|
|
|
end
|
2019-06-03 09:09:24 +02:00
|
|
|
end
|
2019-05-31 09:54:11 +02:00
|
|
|
end
|