using System; using System.Diagnostics; using System.Text; namespace JetBrains.TeamCity.NuGet { /// /// Represents the set of extension methods used by this project. /// internal static class ExtensionMethods { /// /// Writes a event message to the trace listeners in the collection using the specified message. /// /// A instance to write the event to. /// The error message. public static void Error(this TraceSource traceSource, string message) { traceSource.TraceEvent(TraceEventType.Error, 0, message); } /// /// Writes an event message to the trace listeners in the collection using the specified message. /// /// A instance to write the event to. /// The message to write. public static void Info(this TraceSource traceSource, string message) { traceSource.TraceEvent(TraceEventType.Information, 0, message); } /// /// Converts the current with just the host by discarding the other parts like path and querystrings. /// /// The current to convert. /// A with only the host. public static Uri ToHostOnly(this Uri uri) { return uri.Segments.Length > 1 ? new Uri($"{uri.Scheme}://{uri.Host}") : uri; } /// /// Converts the current string to a JSON web access token (JWT) as a string. /// /// The current access token as a string. /// A JWT as a JSON string. public static string ToJsonWebTokenString(this string accessToken) { // Effictively this splits by '.' and converts from a base-64 encoded string. Splitting creates new strings so this just calculates // a substring instead to reduce memory overhead. int start = accessToken.IndexOf(".", StringComparison.Ordinal) + 1; if (start < 0) { return null; } int length = accessToken.IndexOf(".", start, StringComparison.Ordinal) - start; return start > 0 && length < accessToken.Length ? Encoding.UTF8.GetString( Convert.FromBase64String( accessToken.Substring(start, length) .PadRight(length % 2 == 1 ? length + 1 : length, '='))) : null; } /// /// Writes a event message to the trace listeners in the collection using the specified message. /// /// A instance to write the event to. /// The message to write. public static void Verbose(this TraceSource traceSource, string message) { traceSource.TraceEvent(TraceEventType.Verbose, 0, message); } } }