From 04f29441d82f661571a7a49e01edd8f78f6170e8 Mon Sep 17 00:00:00 2001 From: Victor Perl Date: Fri, 15 Mar 2024 08:03:22 -0300 Subject: [PATCH] Update swagger, add csx (SignatureAPI) (#3298) --- .../SignatureAPI/apiDefinition.swagger.json | 42 ++++--- .../SignatureAPI/apiProperties.json | 4 +- certified-connectors/SignatureAPI/script.csx | 111 ++++++++++++++++++ 3 files changed, 136 insertions(+), 21 deletions(-) create mode 100644 certified-connectors/SignatureAPI/script.csx diff --git a/certified-connectors/SignatureAPI/apiDefinition.swagger.json b/certified-connectors/SignatureAPI/apiDefinition.swagger.json index 291d759f4..9b23de10b 100644 --- a/certified-connectors/SignatureAPI/apiDefinition.swagger.json +++ b/certified-connectors/SignatureAPI/apiDefinition.swagger.json @@ -85,24 +85,24 @@ "type": "string", "x-ms-summary": "Name", "description": "The name of the sender.", - "x-ms-visibility": "important" + "x-ms-visibility": "advanced" }, "email": { "type": "string", "x-ms-summary": "Email", "description": "The email address of the sender.", - "x-ms-visibility": "important" + "x-ms-visibility": "advanced" } - }, - "required": [ - "name", - "email" - ] + } }, - "test": { - "type": "boolean", - "x-ms-summary": "Test Envelope", - "description": "Set to true for test envelopes. Test envelopes are not legally binding.", + "mode": { + "type": "string", + "enum": [ + "live", + "test" + ], + "x-ms-summary": "Envelope Mode", + "description": "Test mode envelopes are non-binding and not billed.", "x-ms-visibility": "advanced" } }, @@ -431,10 +431,14 @@ "canceled" ] }, - "EnvelopeTest": { - "type": "boolean", - "x-ms-summary": "Test Envelope", - "description": "Indicates whether this is a test envelope. Test envelopes are not legally binding." + "EnvelopeMode": { + "type": "string", + "enum": [ + "live", + "test" + ], + "x-ms-summary": "Envelope Mode", + "description": "Test mode envelopes are non-binding and not billed." }, "EnvelopeDeliverable": { "type": "object", @@ -468,8 +472,8 @@ "sender": { "$ref": "#/definitions/EnvelopeSender" }, - "test": { - "$ref": "#/definitions/EnvelopeTest" + "mode": { + "$ref": "#/definitions/EnvelopeMode" } } }, @@ -508,8 +512,8 @@ "completed_at": { "$ref": "#/definitions/EnvelopeCompletedAt" }, - "test": { - "$ref": "#/definitions/EnvelopeTest" + "mode": { + "$ref": "#/definitions/EnvelopeMode" } } }, diff --git a/certified-connectors/SignatureAPI/apiProperties.json b/certified-connectors/SignatureAPI/apiProperties.json index 6d12162f9..f8fe72f35 100644 --- a/certified-connectors/SignatureAPI/apiProperties.json +++ b/certified-connectors/SignatureAPI/apiProperties.json @@ -14,7 +14,7 @@ }, "description": "The API Key for your Signature API account.", "displayName": "API Key", - "tooltip": "Generate both test and live API keys at https://dashboard.signatureapi.com." + "tooltip": "Get your API key in https://dashboard.signatureapi.com." } } }, @@ -22,6 +22,6 @@ "scriptOperations": [ "AddDocument", "GetDeliverable" - ] + ] } } \ No newline at end of file diff --git a/certified-connectors/SignatureAPI/script.csx b/certified-connectors/SignatureAPI/script.csx new file mode 100644 index 000000000..8238f713b --- /dev/null +++ b/certified-connectors/SignatureAPI/script.csx @@ -0,0 +1,111 @@ +using System.Net; +using System.Web; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +public class Script : ScriptBase +{ + public override async Task ExecuteAsync() + { + if ("AddDocument".Equals(this.Context.OperationId, StringComparison.OrdinalIgnoreCase)) + { + var contentAsString = await this.Context.Request.Content.ReadAsStringAsync().ConfigureAwait(false); + + var contentAsJson = JObject.Parse(contentAsString); + + var fileContentBase64 = (string)contentAsJson["file_content"]; + + var fileContent = Convert.FromBase64String(fileContentBase64); + + Uri baseUri = BaseUri(Context.Request.RequestUri); + string apiKey = Context.Request.Headers.GetValues("X-Api-Key").FirstOrDefault(); + + HttpRequestMessage createFileRequest = new HttpRequestMessage(); + + createFileRequest.Method = HttpMethod.Post; + createFileRequest.RequestUri = new Uri(baseUri, "/v1/files"); + createFileRequest.Headers.Add("X-Api-Key", apiKey); + + var createFileResponse = await Context.SendAsync(createFileRequest, this.CancellationToken); + + if(!createFileResponse.IsSuccessStatusCode) + { + throw new Exception($"Could not create temporary file. Status code: {createFileResponse.StatusCode}"); + } + + Uri locationUrl = createFileResponse.Headers.Location; + + string createFileResponseString = await createFileResponse.Content.ReadAsStringAsync(); + + JObject createFileResponseJson = JObject.Parse(createFileResponseString); + + string putUrl = (string)createFileResponseJson["put_url"]; + + HttpRequestMessage uploadFileRequest = new HttpRequestMessage(); + + uploadFileRequest.Method = HttpMethod.Put; + uploadFileRequest.RequestUri = new Uri(putUrl); + uploadFileRequest.Content = new ByteArrayContent(fileContent); + + var uploadFileResponse = await Context.SendAsync(uploadFileRequest, this.CancellationToken); + + if(!uploadFileResponse.IsSuccessStatusCode) + { + throw new Exception($"Could not upload temporary file. Status code: {createFileResponse.StatusCode}"); + } + + contentAsJson["file_content"] = null; + contentAsJson["url"] = locationUrl.ToString(); + string outboundContentAsString = contentAsJson.ToString(); + + Context.Request.Content = CreateJsonContent(outboundContentAsString); + + HttpResponseMessage response = await this.Context.SendAsync(this.Context.Request, this.CancellationToken).ConfigureAwait(continueOnCapturedContext: false); + + return response; + } + + if("GetDeliverable".Equals(this.Context.OperationId, StringComparison.OrdinalIgnoreCase)) + { + HttpResponseMessage response = await this.Context.SendAsync(this.Context.Request, this.CancellationToken).ConfigureAwait(continueOnCapturedContext: false); + + if (response.IsSuccessStatusCode) + { + var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(continueOnCapturedContext: false); + var resultJson = JObject.Parse(responseString); + var deliverableUrl = (string)resultJson["url"]; + + if(deliverableUrl != null) + { + HttpRequestMessage downloadRequest = new HttpRequestMessage(); + downloadRequest.Method = HttpMethod.Get; + downloadRequest.RequestUri = new Uri(deliverableUrl); + + var downloadResponse = await Context.SendAsync(downloadRequest, this.CancellationToken); + var downloadBytes = await downloadResponse.Content.ReadAsByteArrayAsync().ConfigureAwait(continueOnCapturedContext: false); + string downloadBase64 = Convert.ToBase64String(downloadBytes); + + resultJson["file_content"] = downloadBase64; + + response.Content = CreateJsonContent(resultJson.ToString()); + + return response; + } + } + + } + + + throw new NotImplementedException(); + } + + private Uri BaseUri(Uri uri) + { + string baseUri = $"{uri.Scheme}://{uri.Host}"; + if(!uri.IsDefaultPort) + { + baseUri += $":{uri.Port}"; + } + return new Uri(baseUri); + } +} \ No newline at end of file