private async Task UpdateResponse()

in certified-connectors/DocuSign/script.csx [435:528]


    private async Task UpdateResponse(HttpResponseMessage response)
    {
        if ("CreateHookEnvelope".Equals(this.Context.OperationId, StringComparison.OrdinalIgnoreCase)
            && response.Headers?.Location != null)
        {
            var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
            var body = ParseContentAsJObject(content, false);
            response.Headers.Location = new Uri(string.Format(
                "{0}/{1}",
                this.Context.OriginalRequestUri.ToString(),
                body.GetValue("connectId").ToString()));
        }

        if ("GetDynamicSigners".Equals(this.Context.OperationId, StringComparison.OrdinalIgnoreCase))
        {
            var body = ParseContentAsJObject(await response.Content.ReadAsStringAsync().ConfigureAwait(false), false);
            var itemProperties = new JObject();
            var basePropertyDefinition = new JObject
            {
                ["type"] = "string",
                ["x-ms-keyOrder"] = 0,
                ["x-ms-keyType"] = "none",
                ["x-ms-sort"] = "none",
            };

            foreach (var signer in (body["signers"] as JArray) ?? new JArray())
            {
                var roleName = signer["roleName"];
                itemProperties[roleName + " Name"] = basePropertyDefinition.DeepClone();
                itemProperties[roleName + " Email"] = basePropertyDefinition.DeepClone();
            }

            var newBody = new JObject
            {
                ["name"] = "dynamicSchema",
                ["title"] = "dynamicSchema",
                ["x-ms-permission"] = "read-write",
                ["schema"] = new JObject
                {
                    ["type"] = "array",
                    ["items"] = new JObject
                    {
                        ["type"] = "object",
                        ["properties"] = itemProperties,
                    },
                },
            };

            response.Content = new StringContent(newBody.ToString(), Encoding.UTF8, "application/json");
        }

        if ("OnEnvelopeStatusChanges".Equals(this.Context.OperationId, StringComparison.OrdinalIgnoreCase))
        {
            var originalQuery = HttpUtility.ParseQueryString(this.Context.Request.RequestUri.Query);
            var triggerState = originalQuery.Get("triggerState");
            var body = ParseContentAsJObject(await response.Content.ReadAsStringAsync().ConfigureAwait(false), false);
            var items = body.SelectToken("envelopes") as JArray;

            if (string.IsNullOrEmpty(triggerState) || items == null || items.Count == 0)
            {
                response.Content = null;
                response.StatusCode = HttpStatusCode.Accepted;
            }

            if (string.IsNullOrEmpty(triggerState))
            {
                // initial trigger call
                triggerState = DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
            }

            if (items?.Count > 0)
            {
                triggerState = items.Max(x => DateTimeOffset.Parse(x["statusChangedDateTime"].ToString())).AddMilliseconds(10).ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
            }

            var locationUriBuilder = new UriBuilder(this.Context.OriginalRequestUri);
            originalQuery["triggerState"] = triggerState;
            locationUriBuilder.Query = originalQuery.ToString();
            response.Headers.Location = locationUriBuilder.Uri;
            response.Headers.RetryAfter = new RetryConditionHeaderValue(TimeSpan.FromSeconds(120));
        }

        if (response.Content?.Headers?.ContentType != null)
        {
            if ("GetDocuments".Equals(this.Context.OperationId, StringComparison.OrdinalIgnoreCase))
            {
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
            }
            else
            {
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            }
        }
    }