Skip to content

Commit

Permalink
Merge pull request #5 from candanedo/feature/proxy_request_to_integra…
Browse files Browse the repository at this point in the history
…tion_api

[FEATURE] Proxy request to integration API
  • Loading branch information
candanedo authored Feb 20, 2024
2 parents 1aeb46e + 01d0269 commit 3e4b789
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ Layout/LineLength:
Metrics/BlockLength:
Enabled: true
Exclude:
- 'spec/**/*_spec.rb' # Exclude block length check for spec files
- 'spec/**/*_spec.rb'
31 changes: 22 additions & 9 deletions lib/use_paragon/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@
module UseParagon
# https://docs.useparagon.com/workflows/triggers#request
class Workflow < Base
VALID_HTTP_METHODS = %w[post get put patch delete].freeze
# The Request trigger can be used to run workflows by sending it an HTTP request
def request(workflow_id, payload = {})
endpoint = path("sdk/triggers/#{workflow_id}")

connection.post(endpoint, payload)
end

# Call request to send an API request to a third-party integration on behalf of
# Call proxy_request to send an API request to a third-party integration on behalf of
# one of your users
# https://docs.useparagon.com/api/api-reference#request-integrationtype-string-path-string-requestoptions-requestinit-promise-less-than-unknown-gre
# ptions: Request options to include, such as:
# body: An object representing JSON contents of the request.
# method: An HTTP verb such as "GET" or "POST". Defaults to GET.
def proxy_request(integration_type, third_party_path, options)
endpoint = path("sdk/proxy/#{integration_type}/#{third_party_path}")

connection.post(endpoint, options)
# https://docs.useparagon.com/api/making-api-requests#server-side-usage
# This endpoint accepts any HTTP verb you want to use with the API:
# post, get, put, patch or delete.
# Body contents must be specified as application/json.
def proxy_request(request_method, integration_type, integration_path, payload = {})
formatted_method = request_method&.downcase

validate_proxy_http_method(formatted_method)

endpoint = path("sdk/proxy/#{integration_type}/#{integration_path}")

connection.send(formatted_method, endpoint, payload)
end

# App Events can be sent from your application using the Paragon REST API.
Expand All @@ -46,5 +51,13 @@ def event_payload(event_name, payload)
payload: payload
}
end

def validate_proxy_http_method(formatted_method)
return if VALID_HTTP_METHODS.include?(formatted_method)

raise ArgumentError,
"Invalid request method: #{formatted_method}. " \
"Allowed methods: #{VALID_HTTP_METHODS.join(", ")}"
end
end
end
33 changes: 28 additions & 5 deletions spec/use_paragon/workflow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,34 @@
end

describe "#proxy_request" do
it "calls the correct endpoint with post method" do
expect(workflow).to receive(:path).with("sdk/proxy/some_type/some_path")
.and_return("/projects/123/sdk/proxy/some_type/some_path")
expect(workflow).to receive(:connection).and_return(double("connection", post: true))
workflow.proxy_request("some_type", "some_path", {})
context "with valid HTTP method" do
it "calls the correct endpoint with post method" do
expect(workflow).to receive(:path).with("sdk/proxy/some_type/some_path")
.and_return("/projects/123/sdk/proxy/some_type/some_path")
expect(workflow).to receive(:connection).and_return(double("connection", post: true))
workflow.proxy_request("post", "some_type", "some_path", {})
end

it "calls the correct endpoint with delete method" do
expect(workflow).to receive(:path).with("sdk/proxy/some_type/some_path")
.and_return("/projects/123/sdk/proxy/some_type/some_path")
expect(workflow).to receive(:connection).and_return(double("connection", delete: true))
workflow.proxy_request("delete", "some_type", "some_path", {})
end
end

context "with invalid HTTP method" do
it "raises ArgumentError" do
expect { workflow.proxy_request("invalid_method", "some_type", "some_path", {}) }
.to raise_error(ArgumentError, /Invalid request method/)
end
end

context "with missing HTTP method" do
it "raises ArgumentError" do
expect { workflow.proxy_request(nil, "some_type", "some_path", {}) }
.to raise_error(ArgumentError, /Invalid request method/)
end
end
end

Expand Down

0 comments on commit 3e4b789

Please sign in to comment.