//------------------------------------------------------------------------------
//
// This code was generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
//
// Changes to this file may cause incorrect behavior and will be lost when
// the code is regenerated.
//
//------------------------------------------------------------------------------
#nullable enable
namespace LukeHagar.PlexAPI.SDK.Utils
{
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
public interface ISpeakeasyHttpClient
{
///
/// Sends an HTTP request asynchronously.
///
/// The HTTP request message to send.
/// The value of the TResult parameter contains the HTTP response message.
Task SendAsync(HttpRequestMessage request);
///
/// Clones an HTTP request asynchronously.
///
///
/// This method is used in the context of Retries. It creates a new HttpRequestMessage instance
/// as a deep copy of the original request, including its method, URI, content, headers and options.
///
/// The HTTP request message to clone.
/// The value of the TResult parameter contains the cloned HTTP request message.
Task CloneAsync(HttpRequestMessage request);
}
public class SpeakeasyHttpClient : ISpeakeasyHttpClient
{
protected readonly HttpClient httpClient;
public SpeakeasyHttpClient()
{
httpClient = new System.Net.Http.HttpClient();
}
public virtual async Task SendAsync(HttpRequestMessage request)
{
return await httpClient.SendAsync(request);
}
public virtual async Task CloneAsync(HttpRequestMessage request)
{
HttpRequestMessage clone = new HttpRequestMessage(request.Method, request.RequestUri);
if (request.Content != null)
{
clone.Content = new ByteArrayContent(await request.Content.ReadAsByteArrayAsync());
if (request.Content.Headers != null)
{
foreach (var h in request.Content.Headers)
{
clone.Content.Headers.Add(h.Key, h.Value);
}
}
}
foreach (KeyValuePair> header in request.Headers)
{
clone.Headers.TryAddWithoutValidation(header.Key, header.Value);
}
foreach (KeyValuePair prop in request.Options)
{
clone.Options.TryAdd(prop.Key, prop.Value);
}
return clone;
}
}
}