1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-09 17:06:19 +00:00
2021-11-12 09:40:42 +01:00

48 lines
1.5 KiB
C#

using System;
using System.Net;
using System.Net.Http;
using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.Objects;
namespace CryptoExchange.Net.Requests
{
/// <summary>
/// Request factory
/// </summary>
public class RequestFactory : IRequestFactory
{
private HttpClient? httpClient;
/// <inheritdoc />
public void Configure(TimeSpan requestTimeout, ApiProxy? proxy, HttpClient? client = null)
{
if (client == null)
{
HttpMessageHandler handler = new HttpClientHandler()
{
Proxy = proxy == null ? null : new WebProxy
{
Address = new Uri($"{proxy.Host}:{proxy.Port}"),
Credentials = proxy.Password == null ? null : new NetworkCredential(proxy.Login, proxy.Password)
}
};
httpClient = new HttpClient(handler) { Timeout = requestTimeout };
}
else
{
httpClient = client;
}
}
/// <inheritdoc />
public IRequest Create(HttpMethod method, string uri, int requestId)
{
if (httpClient == null)
throw new InvalidOperationException("Cant create request before configuring http client");
return new Request(new HttpRequestMessage(method, uri), httpClient, requestId);
}
}
}