mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-06-11 18:06:27 +00:00
72 lines
2.0 KiB
C#
72 lines
2.0 KiB
C#
using System;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using CryptoExchange.Net.Interfaces;
|
|
|
|
namespace CryptoExchange.Net.Requests
|
|
{
|
|
/// <summary>
|
|
/// Request object
|
|
/// </summary>
|
|
internal class Request : IRequest
|
|
{
|
|
private readonly HttpRequestMessage request;
|
|
private readonly HttpClient httpClient;
|
|
|
|
/// <summary>
|
|
/// Create request object for web request
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="client"></param>
|
|
public Request(HttpRequestMessage request, HttpClient client)
|
|
{
|
|
httpClient = client;
|
|
this.request = request;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public HttpRequestHeaders Headers => request.Headers;
|
|
|
|
/// <inheritdoc />
|
|
public string? Content { get; private set; }
|
|
|
|
/// <inheritdoc />
|
|
public string Accept
|
|
{
|
|
set => request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(value));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public HttpMethod Method
|
|
{
|
|
get => request.Method;
|
|
set => request.Method = value;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Uri Uri => request.RequestUri;
|
|
|
|
/// <inheritdoc />
|
|
public void SetContent(string data, string contentType)
|
|
{
|
|
Content = data;
|
|
request.Content = new StringContent(data, Encoding.UTF8, contentType);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void SetContent(byte[] data)
|
|
{
|
|
request.Content = new ByteArrayContent(data);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IResponse> GetResponse(CancellationToken cancellationToken)
|
|
{
|
|
return new Response(await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false));
|
|
}
|
|
}
|
|
}
|