Skip to content

Commit

Permalink
feat: Add CachedApiResponse and modify GET /Tags endpoint in SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
MH321Productions committed Feb 27, 2025
1 parent 58e2606 commit fdc6066
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class EndpointClient
}
""";

private const string EMPTY_CACHED_RESULT = "{}";

private const string EMPTY_VALUE = """
{
"value": {}
Expand Down Expand Up @@ -67,6 +69,18 @@ public async Task<ApiResponse<T>> GetUnauthenticated<T>(string url, NameValueCol
.Execute();
}

public async Task<CachedApiResponse<T>> GetCachedUnauthenticated<T>(string url, NameValueCollection? queryParameters = null, PaginationFilter? pagination = null, CacheControl? cacheControl = null)
{
var builder = Request<T>(HttpMethod.Get, url)
.WithPagination(pagination)
.AddQueryParameters(queryParameters);

if (cacheControl != null)
builder.AddExtraHeader("If-None-Match", cacheControl.ETag);

return await builder.ExecuteCached();
}

public async Task<ApiResponse<T>> Put<T>(string url, object? requestContent = null)
{
return await Request<T>(HttpMethod.Put, url)
Expand Down Expand Up @@ -117,6 +131,28 @@ private async Task<ApiResponse<T>> Execute<T>(HttpRequestMessage request)
return deserializedResponseContent;
}

private async Task<CachedApiResponse<T>> ExecuteCached<T>(HttpRequestMessage request)
{
var response = await _httpClient.SendAsync(request);
var responseContent = await response.Content.ReadAsStreamAsync();
var statusCode = response.StatusCode;

if (statusCode == HttpStatusCode.NotModified || responseContent.Length == 0)
{
responseContent.Close();
responseContent = new MemoryStream(Encoding.UTF8.GetBytes(EMPTY_CACHED_RESULT));
}

var deserializedResponseContent = JsonSerializer.Deserialize<CachedApiResponse<T>>(responseContent, _jsonSerializerOptions);

deserializedResponseContent!.Status = statusCode;
deserializedResponseContent.RawContent = await response.Content.ReadAsStringAsync();
deserializedResponseContent.ContentType = response.Content.Headers.ContentType?.MediaType;
deserializedResponseContent.ETag = response.Headers.ETag!.Tag; //TODO: Timo (Is it safe to assume that the ETag is non-null?)

return deserializedResponseContent;
}

private async Task<ApiResponse<T>> ExecuteOData<T>(HttpRequestMessage request)
{
var response = await _httpClient.SendAsync(request);
Expand Down Expand Up @@ -279,6 +315,11 @@ public async Task<RawApiResponse> ExecuteRaw()
return await _client.ExecuteRaw(await CreateRequestMessage());
}

public async Task<CachedApiResponse<T>> ExecuteCached()
{
return await _client.ExecuteCached<T>(await CreateRequestMessage());
}

private async Task<HttpRequestMessage> CreateRequestMessage()
{
var request = new HttpRequestMessage(_method, EncodeParametersInUrl())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Backbone.BuildingBlocks.SDK.Endpoints.Common.Types;

public class CacheControl
{
public required string ETag { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Backbone.BuildingBlocks.SDK.Endpoints.Common.Types;

public class CachedApiResponse<TResult> : ApiResponse<TResult>
{
public bool NotModified => Result == null;

public string ETag { get; set; } = string.Empty;
}
4 changes: 2 additions & 2 deletions Sdks/ConsumerApi.Sdk/src/Endpoints/Tags/TagsEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Backbone.ConsumerApi.Sdk.Endpoints.Tags;

public class TagsEndpoint(EndpointClient client) : ConsumerApiEndpoint(client)
{
public async Task<ApiResponse<ListTagsResponse>> ListTags()
public async Task<CachedApiResponse<ListTagsResponse>> ListTags(CacheControl? cacheControl = null)
{
return await _client.GetUnauthenticated<ListTagsResponse>($"api/{API_VERSION}/Tags");
return await _client.GetCachedUnauthenticated<ListTagsResponse>($"api/{API_VERSION}/Tags", null, null, cacheControl);
}
}

0 comments on commit fdc6066

Please sign in to comment.