33namespace LessAnnoyingHttp ;
44
55public static class Http {
6+ private static readonly HttpClient Client = new ( new SocketsHttpHandler {
7+ PooledConnectionLifetime = TimeSpan . FromMinutes ( 2 )
8+ } ) ;
9+
610 /// <summary>
711 /// The timeout in seconds which requests should wait for before failing (default: 10)<br/>
812 /// Changing this value changes the timeouts globally
913 /// </summary>
10- public static int Timeout { get ; set ; } = 10 ;
14+ public static int Timeout {
15+ get => ( int ) Client . Timeout . TotalSeconds ;
16+ set => Client . Timeout = TimeSpan . FromSeconds ( value ) ;
17+ }
18+
19+ static Http ( ) {
20+ Timeout = 10 ;
21+ AppDomain . CurrentDomain . ProcessExit += ( _ , _ ) => Client . Dispose ( ) ;
22+ }
1123
1224 /// <summary>
1325 /// Sends a GET request
@@ -16,8 +28,6 @@ public static class Http {
1628 /// <param name="headers">Optional headers</param>
1729 /// <returns><see cref="Response"/></returns>
1830 public static async Task < Response > GetAsync ( string endpoint , Header [ ] ? headers = null ) {
19- using HttpClient client = new ( ) ;
20- client . Timeout = TimeSpan . FromSeconds ( Timeout ) ;
2131 HttpRequestMessage request = new ( ) {
2232 RequestUri = new Uri ( endpoint ) ,
2333 Method = HttpMethod . Get
@@ -29,7 +39,7 @@ public static async Task<Response> GetAsync(string endpoint, Header[]? headers =
2939
3040 HttpResponseMessage response ;
3141 try {
32- response = await client . SendAsync ( request ) ;
42+ response = await Client . SendAsync ( request ) ;
3343 } catch ( TaskCanceledException ) {
3444 throw new TimeoutException ( $ "Timeout waiting for response for request to { endpoint } ") ;
3545 } catch ( Exception e ) {
@@ -45,8 +55,6 @@ public static async Task<Response> GetAsync(string endpoint, Header[]? headers =
4555 public static Response Get ( string endpoint , Header [ ] ? headers = null ) => GetAsync ( endpoint , headers ) . GetAwaiter ( ) . GetResult ( ) ;
4656
4757 private static async Task < Response > BodyRequest ( string endpoint , HttpMethod method , string body , Header [ ] ? headers , string contentType ) {
48- using HttpClient client = new ( ) ;
49- client . Timeout = TimeSpan . FromSeconds ( Timeout ) ;
5058 HttpRequestMessage request = new ( ) {
5159 RequestUri = new Uri ( endpoint ) ,
5260 Method = method ,
@@ -59,7 +67,7 @@ private static async Task<Response> BodyRequest(string endpoint, HttpMethod meth
5967
6068 HttpResponseMessage response ;
6169 try {
62- response = await client . SendAsync ( request ) ;
70+ response = await Client . SendAsync ( request ) ;
6371 } catch ( TaskCanceledException ) {
6472 throw new TimeoutException ( $ "Timeout waiting for response for request to { endpoint } ") ;
6573 } catch ( Exception e ) {
0 commit comments