Updated versions

Added overload for IEnumerable<KeyValuePair<string, string>>
Added icon
Standardized {'s around validation
This commit is contained in:
Tony Valenti
2023-07-11 21:48:44 -05:00
parent 819235eedb
commit aa1e0600da
6 changed files with 89 additions and 19 deletions

BIN
UriBulider.Fluent.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@@ -17,7 +17,7 @@ namespace FluentUriBuilder.Tests
Assert.Throws<ArgumentNullException>(() => tstObj.WithPathSegment(null));
Assert.Throws<ArgumentNullException>(() => tstObj.WithScheme(null));
Assert.Throws<ArgumentNullException>(() => tstObj.WithHost(null));
Assert.Throws<ArgumentNullException>(() => tstObj.WithParameter(parameterDictionary: null));
Assert.Throws<ArgumentNullException>(() => tstObj.WithParameter((IEnumerable<KeyValuePair<string, string>>) null));
Assert.Throws<ArgumentNullException>(() => tstObj.WithFragment(fragmentDictionary: null));
Assert.Throws<ArgumentOutOfRangeException>(() => tstObj.WithPort(-1));
UriBuilder nullPtr = null;

View File

@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net461</TargetFrameworks>
<TargetFrameworks>net480;net60;net70</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />

View File

@@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace System
{
public static class TerribleDevUriExtensions
public static partial 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.
@@ -17,21 +17,25 @@ namespace System
/// <returns></returns>
public static UriBuilder WithParameter(this UriBuilder bld, string key, params string[] values) => bld.WithParameter(key, valuesEnum: values);
/// <summary>
/// Appends query strings from dictionary
/// Appends query strings from a list of key-value pairs (usually a 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)
public static UriBuilder WithParameter(this UriBuilder bld, IEnumerable<KeyValuePair<string, string>> parameterDictionary)
{
if(bld == null)
if (bld == null)
{
throw new ArgumentNullException(nameof(bld));
}
if(parameterDictionary == null) throw new ArgumentNullException(nameof(parameterDictionary));
foreach(var item in parameterDictionary)
if (parameterDictionary == null)
{
throw new ArgumentNullException(nameof(parameterDictionary));
}
foreach (var item in parameterDictionary)
{
bld.WithParameter(item.Key, item.Value);
}
@@ -86,7 +90,10 @@ namespace System
{
throw new ArgumentNullException(nameof(bld));
}
if(fragmentDictionary == null) throw new ArgumentNullException(nameof(fragmentDictionary));
if (fragmentDictionary == null)
{
throw new ArgumentNullException(nameof(fragmentDictionary));
}
foreach(var item in fragmentDictionary)
{
bld.WithFragment(item.Key, item.Value);
@@ -133,7 +140,10 @@ namespace System
{
throw new ArgumentNullException(nameof(bld));
}
if(port < 1) throw new ArgumentOutOfRangeException(nameof(port));
if(port < 1)
{
throw new ArgumentOutOfRangeException(nameof(port));
}
bld.Port = port;
return bld;
}
@@ -149,7 +159,10 @@ namespace System
{
throw new ArgumentNullException(nameof(bld));
}
if (bld.Uri.IsDefaultPort) bld.Port = -1;
if (bld.Uri.IsDefaultPort)
{
bld.Port = -1;
}
return bld;
}
@@ -188,7 +201,10 @@ namespace System
{
throw new ArgumentNullException(nameof(bld));
}
if(string.IsNullOrWhiteSpace(scheme)) throw new ArgumentNullException(nameof(scheme));
if (string.IsNullOrWhiteSpace(scheme))
{
throw new ArgumentNullException(nameof(scheme));
}
bld.Scheme = scheme;
return bld;
}
@@ -206,7 +222,10 @@ namespace System
{
throw new ArgumentNullException(nameof(bld));
}
if(string.IsNullOrWhiteSpace(host)) throw new ArgumentNullException(nameof(host));
if (string.IsNullOrWhiteSpace(host))
{
throw new ArgumentNullException(nameof(host));
}
bld.Host = host;
return bld;
}

View File

@@ -0,0 +1,34 @@
#if NET6_0_OR_GREATER
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace System
{
public static partial class TerribleDevUriExtensions
{
/// <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, IEnumerable<(string, string)> parameterDictionary)
{
if (bld == null) {
throw new ArgumentNullException(nameof(bld));
}
if (parameterDictionary == null) throw new ArgumentNullException(nameof(parameterDictionary));
foreach (var item in parameterDictionary) {
bld.WithParameter(item.Item1, item.Item2);
}
return bld;
}
}
}
#endif

View File

@@ -2,21 +2,38 @@
<PropertyGroup>
<Authors>Tommy Parnell</Authors>
<TargetFrameworks>netstandard1.1;net40;net45;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net480;net60;net70</TargetFrameworks>
<AssemblyName>UriBuilder.Fluent</AssemblyName>
<PackageId>UriBuilder.Fluent</PackageId>
<PackageTags>Url building;Uri;Uri building;fluent;extension</PackageTags>
<PackageProjectUrl>https://github.com/TerribleDev/UriBuilder.Fluent</PackageProjectUrl>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<FileVersion>7.0.0.0</FileVersion>
<Version>7.0.0</Version>
<PackageIcon>UriBulider.Fluent.png</PackageIcon>
<PackageIconUrl />
</PropertyGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'net480' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<PropertyGroup>
<PackageReleaseNotes>
This package adds extension methods over System.UriBuilder to help deal with query string parameters and create more of a fluent interface.
Unlike other projects, this NetStandardLibrary compliant package builds ontop of trusty UriBuilder, does not use custom Uri generators, and has no outside dependencies!
</PackageReleaseNotes>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile />
</PropertyGroup>
<ItemGroup>
<None Include="..\..\Readme.md" Pack="true" PackagePath="\" />
<None Include="..\..\UriBulider.Fluent.png" Pack="true" PackagePath="\" />
</ItemGroup>
</Project>