Skip to content

Instantly share code, notes, and snippets.

@CheetahChrome
Last active May 9, 2022 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CheetahChrome/a63167cb629e76b364cc2b40f960e9e3 to your computer and use it in GitHub Desktop.
Save CheetahChrome/a63167cb629e76b364cc2b40f960e9e3 to your computer and use it in GitHub Desktop.
C# Static Class Extensions
/// This is a repistory of many differen extensions I use throughout multiple projects.
/// A location here for ultimate move to other named extensions.
public static class HTTPExtensions
{
/// <summary>
/// Allow one to build a URL from its parts, similar to Path.Combine.
/// </summary>
/// <remarks>HMTL Extensions</Remarks>
/// <param name="baseUrl">Initial bitof the url</param>
/// <param name="segments">Each inidividual segment to join.</param>
/// <returns>The full Url with slashes.</returns>
public static string UrlCombine(this string baseUrl, params string[] segments)
=> string.Join("/", new[] { baseUrl.TrimEnd('/') }.Concat(segments.Select(s => s.Trim('/'))));
}
public static class MicrosoftGraphExtensions
{
/// <summary>
/// Provide string data if it exists at target key/value.
/// </summary>
/// <remarks>Microsoft Graph Extensions</remarks>
/// <param name="dictionary">Data dictionary</param>
/// <param name="key">Key of value to extract</param>
/// <returns>String of the value if key exists.</returns>
public static string? SetAsStringIfExists(this IDictionary<string, object> dictionary, string key)
=> dictionary.ContainsKey(key) ? dictionary[key]?.ToString() : null;
/// <summary>
/// Generate a Sharepont shared link with the url provided.
/// </summary>
/// <remarks>Microsoft Graph Extensions</remarks>///
/// <param name="url">Valid url</param>
/// <returns>Sharepont shared code url item.</returns>
public static string ToDirectSharedCode(this string url)
{
if (string.IsNullOrEmpty(url))
return string.Empty;
string sharingUrl = url;
string base64Value = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(sharingUrl));
string encodedUrl = "u!" + base64Value.TrimEnd('=').Replace('/', '_').Replace('+', '-');
return encodedUrl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment