-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addresses: #682 Api Keys: https://user-images.githubusercontent.com/50227291/196992417-ab91ed2e-1514-4abf-9954-d44123542583.mov Webhook: https://user-images.githubusercontent.com/50227291/196992487-4c3c9593-4cb6-4b8c-8f7b-c6e22d9385a2.mov **The webhook's payload can be accessed as `parameters[:request]['body']` in external api client.** Eg: https://github.com/restarone/violet_rails/pull/1167/files#diff-1aa9cadc4b4c76bf7ec3d7c0cf8d0da66be3ba87336bad4d32241c99d53aa6beR603-R623 ``` class ExternalApiModelExample def initialize(parameters) @external_api_client = parameters[:external_api_client] @request = parameters[:request] end def start if @request['body']['type'] == 'customer.created' @external_api_client.api_namespace.api_resources.create( properties: { request_body: @request["body"] } ) end end ``` Custom webhook verification method: https://user-images.githubusercontent.com/50227291/197921958-a06b85a8-7c7a-4e3b-afb9-889fe5c6e110.mov Co-authored-by: Pralish Kayastha <[email protected]> Co-authored-by: Pralish Kayastha <[email protected]> Co-authored-by: Prashant Khadka <[email protected]>
- Loading branch information
1 parent
f661907
commit b036672
Showing
64 changed files
with
2,329 additions
and
857 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
class Api::ExternalApiClientsController < Api::BaseController | ||
before_action :find_external_api_client | ||
after_action :unload_api_connection_class, only: :webhook | ||
skip_before_action :authenticate_request | ||
|
||
def webhook | ||
# should only exist if drive strategy is webhook | ||
raise ActionController::RoutingError.new('Not Found') unless @external_api_client.drive_strategy == ExternalApiClient::DRIVE_STRATEGIES[:webhook] | ||
|
||
return render json: { message: 'Webhook not enabled', success: false }, status: 400 unless @external_api_client.enabled | ||
|
||
if @external_api_client.webhook_verification_method.present? | ||
verified, message = Webhook::Verification.new(request, @external_api_client.webhook_verification_method).call | ||
|
||
return render json: { message: message, success: false }, status: 401 unless verified | ||
end | ||
|
||
@model_definition = @external_api_client.evaluated_model_definition | ||
|
||
# add render method on model_definition | ||
# render should only be available on controller context | ||
@model_definition.define_method(:render) do |args| | ||
return { render: true, args: args } | ||
end | ||
|
||
response = @model_definition.new(external_api_client: @external_api_client, request: request).start | ||
|
||
render response[:args] if response.is_a?(Hash) && response[:render] | ||
end | ||
|
||
private | ||
|
||
def unload_api_connection_class | ||
ExternalApiClient.send(:remove_const, @model_definition.name.split('::').last.to_sym) if @model_definition | ||
end | ||
|
||
def find_external_api_client | ||
@external_api_client = ExternalApiClient.find_by(slug: params[:external_api_client]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
class Comfy::Admin::ApiKeysController < Comfy::Admin::Cms::BaseController | ||
before_action :ensure_authority_for_read_api_keys_only_in_api, only: %i[ index ] | ||
before_action :ensure_authority_for_view_api_keys_details_only_in_api, only: %i[ show ] | ||
before_action :ensure_authority_for_delete_access_for_api_keys_only_in_api, only: %i[ destroy ] | ||
before_action :ensure_authority_for_full_access_for_api_keys_only_in_api, only: %i[ new edit create update ] | ||
|
||
before_action :set_api_key, only: [:update, :edit, :destroy, :show] | ||
|
||
def index | ||
@api_keys = ApiKey.all | ||
end | ||
|
||
def new | ||
@api_key = ApiKey.new | ||
@api_key.api_namespace_keys.build | ||
end | ||
|
||
def edit | ||
end | ||
|
||
def create | ||
@api_key = ApiKey.new(api_key_params) | ||
|
||
if @api_key.save | ||
redirect_to api_key_path(id: @api_key.id), notice: "Api key was successfully created." | ||
else | ||
render :new, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def update | ||
if @api_key.update(api_key_params) | ||
redirect_to api_key_path(id: @api_key.id), notice: "Api key was successfully updated." | ||
else | ||
render :edit, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def destroy | ||
@api_key.destroy | ||
redirect_to api_keys_path, notice: "Api key was successfully destroyed." | ||
end | ||
|
||
private | ||
|
||
def set_api_key | ||
@api_key = ApiKey.find_by(id: params[:id]) | ||
end | ||
|
||
def api_key_params | ||
params.require(:api_key).permit(:label, :authentication_strategy, api_namespace_ids: []) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.