Last active
September 16, 2019 06:45
-
-
Save daydos/150902e1ef3d1b55f998c60e6b274402 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[DebuggerDisplay("{Uri}")] | |
public class UriManager : IUriManager | |
{ | |
private bool _hasUriChanged = true; | |
private Uri _uri; | |
private UriBuilder _uriBuilder; | |
/// <summary> | |
/// New Uri Manager | |
/// </summary> | |
/// <param name="uri">The URI.</param> | |
/// <param name="uriBuilder">The URI builder.</param> | |
public UriManager( | |
Uri uri, | |
UriBuilder uriBuilder = null) | |
{ | |
if (uriBuilder != null) | |
{ | |
_uriBuilder = uriBuilder; | |
} | |
SetUri(uri); | |
} | |
/// <summary> | |
/// Uri of UriManager | |
/// </summary> | |
public Uri Uri | |
{ | |
get | |
{ | |
RefreshUri(); | |
return _uri; | |
} | |
} | |
/// <summary> | |
/// Parameters Dictionary Collection | |
/// </summary> | |
public IReadOnlyCollection<Parameter> Parameters { get; set; } | |
/// <summary> | |
/// If the provided parameter doesn't exist adds a new one, if it does updates the current parameter. | |
/// </summary> | |
/// <param name="parameter">Parameter to add.</param> | |
public void SetParameter(Parameter parameter) | |
{ | |
//..... | |
_hasUriChanged = true; | |
} | |
/// <summary> | |
/// Sets the Uri of this instance. | |
/// </summary> | |
/// <param name="uri">The URI.</param> | |
/// <param name="force">If set to <c>true</c>, URI will re-initialized even its state is not changed.</param> | |
/// <remarks>This is a quite tricky operation, hence not in the setter of Uri</remarks> | |
public void SetUri(Uri uri, bool force = false) | |
{ | |
if (_uri == uri && !force) | |
{ | |
return; | |
} | |
// Parses the URL and stores the parameters | |
_uri = ParseUri(uri); | |
} | |
/// <summary> | |
/// Refresh Uri Property to reflect all parameters changes | |
/// </summary> | |
public void RefreshUri() | |
{ | |
if (!_hasUriChanged) | |
{ | |
return; | |
} | |
// Rebuilds the Uri if parameters have changed | |
_uri = BuildUri(_uri); | |
_hasUriChanged = false; | |
} | |
/// <summary> | |
/// Parses query parameters. | |
/// </summary> | |
/// <param name="uri">The URI.</param> | |
public void ParseUri(Uri uri) | |
{ | |
// .... | |
SetParameter(param); | |
} | |
/// <summary> | |
/// Builds a new URI using parameters. | |
/// </summary> | |
/// <param name="uri">The URI.</param> | |
public void BuildUri(Uri uri) | |
{ | |
var builder = new StringBuilder(); | |
builder.Append(uri.GetLeftPart(UriPartial.Authority)); | |
builder.Append(ResolvePath(uri)); | |
AppendParameters(uri); | |
return new Uri(builder.ToString(), uriManager.DontEscape); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment