in certified-connectors/DocuSign/script.csx [280:323]
private async Task UpdateApiEndpoint()
{
string content = string.Empty;
using var userInfoRequest = new HttpRequestMessage(HttpMethod.Get, "https://account-d.docusign.com/oauth/userinfo");
// Access token is in the authorization header already
userInfoRequest.Headers.Authorization = this.Context.Request.Headers.Authorization;
try
{
using var userInfoResponse = await this.Context.SendAsync(userInfoRequest, this.CancellationToken).ConfigureAwait(false);
content = await userInfoResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
if (userInfoResponse.IsSuccessStatusCode)
{
var jsonContent = JObject.Parse(content);
var baseUri = jsonContent["accounts"]?[0]?["base_uri"]?.ToString();
if (!string.IsNullOrEmpty(baseUri))
{
this.Context.Request.RequestUri = new Uri(new Uri(baseUri), this.Context.Request.RequestUri.PathAndQuery);
}
else
{
throw new ConnectorException(HttpStatusCode.BadGateway, "Unable to get User's API endpoint from the response: " + content);
}
}
else
{
throw new ConnectorException(userInfoResponse.StatusCode, content);
}
}
catch (HttpRequestException ex)
{
throw new ConnectorException(HttpStatusCode.BadGateway, "Unable to get User Info: " + ex.Message, ex);
}
catch (JsonReaderException ex)
{
throw new ConnectorException(HttpStatusCode.BadGateway, "Unable to parse User Info response: " + content, ex);
}
catch (UriFormatException ex)
{
throw new ConnectorException(HttpStatusCode.BadGateway, "Unable to construct User's API endpoint from the response: " + content, ex);
}
}