From 941e8d9b3a0f49767ec1239dd09807fd7a0107a7 Mon Sep 17 00:00:00 2001 From: Anders Bjerner Date: Sat, 17 Aug 2019 17:48:02 +0200 Subject: [PATCH] Added some extra convience methods for making HTTP requests --- src/Skybrud.Essentials.Http/HttpRequest.cs | 72 +++++++++++++++++++ .../HttpUtils.Requests.cs | 26 +++++++ 2 files changed, 98 insertions(+) diff --git a/src/Skybrud.Essentials.Http/HttpRequest.cs b/src/Skybrud.Essentials.Http/HttpRequest.cs index 4698ffd..ae65b55 100644 --- a/src/Skybrud.Essentials.Http/HttpRequest.cs +++ b/src/Skybrud.Essentials.Http/HttpRequest.cs @@ -453,6 +453,78 @@ private void SetRequestContentLength(HttpWebRequest request, int contentLength) #endregion + #region Static methods + + /// + /// Initializes a new GET request based on the specified . + /// + /// The URL of the request. The query string may be part of the specified URL or via the property. + public static HttpRequest Get(string url) { + return new HttpRequest(HttpMethod.Get, url); + } + + /// + /// Initializes a new GET request based on the specified and . + /// + /// The URL of the request. + /// The query string of the request. + public static HttpRequest Get(string url, IHttpQueryString queryString) { + return new HttpRequest(HttpMethod.Get, url, queryString); + } + + /// + /// Initializes a new POST request based on the specified . + /// + /// The URL of the request. + /// The query string of the request. + public static HttpRequest Post(string url, IHttpQueryString queryString) { + return new HttpRequest(HttpMethod.Post, url, queryString); + } + + /// + /// Initializes a new POST request based on the specified and . + /// + /// The URL of the request. + /// The HTTP POST data of the request. + public static HttpRequest Post(string url, IHttpPostData postData) { + return new HttpRequest(HttpMethod.Post, url, postData); + } + + /// + /// Initializes a new POST request based on the specified , and . + /// + /// The URL of the request. + /// The query string of the request. + /// The HTTP POST data of the request. + public static HttpRequest Post(string url, IHttpQueryString queryString, IHttpPostData postData) { + return new HttpRequest(HttpMethod.Post, url, queryString, postData); + } + + /// + /// Initializes a new POST request based on the specified and JSON . + /// + /// With this method, the property is automatically set to application/json. + /// + /// The URL of the request. + /// An instance of representing the POST body. + public static HttpRequest Post(string url, JToken body) { + return new HttpRequest(HttpMethod.Post, url, body); + } + + /// + /// Initializes a new POST request based on the specified , and JSON . + /// + /// With this method, the property is automatically set to application/json. + /// + /// The URL of the request. + /// The query string of the request. + /// An instance of representing the POST body. + public static HttpRequest Post(string url, IHttpQueryString queryString, JToken body) { + return new HttpRequest(HttpMethod.Post, url, queryString, body); + } + + #endregion + } } \ No newline at end of file diff --git a/src/Skybrud.Essentials.Http/HttpUtils.Requests.cs b/src/Skybrud.Essentials.Http/HttpUtils.Requests.cs index fbe171c..3fbbf06 100644 --- a/src/Skybrud.Essentials.Http/HttpUtils.Requests.cs +++ b/src/Skybrud.Essentials.Http/HttpUtils.Requests.cs @@ -1,4 +1,5 @@ using System; +using Newtonsoft.Json.Linq; using Skybrud.Essentials.Http.Collections; using Skybrud.Essentials.Http.Options; @@ -116,6 +117,31 @@ public static IHttpResponse Post(string url, IHttpQueryString queryString, IHttp return DoHttpRequest(HttpMethod.Post, url, queryString, postData); } + /// + /// Makes a POST request to the specified and JSON . + /// + /// The URL of the request. + /// The representing the POST body. + /// An instance of representing the response. + public static IHttpResponse Post(string url, JToken body) { + if (string.IsNullOrWhiteSpace(url)) throw new ArgumentNullException(nameof(url)); + if (body == null) throw new ArgumentNullException(nameof(body)); + return new HttpRequest(HttpMethod.Post, url, body).GetResponse(); + } + + /// + /// Makes a POST request to the specified and JSON . + /// + /// The URL of the request. + /// The query string of the request. + /// The representing the POST body. + /// An instance of representing the response. + public static IHttpResponse Post(string url, IHttpQueryString queryString, JToken body) { + if (string.IsNullOrWhiteSpace(url)) throw new ArgumentNullException(nameof(url)); + if (body == null) throw new ArgumentNullException(nameof(body)); + return new HttpRequest(HttpMethod.Post, url, queryString, body).GetResponse(); + } + #endregion #region Put