diff --git a/Readme.md b/Readme.md index 9bbbe91..c7d2d4d 100644 --- a/Readme.md +++ b/Readme.md @@ -32,6 +32,19 @@ new UriBuilder("https://awesome.com/yo) ``` result: `https://awesome.com/yo?id=5` +you can even pass a dictionary of parameters + +```csharp + var dictionary = new Dictionary() + { + ["yo"] = "dawg" + }; + new UriBuilder("http://awesome.com") + .WithParameter(dictionary); + http://awesome.com/?yo=dawg + +``` + ## Getting started Just install the nuget package `install-package UriBuilder.Fluent` and thats it. The extension methods should be available to you! \ No newline at end of file diff --git a/src/UriBuilder.Fluent.UnitTests/ExtensionTests.cs b/src/UriBuilder.Fluent.UnitTests/ExtensionTests.cs index 308880a..548a78c 100644 --- a/src/UriBuilder.Fluent.UnitTests/ExtensionTests.cs +++ b/src/UriBuilder.Fluent.UnitTests/ExtensionTests.cs @@ -116,5 +116,19 @@ namespace FluentUriBuilder.Tests .WithParameter("supgf", "no22"); Assert.Equal("http://awesome.com/?awesome=yodawg&supg=no2&supgf=no22", url.Uri.ToString()); } + + [Fact] + public void AddDictOfParams() + { + var dictionary = new Dictionary() + { + ["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()); + } } } \ No newline at end of file diff --git a/src/UriBuilder.Fluent.UnitTests/ThrowsTests.cs b/src/UriBuilder.Fluent.UnitTests/ThrowsTests.cs index 012070c..e055915 100644 --- a/src/UriBuilder.Fluent.UnitTests/ThrowsTests.cs +++ b/src/UriBuilder.Fluent.UnitTests/ThrowsTests.cs @@ -16,6 +16,7 @@ namespace FluentUriBuilder.Tests Assert.Throws(() => tstObj.WithPathSegment(null)); Assert.Throws(() => tstObj.WithScheme(null)); Assert.Throws(() => tstObj.WithHost(null)); + Assert.Throws(() => tstObj.WithParameter(parameterDictionary: null)); Assert.Throws(() => tstObj.WithPort(-1)); } } diff --git a/src/UriBuilder.Fluent/TerribleDevUriExtensions.cs b/src/UriBuilder.Fluent/TerribleDevUriExtensions.cs index ac3f8ea..466c374 100644 --- a/src/UriBuilder.Fluent/TerribleDevUriExtensions.cs +++ b/src/UriBuilder.Fluent/TerribleDevUriExtensions.cs @@ -8,8 +8,38 @@ namespace System { public static class TerribleDevUriExtensions { + /// + /// 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. + /// + /// + /// + /// + /// public static UriBuilder WithParameter(this UriBuilder bld, string key, params string[] values) => bld.WithParameter(key, valuesEnum: values); + /// + /// Appends query strings from dictionary + /// + /// + /// + /// + public static UriBuilder WithParameter(this UriBuilder bld, IDictionary parameterDictionary) + { + if(parameterDictionary == null) throw new ArgumentNullException(nameof(parameterDictionary)); + foreach(var item in parameterDictionary) + { + bld.WithParameter(item.Key, item.Value); + } + return bld; + } + + /// + /// 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. + /// + /// + /// + /// + /// public static UriBuilder WithParameter(this UriBuilder bld, string key, IEnumerable valuesEnum) { if(string.IsNullOrWhiteSpace(key)) @@ -20,8 +50,7 @@ namespace System { valuesEnum = new string[0]; } - var isfirst = string.IsNullOrWhiteSpace(bld.Query); - var intitialValue = isfirst ? "" : $"{bld.Query.TrimStart('?')}&"; + var intitialValue = string.IsNullOrWhiteSpace(bld.Query) ? "" : $"{bld.Query.TrimStart('?')}&"; var sb = new StringBuilder($"{intitialValue}{key}"); var validValueHit = false; foreach(var value in valuesEnum) @@ -37,6 +66,13 @@ namespace System return bld; } + /// + /// Sets the port to be the port number + /// + /// + /// + /// Throws if port is less than one + /// public static UriBuilder WithPort(this UriBuilder bld, int port) { if(port < 1) throw new ArgumentOutOfRangeException(nameof(port)); @@ -44,6 +80,13 @@ namespace System return bld; } + /// + /// appends a path segment to the path. Can be called multiple times to append multiple segments + /// + /// + /// + /// You pass a string as a path segment + /// public static UriBuilder WithPathSegment(this UriBuilder bld, string pathSegment) { if(string.IsNullOrWhiteSpace(pathSegment)) @@ -55,6 +98,13 @@ namespace System return bld; } + /// + /// Sets your Uri Scheme + /// + /// + /// + /// You must pass a scheme + /// public static UriBuilder WithScheme(this UriBuilder bld, string scheme) { if(string.IsNullOrWhiteSpace(scheme)) throw new ArgumentNullException(nameof(scheme)); @@ -62,6 +112,13 @@ namespace System return bld; } + /// + /// + /// + /// + /// + /// You must pass a ho0st + /// public static UriBuilder WithHost(this UriBuilder bld, string host) { if(string.IsNullOrWhiteSpace(host)) throw new ArgumentNullException(nameof(host)); @@ -69,6 +126,12 @@ namespace System return bld; } + /// + /// Use Https? + /// + /// + /// default true, if false sets scheme to http + /// public static UriBuilder UseHttps(this UriBuilder bld, bool predicate = true) { bld.Scheme = predicate ? "https" : "http";