Skip to content

Commit

Permalink
Added some extra convience methods for making HTTP requests
Browse files Browse the repository at this point in the history
  • Loading branch information
abjerner committed Aug 17, 2019
1 parent 0a2ed4c commit 941e8d9
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/Skybrud.Essentials.Http/HttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,78 @@ private void SetRequestContentLength(HttpWebRequest request, int contentLength)

#endregion

#region Static methods

/// <summary>
/// Initializes a new GET request based on the specified <paramref name="url"/>.
/// </summary>
/// <param name="url">The URL of the request. The query string may be part of the specified URL or via the <see cref="QueryString"/> property.</param>
public static HttpRequest Get(string url) {
return new HttpRequest(HttpMethod.Get, url);
}

/// <summary>
/// Initializes a new GET request based on the specified <paramref name="url"/> and <paramref name="queryString"/>.
/// </summary>
/// <param name="url">The URL of the request.</param>
/// <param name="queryString">The query string of the request.</param>
public static HttpRequest Get(string url, IHttpQueryString queryString) {
return new HttpRequest(HttpMethod.Get, url, queryString);
}

/// <summary>
/// Initializes a new POST request based on the specified <paramref name="url"/>.
/// </summary>
/// <param name="url">The URL of the request.</param>
/// <param name="queryString">The query string of the request.</param>
public static HttpRequest Post(string url, IHttpQueryString queryString) {
return new HttpRequest(HttpMethod.Post, url, queryString);
}

/// <summary>
/// Initializes a new POST request based on the specified <paramref name="url"/> and <paramref name="postData"/>.
/// </summary>
/// <param name="url">The URL of the request.</param>
/// <param name="postData">The HTTP POST data of the request.</param>
public static HttpRequest Post(string url, IHttpPostData postData) {
return new HttpRequest(HttpMethod.Post, url, postData);
}

/// <summary>
/// Initializes a new POST request based on the specified <paramref name="url"/>, <paramref name="queryString"/> and <paramref name="postData"/>.
/// </summary>
/// <param name="url">The URL of the request.</param>
/// <param name="queryString">The query string of the request.</param>
/// <param name="postData">The HTTP POST data of the request.</param>
public static HttpRequest Post(string url, IHttpQueryString queryString, IHttpPostData postData) {
return new HttpRequest(HttpMethod.Post, url, queryString, postData);
}

/// <summary>
/// Initializes a new POST request based on the specified <paramref name="url"/> and JSON <paramref name="body"/>.
///
/// With this method, the <see cref="ContentType"/> property is automatically set to <c>application/json</c>.
/// </summary>
/// <param name="url">The URL of the request.</param>
/// <param name="body">An instance of <see cref="JToken"/> representing the POST body.</param>
public static HttpRequest Post(string url, JToken body) {
return new HttpRequest(HttpMethod.Post, url, body);
}

/// <summary>
/// Initializes a new POST request based on the specified <paramref name="url"/>, <paramref name="queryString"/> and JSON <paramref name="body"/>.
///
/// With this method, the <see cref="ContentType"/> property is automatically set to <c>application/json</c>.
/// </summary>
/// <param name="url">The URL of the request.</param>
/// <param name="queryString">The query string of the request.</param>
/// <param name="body">An instance of <see cref="JToken"/> representing the POST body.</param>
public static HttpRequest Post(string url, IHttpQueryString queryString, JToken body) {
return new HttpRequest(HttpMethod.Post, url, queryString, body);
}

#endregion

}

}
26 changes: 26 additions & 0 deletions src/Skybrud.Essentials.Http/HttpUtils.Requests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Newtonsoft.Json.Linq;
using Skybrud.Essentials.Http.Collections;
using Skybrud.Essentials.Http.Options;

Expand Down Expand Up @@ -116,6 +117,31 @@ public static IHttpResponse Post(string url, IHttpQueryString queryString, IHttp
return DoHttpRequest(HttpMethod.Post, url, queryString, postData);
}

/// <summary>
/// Makes a POST request to the specified <paramref name="url"/> and JSON <paramref name="body"/>.
/// </summary>
/// <param name="url">The URL of the request.</param>
/// <param name="body">The <see cref="JToken"/> representing the POST body.</param>
/// <returns>An instance of <see cref="IHttpResponse"/> representing the response.</returns>
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();
}

/// <summary>
/// Makes a POST request to the specified <paramref name="url"/> and JSON <paramref name="body"/>.
/// </summary>
/// <param name="url">The URL of the request.</param>
/// <param name="queryString">The query string of the request.</param>
/// <param name="body">The <see cref="JToken"/> representing the POST body.</param>
/// <returns>An instance of <see cref="IHttpResponse"/> representing the response.</returns>
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
Expand Down

0 comments on commit 941e8d9

Please sign in to comment.