mirror of
https://github.com/JKorf/CryptoExchange.Net.git
synced 2026-10-10 05:01:41 +00:00
3.0 wip
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using CryptoExchange.Net.Interfaces;
|
||||
|
||||
@@ -9,84 +11,61 @@ namespace CryptoExchange.Net.Requests
|
||||
/// <summary>
|
||||
/// Request object
|
||||
/// </summary>
|
||||
public class Request : IRequest
|
||||
internal class Request : IRequest
|
||||
{
|
||||
private readonly WebRequest request;
|
||||
private readonly HttpRequestMessage request;
|
||||
private readonly HttpClient httpClient;
|
||||
|
||||
/// <summary>
|
||||
/// Create request object for web request
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
public Request(WebRequest request)
|
||||
/// <param name="client"></param>
|
||||
public Request(HttpRequestMessage request, HttpClient client)
|
||||
{
|
||||
httpClient = client;
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public WebHeaderCollection Headers
|
||||
{
|
||||
get => request.Headers;
|
||||
set => request.Headers = value;
|
||||
}
|
||||
public HttpRequestHeaders Headers => request.Headers;
|
||||
|
||||
/// <inheritdoc />
|
||||
public string ContentType
|
||||
{
|
||||
get => request.ContentType;
|
||||
set => request.ContentType = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Content { get; set; }
|
||||
public string? Content { get; private set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Accept
|
||||
{
|
||||
get => ((HttpWebRequest)request).Accept;
|
||||
set => ((HttpWebRequest)request).Accept = value;
|
||||
set => request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(value));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public long ContentLength
|
||||
{
|
||||
get => ((HttpWebRequest)request).ContentLength;
|
||||
set => ((HttpWebRequest)request).ContentLength = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Method
|
||||
public HttpMethod Method
|
||||
{
|
||||
get => request.Method;
|
||||
set => request.Method = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public TimeSpan Timeout
|
||||
{
|
||||
get => TimeSpan.FromMilliseconds(request.Timeout);
|
||||
set => request.Timeout = (int)Math.Round(value.TotalMilliseconds);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Uri Uri => request.RequestUri;
|
||||
|
||||
/// <inheritdoc />
|
||||
public void SetProxy(string host, int port, string login, string password)
|
||||
public void SetContent(string data, string contentType)
|
||||
{
|
||||
request.Proxy = new WebProxy(host, port);
|
||||
if(!string.IsNullOrEmpty(login) && !string.IsNullOrEmpty(password)) request.Proxy.Credentials = new NetworkCredential(login, password);
|
||||
Content = data;
|
||||
request.Content = new StringContent(data, Encoding.UTF8, contentType);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<Stream> GetRequestStream()
|
||||
public void SetContent(byte[] data)
|
||||
{
|
||||
return await request.GetRequestStreamAsync().ConfigureAwait(false);
|
||||
request.Content = new ByteArrayContent(data);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IResponse> GetResponse()
|
||||
public async Task<IResponse> GetResponse(CancellationToken cancellationToken)
|
||||
{
|
||||
return new Response((HttpWebResponse)await request.GetResponseAsync().ConfigureAwait(false));
|
||||
return new Response(await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
using System.Net;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using CryptoExchange.Net.Interfaces;
|
||||
using CryptoExchange.Net.Objects;
|
||||
|
||||
namespace CryptoExchange.Net.Requests
|
||||
{
|
||||
@@ -8,10 +11,31 @@ namespace CryptoExchange.Net.Requests
|
||||
/// </summary>
|
||||
public class RequestFactory : IRequestFactory
|
||||
{
|
||||
private HttpClient? httpClient;
|
||||
|
||||
/// <inheritdoc />
|
||||
public IRequest Create(string uri)
|
||||
public void Configure(TimeSpan requestTimeout, ApiProxy? proxy)
|
||||
{
|
||||
return new Request(WebRequest.Create(uri));
|
||||
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);
|
||||
httpClient.Timeout = requestTimeout;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IRequest Create(HttpMethod method, string uri)
|
||||
{
|
||||
if (httpClient == null)
|
||||
throw new InvalidOperationException("Cant create request before configuring http client");
|
||||
|
||||
return new Request(new HttpRequestMessage(method, uri), httpClient);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using CryptoExchange.Net.Interfaces;
|
||||
|
||||
namespace CryptoExchange.Net.Requests
|
||||
@@ -9,38 +10,38 @@ namespace CryptoExchange.Net.Requests
|
||||
/// <summary>
|
||||
/// HttpWebResponse response object
|
||||
/// </summary>
|
||||
public class Response : IResponse
|
||||
internal class Response : IResponse
|
||||
{
|
||||
private readonly HttpWebResponse response;
|
||||
private readonly HttpResponseMessage response;
|
||||
|
||||
/// <inheritdoc />
|
||||
public HttpStatusCode StatusCode => response.StatusCode;
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsSuccessStatusCode => response.IsSuccessStatusCode;
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerable<KeyValuePair<string, IEnumerable<string>>> ResponseHeaders => response.Headers;
|
||||
|
||||
/// <summary>
|
||||
/// Create response for http web response
|
||||
/// Create response for a http response message
|
||||
/// </summary>
|
||||
/// <param name="response"></param>
|
||||
public Response(HttpWebResponse response)
|
||||
/// <param name="response">The actual response</param>
|
||||
public Response(HttpResponseMessage response)
|
||||
{
|
||||
this.response = response;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Stream GetResponseStream()
|
||||
public async Task<Stream> GetResponseStream()
|
||||
{
|
||||
return response.GetResponseStream();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerable<Tuple<string, string>> GetResponseHeaders()
|
||||
{
|
||||
return response.Headers.ToIEnumerable();
|
||||
return await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Close()
|
||||
{
|
||||
response.Close();
|
||||
response.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user