diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..ebead36 --- /dev/null +++ b/Readme.md @@ -0,0 +1,30 @@ +## FluentUriBuilder + +This lets you do things like + + +```csharp + +new UriBuilder() + .WithParameter("awesome", "yodawg") + .WithParameter("fun", ["cool", "yay"]) + .WithHost("awesome.com") + .WithPathSegment("seg") + .UseHttps() + .ToString() + +result: https://awesome.com/seg?awesome=yodawg&fun=cool,yay + +``` + +or + +```csharp + +new UriBuilder("https://awesome.com/yo) + .WithParameter("id", "5") + .ToString(); + +result: https://awesome.com/yo?id=5 + +``` \ No newline at end of file diff --git a/src/FluentUriBuilder/TerribleDevUriExtensions.cs b/src/FluentUriBuilder/TerribleDevUriExtensions.cs index 4391ac8..790b80c 100644 --- a/src/FluentUriBuilder/TerribleDevUriExtensions.cs +++ b/src/FluentUriBuilder/TerribleDevUriExtensions.cs @@ -8,26 +8,77 @@ namespace System { public static class TerribleDevUriExtensions { - public static void WithParameter(this UriBuilder bld, string key, string value) + public static UriBuilder WithParameter(this UriBuilder bld, string key, string value) { + if(string.IsNullOrWhiteSpace(key)) + { + throw new ArgumentNullException(nameof(key)); + } + if(string.IsNullOrWhiteSpace(value)) + { + throw new ArgumentNullException(nameof(value)); + } if(!string.IsNullOrWhiteSpace(bld.Query)) { bld.Query += $"&{key}={value}"; - return; + return bld; } bld.Query = $"{key}={value}"; + return bld; } - public static void WithParameter(this UriBuilder bld, string key, IEnumerable values) + public static UriBuilder WithParameter(this UriBuilder bld, string key, IEnumerable values) { var isfirst = string.IsNullOrWhiteSpace(bld.Query); - var intitialValue = isfirst ? "?" : "&"; + var intitialValue = isfirst ? "?" : $"{bld.Query}& "; var sb = new StringBuilder($"{intitialValue}{key}="); foreach(var value in values) { - sb.Append($"{value.ToString()},"); + var toSValue = value?.ToString(); + if(string.IsNullOrWhiteSpace(toSValue)) continue; + sb.Append($"{value},"); } bld.Query = sb.ToString().TrimEnd(','); + return bld; + } + + public static UriBuilder WithPort(this UriBuilder bld, int port) + { + if(port < 1) throw new ArgumentOutOfRangeException(nameof(port)); + bld.Port = port; + return bld; + } + + public static UriBuilder WithPathSegment(this UriBuilder bld, string pathSegment) + { + var path = pathSegment.TrimStart('/'); + if(string.IsNullOrWhiteSpace(bld.Path)) + { + bld.Path = path; + return bld; + } + bld.Path = $"{bld.Path.TrimEnd('/')}/{path}"; + return bld; + } + + public static UriBuilder WithScheme(this UriBuilder bld, string scheme) + { + if(string.IsNullOrWhiteSpace(scheme)) throw new ArgumentNullException(nameof(scheme)); + bld.Scheme = scheme; + return bld; + } + + public static UriBuilder WithHost(this UriBuilder bld, string host) + { + if(string.IsNullOrWhiteSpace(host)) throw new ArgumentNullException(nameof(host)); + bld.Host = host; + return bld; + } + + public static UriBuilder UseHttps(this UriBuilder bld, bool predicate = true) + { + if(predicate) bld.Scheme = "https"; + return bld; } } } \ No newline at end of file