Relativity.API.Extensions

The Relativity.API.Extensions library provides additional helper functions that will be useful when interacting with RelativityOne environments in the cloud-based infrastructure. You can use the helper functions in this library to set up calls to REST services in a specific environment.

  • GetInstanceBaseURL - Allows you to build URLs to any API without assuming the host name. Use this method to safeguard your application against backend URL changes in the RelativityOne infrastructure.
  • BuildHTTPAuthorizationHeader - Allows populating the Authentication header on HTTP requests. Use this method to safeguard your application against backend authentication/authorization changes in RelativityOne.

While this is a new API focused more on interacting with RelativityOne through REST services, you can also continue to use the ServiceFactory, which provides the same benefits for consumers of RelativityOne .NET API client libraries.

Relativity API Extensions .NET API Reference

Usage Examples

The code sample below illustrates how to use the GetInstanceBaseURL and BuildHTTPAuthorizationHeader methods to construct a HTTP service call (you will need to reference Relativity.API.Extensions.dll in your project).

Copy
using Relativity.API.Extensions;
public class AgentBaseTest : AgentBase
{
    private IServicesMgr _servicesMgr;

    public AgentBaseTest()
    {
    }

    public override string Name => "AgentBaseTest";

    public override void Execute()
    {
        _servicesMgr = Helper.GetServicesManager();
        var client = HttpClientFactory.CreateClient(_servicesMgr, ExecutionIdentity.CurrentUser);
        var requestUrl = GetRequestUrl("/api/test-endpoint");
        var request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
        var response = client.SendAsync(request).Result;
    }

    private Uri GetRequestUrl(string requestEndpoint)
    {
        var baseUrl = _servicesMgr.GetInstanceBaseURL();
        if (string.IsNullOrEmpty(baseUrl))
        {
            throw new InvalidOperationException("Base URL is not set.");
        }
        var fullUri = new Uri(new Uri(baseUrl), requestEndpoint);
        return fullUri;
    }
}

public class HttpClientFactory
{
    private static Lazy<HttpClientHandler> _sharedHandler  => new Lazy<HttpClientHandler>();
    
    public static HttpClient CreateClient(IServicesMgr servicesMgr, ExecutionIdentity identity)
    {
        if (servicesMgr == null)
            throw new ArgumentNullException(nameof(servicesMgr));
        var handler = new AuthDelegatingHandler(servicesMgr, identity)
        {
            InnerHandler = _sharedHandler.Value
        };
        return new HttpClient(handler, disposeHandler: false);
    }
}

public class AuthDelegatingHandler : DelegatingHandler
{
    private IServicesMgr _servicesMgr;
    private ExecutionIdentity _identity;

    public AuthDelegatingHandler(IServicesMgr servicesMgr, ExecutionIdentity identity)
    {
        _servicesMgr = servicesMgr;
        _identity = identity;
    }
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        if (request.Headers.Contains("Authorization")) return base.SendAsync(request, cancellationToken);
        // Set Authorization header
        var authorizationHeader = _servicesMgr.BuildHTTPAuthorizationHeader(_identity);
        if (!string.IsNullOrWhiteSpace(authorizationHeader))
        {
            request.Headers.Add("Authorization", authorizationHeader);
        }
        return base.SendAsync(request, cancellationToken);
    }
}

FAQs

Version History