Skip to content

Commit

Permalink
Tidy up logging code for Watch streams
Browse files Browse the repository at this point in the history
  • Loading branch information
tintoy committed Oct 16, 2024
1 parent 9ab1a6e commit 5137ec9
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/KubeClient/ResourceClients/KubeResourceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -594,9 +594,10 @@ protected IObservable<string> ObserveLines(Func<HttpRequest> requestFactory, str

try
{
HttpRequest request = requestFactory();

logger.LogDebug("Start streaming {RequestMethod} request for {RequestUri}...", HttpMethod.Get.Method, request.Uri.SafeGetPathAndQuery());
HttpRequest request = requestFactory().WithRequestAction((HttpRequestMessage requestMessage) =>
{
logger.LogDebug("Start streaming {RequestMethod} request for {RequestUri}...", HttpMethod.Get, requestMessage.RequestUri.SafeGetPathAndQuery());
});

using (HttpResponseMessage responseMessage = await Http.GetStreamedAsync(request, subscriptionCancellationToken).ConfigureAwait(false))
{
Expand Down
59 changes: 59 additions & 0 deletions src/KubeClient/Utilities/HttpRequestHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using HTTPlease;
using HTTPlease.Core;
using System;
using System.Net.Http;

namespace KubeClient.Utilities
{
/// <summary>
/// Helper methods for working with <see cref="HttpRequest"/> and related types.
/// </summary>
public static class HttpRequestHelper
{
/// <summary>
/// Expand an <see cref="HttpRequest"/>'s request URI, populating its URI template if necessary.
/// </summary>
/// <param name="request">
/// The target <see cref="HttpRequest"/>.
/// </param>
/// <param name="baseUri">
/// The base URI to use (optional, unless <see cref="HttpRequestBase.Uri"/> is <c>null</c> or not an absolute URI).
/// </param>
/// <returns>
/// The expanded request URI (always an absolute URI).
/// </returns>
public static Uri ExpandRequestUri(this HttpRequest request, Uri baseUri = null)
{
using (HttpRequestMessage requestMessage = request.BuildRequestMessage(HttpMethod.Get, body: null, baseUri))
{
return requestMessage.RequestUri;
}
}

/// <summary>
/// Expand an <see cref="HttpRequest{TContext}"/>'s request URI, populating its URI template if necessary.
/// </summary>
/// <typeparam name="TContext">
/// The type of object used as a context when evaluating request template parameters.
/// </typeparam>
/// <param name="request">
/// The target <see cref="HttpRequest{TContext}"/>.
/// </param>
/// <param name="context">
/// The <typeparamref name="TContext"/> used as a context when evaluating request template parameters.
/// </param>
/// <param name="baseUri">
/// The base URI to use (optional, unless <see cref="HttpRequestBase.Uri"/> is <c>null</c> or not an absolute URI).
/// </param>
/// <returns>
/// The expanded request URI (always an absolute URI).
/// </returns>
public static Uri ExpandRequestUri<TContext>(this HttpRequest<TContext> request, TContext context, Uri baseUri = null)
{
using (HttpRequestMessage requestMessage = request.BuildRequestMessage(HttpMethod.Get, context, body: null, baseUri))
{
return requestMessage.RequestUri;
}
}
}
}
21 changes: 19 additions & 2 deletions src/KubeClient/Utilities/UriHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,34 @@ public static class UriHelper
/// Unlike <see cref="Uri.PathAndQuery"/>, also handles relative URIs.
/// </remarks>
public static string SafeGetPathAndQuery(this Uri uri)
{
if (uri == null)
throw new ArgumentNullException(nameof(uri));

return uri.EnsureAbsolute().PathAndQuery;
}

/// <summary>
/// Ensure that the URI is an absolute URI.
/// </summary>
/// <param name="uri">
/// The target URI.
/// </param>
/// <returns>
/// The target URI, if <see cref="Uri.IsAbsoluteUri"/> is <c>true</c>; otherwise, an absolute URI using <see cref="DummyBaseUri"/> as the base URI.
/// </returns>
static Uri EnsureAbsolute(this Uri uri)
{
if (uri == null)
throw new ArgumentNullException(nameof(uri));

if (uri.IsAbsoluteUri)
return uri.PathAndQuery;
return uri;

// Slightly ugly, but System.Uri doesn't attempt to parse relative URIs so we have to convert it to an absolute URI.
Uri absoluteUri = new Uri(DummyBaseUri, relativeUri: uri);

return absoluteUri.PathAndQuery;
return absoluteUri;
}
}
}

0 comments on commit 5137ec9

Please sign in to comment.