2020-05-01 19:05:28 +01:00
|
|
|
using System.Web;
|
|
|
|
using System.Linq;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
|
|
|
|
namespace SpotifyAPI.Web
|
|
|
|
{
|
|
|
|
public static class URIExtensions
|
|
|
|
{
|
|
|
|
public static Uri ApplyParameters(this Uri uri, IDictionary<string, string> parameters)
|
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(uri, nameof(uri));
|
|
|
|
|
|
|
|
if (parameters == null || !parameters.Any())
|
|
|
|
{
|
|
|
|
return uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
var newParameters = new Dictionary<string, string>();
|
|
|
|
NameValueCollection existingParameters = HttpUtility.ParseQueryString(uri.Query);
|
|
|
|
foreach (string key in existingParameters.AllKeys)
|
|
|
|
{
|
|
|
|
newParameters.Add(key, existingParameters[key]);
|
|
|
|
}
|
|
|
|
foreach (KeyValuePair<string, string> parameter in parameters)
|
|
|
|
{
|
|
|
|
newParameters.Add(parameter.Key, HttpUtility.UrlEncode(parameter.Value));
|
|
|
|
}
|
|
|
|
|
2020-05-02 13:58:11 +01:00
|
|
|
var queryString = string.Join("&", newParameters.Select((parameter) => $"{parameter.Key}={parameter.Value}"));
|
2020-06-12 23:27:28 +01:00
|
|
|
var query = string.IsNullOrEmpty(queryString) ? null : queryString;
|
2020-05-01 19:05:28 +01:00
|
|
|
|
2020-05-02 12:04:26 +01:00
|
|
|
var uriBuilder = new UriBuilder(uri)
|
|
|
|
{
|
|
|
|
Query = query
|
|
|
|
};
|
2020-05-01 19:05:28 +01:00
|
|
|
|
|
|
|
return uriBuilder.Uri;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|