0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-26 02:50:28 +01:00

added logging to token refresh, added user column to log and log display

Dieser Commit ist enthalten in:
Robert Barrow 2019-06-16 12:49:51 +01:00
Ursprung c6fed81d28
Commit 7efaf25572
6 geänderte Dateien mit 41 neuen und 12 gelöschten Zeilen

Datei anzeigen

@ -260,14 +260,20 @@
<div class="log-list"> <div class="log-list">
<table class="wizard-api-log-table"> <table class="wizard-api-log-table">
<th>Datetime</th> <th>Datetime</th>
<th>User</th>
<th>Status</th> <th>Status</th>
<th>Endpoint</th> <th>URL</th>
<th>Error</th> <th>Error</th>
{{#each api.log as |logentry|}} {{#each api.log as |logentry|}}
<tr> <tr>
<td>{{logentry.time}}</td> <td>{{logentry.time}}</td>
<td class="user-image">
<div class="user-image-inner">
<a href="{{unbound logentry.userpath}}" data-user-card="{{unbound logentry.username}}">{{avatar logentry imageSize="large"}}</a>
</div>
</td>
<td>{{logentry.status}}</td> <td>{{logentry.status}}</td>
<td>{{logentry.endpoint_url}}</td> <td>{{logentry.url}}</td>
<td>{{logentry.error}}</td> <td>{{logentry.error}}</td>
</tr> </tr>
{{/each}} {{/each}}

Datei anzeigen

@ -116,8 +116,14 @@ class CustomWizard::Api::Authorization
:method => 'GET', :method => 'GET',
:query => URI.encode_www_form(body) :query => URI.encode_www_form(body)
) )
begin
result = connection.request() result = connection.request()
log_params = {time: Time.now, user_id: 0, status: 'SUCCESS', url: token_url, error: ""}
CustomWizard::Api::LogEntry.set(name, log_params)
rescue
log_params = {time: Time.now, user_id: 0, status: 'FAILURE', url: token_url, error: "Token refresh request failed"}
CustomWizard::Api::LogEntry.set(name, log_params)
end
self.handle_token_result(name, result) self.handle_token_result(name, result)
end end

Datei anzeigen

@ -62,7 +62,7 @@ class CustomWizard::Api::Endpoint
end end
end end
def self.request(api_name, endpoint_id, body) def self.request(user, api_name, endpoint_id, body)
endpoint = self.get(api_name, endpoint_id) endpoint = self.get(api_name, endpoint_id)
auth = CustomWizard::Api::Authorization.get_header_authorization_string(api_name) auth = CustomWizard::Api::Authorization.get_header_authorization_string(api_name)
@ -87,12 +87,13 @@ class CustomWizard::Api::Endpoint
begin begin
response = connection.request(params) response = connection.request(params)
log_params = {time: Time.now, status: 'SUCCESS', endpoint_url: endpoint.url, error: ""} log_params = {time: Time.now, user_id: user.id, status: 'SUCCESS', url: endpoint.url, error: ""}
CustomWizard::Api::LogEntry.set(api_name, log_params) CustomWizard::Api::LogEntry.set(api_name, log_params)
return JSON.parse(response.body) return JSON.parse(response.body)
rescue rescue
# TODO: improve error detail # TODO: improve error detail
log_params = {time: Time.now, status: 'FAILURE', endpoint_url: endpoint.url, error: "API request failed"} log_params = {time: Time.now, user_id: user.id, status: 'FAILURE', url: endpoint.url, error: "API request failed"}
CustomWizard::Api::LogEntry.set(api_name, log_params) CustomWizard::Api::LogEntry.set(api_name, log_params)
return JSON.parse "[{\"error\":\"API request failed\"}]" return JSON.parse "[{\"error\":\"API request failed\"}]"
end end

Datei anzeigen

@ -3,9 +3,14 @@ class CustomWizard::Api::LogEntry
attr_accessor :log_id, attr_accessor :log_id,
:time, :time,
:user_id,
:status, :status,
:endpoint_url, :url,
:error :error,
:username,
:userpath,
:name,
:avatar_template
def initialize(api_name, data={}) def initialize(api_name, data={})
@api_name = api_name @api_name = api_name
@ -58,6 +63,12 @@ class CustomWizard::Api::LogEntry
api_name = record['plugin_name'].sub("custom_wizard_api_", "") api_name = record['plugin_name'].sub("custom_wizard_api_", "")
data = ::JSON.parse(record['value']) data = ::JSON.parse(record['value'])
data[:log_id] = record['key'].split('_').last data[:log_id] = record['key'].split('_').last
this_user = User.find_by(id: data['user_id'])
data[:user_id] = this_user.id || nil
data[:username] = this_user.username || ""
data[:userpath] = "/u/#{this_user.username_lower}/activity"
data[:name] = this_user.name || ""
data[:avatar_template] = "/user_avatar/default/#{this_user.username_lower}/97/#{this_user.uploaded_avatar_id}.png"
self.new(api_name, data) self.new(api_name, data)
end end
end end

Datei anzeigen

@ -406,7 +406,7 @@ class CustomWizard::Builder
api_body = CustomWizard::Builder.fill_placeholders(JSON.generate(api_body_parsed), user, data) api_body = CustomWizard::Builder.fill_placeholders(JSON.generate(api_body_parsed), user, data)
end end
result = CustomWizard::Api::Endpoint.request(action['api'], action['api_endpoint'], api_body) result = CustomWizard::Api::Endpoint.request(user, action['api'], action['api_endpoint'], api_body)
if error = result['error'] || (result[0] && result[0]['error']) if error = result['error'] || (result[0] && result[0]['error'])
error = error['message'] || error error = error['message'] || error

Datei anzeigen

@ -2,6 +2,11 @@ class CustomWizard::Api::LogSerializer < ApplicationSerializer
attributes :log_id, attributes :log_id,
:time, :time,
:status, :status,
:endpoint_url, :url,
:error :error,
:user_id,
:username,
:userpath,
:name,
:avatar_template
end end