This repository was archived by the owner on Feb 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathHttpProtocolClientBuilder.cs
More file actions
97 lines (85 loc) · 4.18 KB
/
HttpProtocolClientBuilder.cs
File metadata and controls
97 lines (85 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* ******************************************************************************
* Copyright 2022 Ellucian Company L.P. and its affiliates.
* ******************************************************************************
*/
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Security.Authentication;
namespace Ellucian.Ethos.Integration.Client
{
/// <summary>
/// Builds an <see cref="System.Net.Http.HttpClient"/> used for making secure API calls over HTTP.
/// Uses Tls13, Tls12, Tls11 protocol.
/// </summary>
public class HttpProtocolClientBuilder : IHttpProtocolClientBuilder
{
/// <summary>
/// Time in seconds to allow an http connection to time out. Default is
/// 300 seconds (5 minutes).
/// </summary>
private static readonly int CONNECTION_TIMEOUT = 300;
private const SslProtocols PROTOCOL = SslProtocols.Tls13 | SslProtocols.Tls12;
private const string CLIENT_NAME = "EllucianEthosIntegrationSdk-dotnet";
/// <summary>
/// <see cref="HttpClient"/>.
/// </summary>
public HttpClient Client { get; private set; }
/// <summary>
/// Instantiates a new Http client builder service and initializes it with default configurations for the
/// SslProtocols, ClientCertificateOptions and DefaultRequestHeaders.
/// </summary>
/// <param name="client">Instance of <see cref="System.Net.Http.HttpClient"/> passed from <see cref="EthosClient"/>.</param>
/// <param name="connectionTimeOut">Time in seconds to allow an http connection to time out. Default is
/// 300 seconds (5 minutes).</param>
public HttpProtocolClientBuilder( HttpClient client, int? connectionTimeOut = null )
{
client ??= BuildHttpClient( connectionTimeOut.HasValue ? connectionTimeOut : CONNECTION_TIMEOUT );
Client = client;
}
/// <summary>
/// Creates instance of <see cref="System.Net.Http.HttpClient"/>. Called internally from EthosClientFactory./>
/// </summary>
/// <param name="connectionTimeout">An optional parameter indicating the amount of seconds before the client request should time out.</param>
/// <returns>Returns instance of HttpClient.</returns>
public HttpClient BuildHttpClient( int? connectionTimeout )
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddHttpClient( CLIENT_NAME, client =>
{
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Add( "pragma", "no-cache" );
client.DefaultRequestHeaders.Add( "cache-control", "no-cache" );
ProductInfoHeaderValue prodHeaderVal = new( CLIENT_NAME, Assembly.GetExecutingAssembly().GetName()?.Version?.ToString() );
client.DefaultRequestHeaders.UserAgent.Add( prodHeaderVal );
client.Timeout = TimeSpan.FromMinutes( ( double ) connectionTimeout );
} )
.SetHandlerLifetime( TimeSpan.FromSeconds( CONNECTION_TIMEOUT ) )
.ConfigurePrimaryHttpMessageHandler( () =>
{
return new HttpClientHandler()
{
ClientCertificateOptions = ClientCertificateOption.Automatic,
SslProtocols = PROTOCOL
};
} );
var services = serviceCollection.BuildServiceProvider();
var httpClientFactory = services.GetRequiredService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient( CLIENT_NAME );
Client = httpClient;
return Client;
}
/// <summary>
/// Creates instance of <see cref="System.Net.Http.HttpClient"/>. Called internally from EthosClientFactory./>
/// </summary>
/// <returns>Returns instance of HttpClient with a default timeout.</returns>
public HttpClient BuildHttpClient()
{
this.Client = BuildHttpClient( CONNECTION_TIMEOUT );
return this.Client;
}
}
}