8 Commits
1.0.4 ... 1.2

Author SHA1 Message Date
Tommy Parnell
fb6675ab94 escaped uris 2016-11-16 16:17:48 -05:00
Tommy Parnell
8b053f0f77 minor doc comment 2016-11-12 16:58:01 -05:00
Tommy Parnell
b68af327ab Merge pull request #8 from TerribleDev/1.1
1.1
2016-11-12 16:52:02 -05:00
Tommy Parnell
68bf719c27 Merge pull request #7 from TerribleDev/dictOfParams
Dict of params
2016-11-12 16:49:50 -05:00
Tommy Parnell
3ff3165a42 add exception throw test 2016-11-12 16:40:22 -05:00
Tommy Parnell
16f96b9b27 add dictionary of params 2016-11-12 16:38:10 -05:00
Tommy Parnell
04e2090a95 Merge pull request #5 from TerribleDev/documentation
add docs issue #4
2016-11-12 16:25:34 -05:00
Tommy Parnell
6e3e23061a add docs issue #4 2016-11-12 14:17:46 -05:00
4 changed files with 98 additions and 3 deletions

View File

@@ -32,6 +32,19 @@ new UriBuilder("https://awesome.com/yo)
``` ```
result: `https://awesome.com/yo?id=5` result: `https://awesome.com/yo?id=5`
you can even pass a dictionary of parameters
```csharp
var dictionary = new Dictionary<string, string>()
{
["yo"] = "dawg"
};
new UriBuilder("http://awesome.com")
.WithParameter(dictionary);
http://awesome.com/?yo=dawg
```
## Getting started ## Getting started
Just install the nuget package `install-package UriBuilder.Fluent` and thats it. The extension methods should be available to you! Just install the nuget package `install-package UriBuilder.Fluent` and thats it. The extension methods should be available to you!

View File

@@ -116,5 +116,19 @@ namespace FluentUriBuilder.Tests
.WithParameter("supgf", "no22"); .WithParameter("supgf", "no22");
Assert.Equal("http://awesome.com/?awesome=yodawg&supg=no2&supgf=no22", url.Uri.ToString()); Assert.Equal("http://awesome.com/?awesome=yodawg&supg=no2&supgf=no22", url.Uri.ToString());
} }
[Fact]
public void AddDictOfParams()
{
var dictionary = new Dictionary<string, string>()
{
["yo"] = "dawg",
["troll"] = "toll",
["hammer"] = string.Empty
};
var url = new UriBuilder("http://awesome.com")
.WithParameter(dictionary);
Assert.Equal("http://awesome.com/?yo=dawg&troll=toll&hammer", url.Uri.ToString());
}
} }
} }

View File

@@ -16,6 +16,7 @@ namespace FluentUriBuilder.Tests
Assert.Throws<ArgumentNullException>(() => tstObj.WithPathSegment(null)); Assert.Throws<ArgumentNullException>(() => tstObj.WithPathSegment(null));
Assert.Throws<ArgumentNullException>(() => tstObj.WithScheme(null)); Assert.Throws<ArgumentNullException>(() => tstObj.WithScheme(null));
Assert.Throws<ArgumentNullException>(() => tstObj.WithHost(null)); Assert.Throws<ArgumentNullException>(() => tstObj.WithHost(null));
Assert.Throws<ArgumentNullException>(() => tstObj.WithParameter(parameterDictionary: null));
Assert.Throws<ArgumentOutOfRangeException>(() => tstObj.WithPort(-1)); Assert.Throws<ArgumentOutOfRangeException>(() => tstObj.WithPort(-1));
} }
} }

View File

@@ -8,8 +8,39 @@ namespace System
{ {
public static class TerribleDevUriExtensions public static class TerribleDevUriExtensions
{ {
/// <summary>
/// Appends a query string parameter with a key, and many values. Multiple values will be comma seperated. If only 1 value is passed and its null or value, the key will be added to the QS.
/// </summary>
/// <param name="bld"></param>
/// <param name="key"></param>
/// <param name="values"></param>
/// <returns></returns>
public static UriBuilder WithParameter(this UriBuilder bld, string key, params string[] values) => bld.WithParameter(key, valuesEnum: values); public static UriBuilder WithParameter(this UriBuilder bld, string key, params string[] values) => bld.WithParameter(key, valuesEnum: values);
/// <summary>
/// Appends query strings from dictionary
/// </summary>
/// <param name="bld"></param>
/// <param name="parameterDictionary"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <returns></returns>
public static UriBuilder WithParameter(this UriBuilder bld, IDictionary<string, string> parameterDictionary)
{
if(parameterDictionary == null) throw new ArgumentNullException(nameof(parameterDictionary));
foreach(var item in parameterDictionary)
{
bld.WithParameter(item.Key, item.Value);
}
return bld;
}
/// <summary>
/// Appends a query string parameter with a key, and many values. Multiple values will be comma seperated. If only 1 value is passed and its null or value, the key will be added to the QS.
/// </summary>
/// <param name="bld"></param>
/// <param name="key"></param>
/// <param name="valuesEnum"></param>
/// <returns></returns>
public static UriBuilder WithParameter(this UriBuilder bld, string key, IEnumerable<object> valuesEnum) public static UriBuilder WithParameter(this UriBuilder bld, string key, IEnumerable<object> valuesEnum)
{ {
if(string.IsNullOrWhiteSpace(key)) if(string.IsNullOrWhiteSpace(key))
@@ -20,14 +51,14 @@ namespace System
{ {
valuesEnum = new string[0]; valuesEnum = new string[0];
} }
var isfirst = string.IsNullOrWhiteSpace(bld.Query); var intitialValue = string.IsNullOrWhiteSpace(bld.Query) ? "" : $"{bld.Query.TrimStart('?')}&";
var intitialValue = isfirst ? "" : $"{bld.Query.TrimStart('?')}&";
var sb = new StringBuilder($"{intitialValue}{key}"); var sb = new StringBuilder($"{intitialValue}{key}");
var validValueHit = false; var validValueHit = false;
foreach(var value in valuesEnum) foreach(var value in valuesEnum)
{ {
var toSValue = value?.ToString(); var toSValue = value?.ToString();
if(string.IsNullOrWhiteSpace(toSValue)) continue; //whitespace is valid
if(string.IsNullOrEmpty(toSValue)) continue;
// we can't just have an = sign since its valid to have query string paramters with no value; // we can't just have an = sign since its valid to have query string paramters with no value;
if(!validValueHit) toSValue = "=" + value; if(!validValueHit) toSValue = "=" + value;
validValueHit = true; validValueHit = true;
@@ -37,6 +68,13 @@ namespace System
return bld; return bld;
} }
/// <summary>
/// Sets the port to be the port number
/// </summary>
/// <param name="bld"></param>
/// <param name="port"></param>
/// <exception cref="ArgumentOutOfRangeException">Throws if port is less than one</exception>
/// <returns></returns>
public static UriBuilder WithPort(this UriBuilder bld, int port) public static UriBuilder WithPort(this UriBuilder bld, int port)
{ {
if(port < 1) throw new ArgumentOutOfRangeException(nameof(port)); if(port < 1) throw new ArgumentOutOfRangeException(nameof(port));
@@ -44,6 +82,13 @@ namespace System
return bld; return bld;
} }
/// <summary>
/// appends a path segment to the path. Can be called multiple times to append multiple segments
/// </summary>
/// <param name="bld"></param>
/// <param name="pathSegment"></param>
/// <exception cref="ArgumentNullException">You pass a string as a path segment</exception>
/// <returns></returns>
public static UriBuilder WithPathSegment(this UriBuilder bld, string pathSegment) public static UriBuilder WithPathSegment(this UriBuilder bld, string pathSegment)
{ {
if(string.IsNullOrWhiteSpace(pathSegment)) if(string.IsNullOrWhiteSpace(pathSegment))
@@ -55,6 +100,13 @@ namespace System
return bld; return bld;
} }
/// <summary>
/// Sets your Uri Scheme
/// </summary>
/// <param name="bld"></param>
/// <param name="scheme"></param>
/// <exception cref="ArgumentNullException">You must pass a scheme</exception>
/// <returns></returns>
public static UriBuilder WithScheme(this UriBuilder bld, string scheme) public static UriBuilder WithScheme(this UriBuilder bld, string scheme)
{ {
if(string.IsNullOrWhiteSpace(scheme)) throw new ArgumentNullException(nameof(scheme)); if(string.IsNullOrWhiteSpace(scheme)) throw new ArgumentNullException(nameof(scheme));
@@ -62,6 +114,13 @@ namespace System
return bld; return bld;
} }
/// <summary>
///
/// </summary>
/// <param name="bld"></param>
/// <param name="host"></param>
/// <exception cref="ArgumentNullException">You must pass a host</exception>
/// <returns></returns>
public static UriBuilder WithHost(this UriBuilder bld, string host) public static UriBuilder WithHost(this UriBuilder bld, string host)
{ {
if(string.IsNullOrWhiteSpace(host)) throw new ArgumentNullException(nameof(host)); if(string.IsNullOrWhiteSpace(host)) throw new ArgumentNullException(nameof(host));
@@ -69,10 +128,18 @@ namespace System
return bld; return bld;
} }
/// <summary>
/// Use Https?
/// </summary>
/// <param name="bld"></param>
/// <param name="predicate">default true, if false sets scheme to http</param>
/// <returns></returns>
public static UriBuilder UseHttps(this UriBuilder bld, bool predicate = true) public static UriBuilder UseHttps(this UriBuilder bld, bool predicate = true)
{ {
bld.Scheme = predicate ? "https" : "http"; bld.Scheme = predicate ? "https" : "http";
return bld; return bld;
} }
public static string ToEscapedString(this Uri bld) => Uri.EscapeUriString(bld.ToString());
public static string ToEscapedString(this UriBuilder bld) => Uri.EscapeUriString(bld.Uri.ToString());
} }
} }