Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 37 additions & 63 deletions src/HttpClientServiceHelper/Delete.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using HttpClientServiceHelper.Models;
using HttpClientServiceHelper.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
Expand All @@ -17,11 +17,7 @@ public partial class HttpClientHelper
/// <returns>an HTTP Response Message</returns>
public static async Task<HttpResponseMessage> DeleteAsync(string Route)
{
using (HttpClient httpClient = new HttpClient())
{
var httpResponse = await httpClient.DeleteAsync(new Uri(Route));
return httpResponse;
}
return await _httpClient.DeleteAsync(new Uri(Route));
}
/// <summary>
/// Triggers a DELETE request to the specified route with a Bearer token and retrieves the result as a string which you can serialize.
Expand All @@ -31,12 +27,10 @@ public static async Task<HttpResponseMessage> DeleteAsync(string Route)
/// <returns>an HTTP Response Message</returns>
public static async Task<HttpResponseMessage> DeleteAsync(string Route, string Token = null)
{
using (HttpClient httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Authorization = !string.IsNullOrEmpty(Token) ? new AuthenticationHeaderValue("Bearer", Token) : null;
var httpResponse = await httpClient.DeleteAsync(new Uri(Route));
return httpResponse;
}
using var request = new HttpRequestMessage(HttpMethod.Delete, new Uri(Route));
if (!string.IsNullOrEmpty(Token))
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
return await _httpClient.SendAsync(request);
}
/// <summary>
/// Triggers a DELETE request with headers to the specified route and retrieves the result as a string which you can serialize.
Expand All @@ -46,15 +40,10 @@ public static async Task<HttpResponseMessage> DeleteAsync(string Route, string T
/// <returns>an HTTP Response Message</returns>
public static async Task<HttpResponseMessage> DeleteAsync(string Route, List<Header> Headers)
{
using (HttpClient httpClient = new HttpClient())
{
foreach (var Header in Headers)
{
httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
}
var httpResponse = await httpClient.DeleteAsync(new Uri(Route));
return httpResponse;
}
using var request = new HttpRequestMessage(HttpMethod.Delete, new Uri(Route));
foreach (var Header in Headers)
request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
return await _httpClient.SendAsync(request);
}
/// <summary>
/// Triggers a DELETE request with headers and authorization to the specified route and retrieves the result as a string which you can serialize.
Expand All @@ -65,17 +54,13 @@ public static async Task<HttpResponseMessage> DeleteAsync(string Route, List<Hea
/// <returns>an HTTP Response Message</returns>
public static async Task<HttpResponseMessage> DeleteAsync(string Route, List<Header> Headers, Authorization Authorization)
{
using (HttpClient httpClient = new HttpClient())
{
foreach (var Header in Headers)
{
httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
}
httpClient.DefaultRequestHeaders.Authorization = string.IsNullOrEmpty(Authorization.Parameter) ?
new AuthenticationHeaderValue(Authorization.Scheme) : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
var httpResponse = await httpClient.DeleteAsync(new Uri(Route));
return httpResponse;
}
using var request = new HttpRequestMessage(HttpMethod.Delete, new Uri(Route));
foreach (var Header in Headers)
request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
request.Headers.Authorization = string.IsNullOrEmpty(Authorization.Parameter)
? new AuthenticationHeaderValue(Authorization.Scheme)
: new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
return await _httpClient.SendAsync(request);
}

/// <summary>
Expand All @@ -85,11 +70,8 @@ public static async Task<HttpResponseMessage> DeleteAsync(string Route, List<Hea
/// <returns>a string format of the HTTP Response message</returns>
public static async Task<string> DeleteAndGetResponseAsStringAsync(string Route)
{
using (HttpClient httpClient = new HttpClient())
{
var httpResponse = await httpClient.DeleteAsync(new Uri(Route));
return await httpResponse.Content.ReadAsStringAsync();
}
var httpResponse = await _httpClient.DeleteAsync(new Uri(Route));
return await httpResponse.Content.ReadAsStringAsync();
}
/// <summary>
/// Triggers a DELETE request to the specified route with a Bearer token and retrieves the result as a string which you can serialize.
Expand All @@ -99,12 +81,11 @@ public static async Task<string> DeleteAndGetResponseAsStringAsync(string Route)
/// <returns>a string format of the HTTP Response message</returns>
public static async Task<string> DeleteAndGetResponseAsStringAsync(string Route, string Token = null)
{
using (HttpClient httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Authorization = !string.IsNullOrEmpty(Token) ? new AuthenticationHeaderValue("Bearer", Token) : null;
var httpResponse = await httpClient.DeleteAsync(new Uri(Route));
return await httpResponse.Content.ReadAsStringAsync();
}
using var request = new HttpRequestMessage(HttpMethod.Delete, new Uri(Route));
if (!string.IsNullOrEmpty(Token))
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
var httpResponse = await _httpClient.SendAsync(request);
return await httpResponse.Content.ReadAsStringAsync();
}
/// <summary>
/// Triggers a DELETE request with headers to the specified route and retrieves the result as a string which you can serialize.
Expand All @@ -114,15 +95,11 @@ public static async Task<string> DeleteAndGetResponseAsStringAsync(string Route,
/// <returns>a string format of the HTTP Response message</returns>
public static async Task<string> DeleteAndGetResponseAsStringAsync(string Route, List<Header> Headers)
{
using (HttpClient httpClient = new HttpClient())
{
foreach (var Header in Headers)
{
httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
}
var httpResponse = await httpClient.DeleteAsync(new Uri(Route));
return await httpResponse.Content.ReadAsStringAsync();
}
using var request = new HttpRequestMessage(HttpMethod.Delete, new Uri(Route));
foreach (var Header in Headers)
request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
var httpResponse = await _httpClient.SendAsync(request);
return await httpResponse.Content.ReadAsStringAsync();
}
/// <summary>
/// Triggers a DELETE request with headers and authorization to the specified route and retrieves the result as a string which you can serialize.
Expand All @@ -133,17 +110,14 @@ public static async Task<string> DeleteAndGetResponseAsStringAsync(string Route,
/// <returns>a string format of the HTTP Response message</returns>
public static async Task<string> DeleteAndGetResponseAsStringAsync(string Route, List<Header> Headers, Authorization Authorization)
{
using (HttpClient httpClient = new HttpClient())
{
foreach (var Header in Headers)
{
httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
}
httpClient.DefaultRequestHeaders.Authorization = string.IsNullOrEmpty(Authorization.Parameter) ?
new AuthenticationHeaderValue(Authorization.Scheme) : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
var httpResponse = await httpClient.DeleteAsync(new Uri(Route));
return await httpResponse.Content.ReadAsStringAsync();
}
using var request = new HttpRequestMessage(HttpMethod.Delete, new Uri(Route));
foreach (var Header in Headers)
request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
request.Headers.Authorization = string.IsNullOrEmpty(Authorization.Parameter)
? new AuthenticationHeaderValue(Authorization.Scheme)
: new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
var httpResponse = await _httpClient.SendAsync(request);
return await httpResponse.Content.ReadAsStringAsync();
}

/// <summary>
Expand Down
Loading
Loading