diff --git a/BinaryDad.Extensions/Extensions/HttpExtensions.cs b/BinaryDad.Extensions/Extensions/HttpExtensions.cs index 30c7645..0fd04b0 100644 --- a/BinaryDad.Extensions/Extensions/HttpExtensions.cs +++ b/BinaryDad.Extensions/Extensions/HttpExtensions.cs @@ -1,4 +1,6 @@ -using System.Net.Http; +using System.IO; +using System.Net; +using System.Net.Http; using System.Text; using System.Threading.Tasks; @@ -22,6 +24,40 @@ namespace BinaryDad.Extensions return client.PostAsync(requestUrl, content); } + /// + /// Send a PATCH request to the specified Uri as an asynchronous operation. + /// + /// + /// + /// + /// + /// + public static Task PatchAsJsonAsync(this HttpClient client, string requestUrl, object model, bool useDataContractJsonSerializer = false) + { + var content = new StringContent(model.Serialize(), Encoding.UTF8, "application/json"); + + return client.PatchAsync(requestUrl, content); + } + + /// + /// Send a PATCH request to the specified Uri as an asynchronous operation. + /// + /// + /// + /// + /// + public static Task PatchAsync(this HttpClient client, string requestUri, HttpContent content) + { + var method = new HttpMethod("PATCH"); + + var message = new HttpRequestMessage(method, requestUri) + { + Content = content + }; + + return client.SendAsync(message); + } + /// /// Serialize the HTTP content to type as an asynchronous operation. /// @@ -35,5 +71,41 @@ namespace BinaryDad.Extensions return result.Deserialize(); } + + /// + /// Gets the body of a as a string + /// + /// + /// + public static string GetResponseBody(this WebResponse response) + { + if (response != null) + { + using (var reader = new StreamReader(response.GetResponseStream())) + { + return reader.ReadToEnd(); + } + } + + return null; + } + + /// + /// Gets the body of a as a string + /// + /// + /// + public static async Task GetResponseBodyAsync(this WebResponse response) + { + if (response != null) + { + using (var reader = new StreamReader(response.GetResponseStream())) + { + return await reader.ReadToEndAsync(); + } + } + + return null; + } } }