Skip to content

Commit

Permalink
feat: Support url segments and add base url to logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Abbas-b-b committed Aug 5, 2024
1 parent a2b8951 commit 8e33bfd
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 11 deletions.
23 changes: 15 additions & 8 deletions src/Garnet.Detail.ExternalService.Rest/Clients/BasicRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,18 @@ public virtual async Task<RestResponse> SendRequestAsync(RestRequest request)
/// <param name="uri">Relative path for the request</param>
/// <param name="httpMethod">HttpMethod of the request</param>
/// <param name="request">Request parameters which will be added to the body or query parameters in respect to the request method (GET or not)</param>
/// <param name="urlSegmentValues">List of values to replace route arguments</param>
/// <typeparam name="TRequest">Type of the request object</typeparam>
/// <typeparam name="TResponse">Type of the response object</typeparam>
/// <returns>Response object if request is success</returns>
public virtual async Task<TResponse> SendRequestAsync<TRequest, TResponse>(string uri, Method httpMethod,
TRequest request)
public virtual async Task<TResponse> SendRequestAsync<TRequest, TResponse>(string uri,
Method httpMethod,
TRequest request,
params object?[] urlSegmentValues)

Check warning on line 81 in src/Garnet.Detail.ExternalService.Rest/Clients/BasicRestClient.cs

View workflow job for this annotation

GitHub Actions / pack_push

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
where TRequest : class
where TResponse : class
{
var restRequest = CreateRestRequestAndAddParameters(uri, httpMethod, request);
var restRequest = CreateRestRequestAndAddParameters(uri, httpMethod, request, urlSegmentValues);

var restResponse = await SendRequestAsync(restRequest);

Expand All @@ -98,10 +101,12 @@ public virtual async Task<TResponse> SendRequestAsync<TRequest, TResponse>(strin
/// <param name="uri">The relative path of the request</param>
/// <param name="httpMethod">Request http method</param>
/// <param name="requestData">An object to fetch parameters from</param>
/// <param name="urlSegmentValues">List of values to replace route arguments</param>
/// <returns>RestSharp request after adding the parameters</returns>
protected virtual RestRequest CreateRestRequestAndAddParameters(string uri, Method httpMethod, object requestData)
protected virtual RestRequest CreateRestRequestAndAddParameters(string uri, Method httpMethod, object requestData,
params object?[] urlSegmentValues)

Check warning on line 107 in src/Garnet.Detail.ExternalService.Rest/Clients/BasicRestClient.cs

View workflow job for this annotation

GitHub Actions / pack_push

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
return RequestUtility.CreateRestRequestAndAddParameters(uri, httpMethod, requestData);
return RequestUtility.CreateRestRequestAndAddParameters(uri, httpMethod, requestData, urlSegmentValues);
}

/// <summary>
Expand Down Expand Up @@ -186,8 +191,9 @@ protected void LogRequestResponse(RestRequest restRequest, RestResponse restResp
{
using (Logger.BeginScope("Request_Response_Log"))
{
Logger.LogInformation("A {$httpMethod} request to {$uri} in {$executionTime} ms with parameters {@parameters} has been sent with response status {$status} and content: {$content}",
Logger.LogInformation("A {$httpMethod} request to {$baseUri} with route {$route} in {$executionTime} ms with parameters {@parameters} has been sent with response status {$status} and content: {$content}",
restRequest.Method,
Client.Options.BaseUrl,
restRequest.Resource,
executionTime.TotalMilliseconds,
restRequest.Parameters.ToList(),
Expand All @@ -203,9 +209,10 @@ protected void LogRequestResponse(RestRequest restRequest, RestResponse restResp
/// <param name="restResponse"></param>
protected void LogFailedResponseReceived(RestRequest restRequest, RestResponse restResponse)
{
Logger.LogError(restResponse.ErrorException,
"A {$httpMethod} request to {$uri} with parameters {@parameters} has been failed with status {$status} and error: {$error} and content: {$content}",
Logger.LogError(restResponse.ErrorException,
"A {$httpMethod} request to {$baseUri} with route {$uri} with parameters {@parameters} has been failed with status {$status} and error: {$error} and content: {$content}",
restRequest.Method,
Client.Options.BaseUrl,
restRequest.Resource,
restRequest.Parameters.ToList(),
restResponse.StatusCode,
Expand Down
54 changes: 51 additions & 3 deletions src/Garnet.Detail.ExternalService.Rest/Utilities/RequestUtility.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using System.Text.RegularExpressions;
using RestSharp;

namespace Garnet.Detail.ExternalService.Rest.Utilities;
Expand All @@ -13,16 +15,55 @@ public class RequestUtility
/// <param name="uri">The relative path of the request</param>
/// <param name="httpMethod">Request http method</param>
/// <param name="requestData">An object to fetch parameters from</param>
/// <param name="urlSegmentValues">List of values to replace route arguments</param>
/// <returns>RestSharp request after adding the parameters</returns>
public static RestRequest CreateRestRequestAndAddParameters(string uri, Method httpMethod, object requestData)
public static RestRequest CreateRestRequestAndAddParameters(string uri, Method httpMethod, object requestData,
params object?[] urlSegmentValues)

Check warning on line 21 in src/Garnet.Detail.ExternalService.Rest/Utilities/RequestUtility.cs

View workflow job for this annotation

GitHub Actions / pack_push

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
var request = CreateRestRequest(uri, httpMethod);

AddUrlSegmentParameters(request, urlSegmentValues);

AddParametersToTheRequest(request, requestData);

return request;
}

/// <summary>
/// Add url segment to the <paramref name="request"/> to fill route values
/// </summary>
/// <param name="request">RestSharp request to add parameters to</param>
/// <param name="urlSegmentValues">List of values to replace route arguments</param>
/// <exception cref="ArgumentNullException">When <paramref name="urlSegmentValues"/> is null</exception>
public static void AddUrlSegmentParameters(RestRequest request, params object?[] urlSegmentValues)

Check warning on line 38 in src/Garnet.Detail.ExternalService.Rest/Utilities/RequestUtility.cs

View workflow job for this annotation

GitHub Actions / pack_push

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
if (urlSegmentValues is null || urlSegmentValues.Length <= 0)
{
return;
}

var matches = new Regex(@"\{(.*?)\}").Matches(request.Resource);

for (var i = 0; i < matches.Count; i++)
{
var name = matches[i].Groups[1].Value;

if (i >= urlSegmentValues.Length)
{
break;
}

var value = urlSegmentValues[i]?.ToString();
if (value is null)
{
throw new ArgumentNullException(nameof(urlSegmentValues),
$"Value in {nameof(urlSegmentValues)} cannot be null");
}

request.AddUrlSegment(name, value);
}
}

/// <summary>
/// Creates a new instance of a RestSharp request
/// </summary>
Expand Down Expand Up @@ -55,8 +96,15 @@ public static void AddParametersToTheRequest(RestRequest request, object request
{
continue;
}

request.AddParameter(prop.Name, value);

if (request.Resource.Contains(prop.Name))
{
request.AddUrlSegment(prop.Name, value);
}
else
{
request.AddParameter(prop.Name, value);
}
}
}
else
Expand Down

0 comments on commit 8e33bfd

Please sign in to comment.