Skip to content

Commit

Permalink
CLIENTS: Get Byte Array
Browse files Browse the repository at this point in the history
  • Loading branch information
ElbekDeveloper committed Mar 29, 2024
1 parent fc69505 commit c832706
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions RESTFulSense/Clients/IRESTFulApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ ValueTask<T> GetContentAsync<T>(

ValueTask<Stream> GetContentStreamAsync(string relativeUrl);

ValueTask<byte[]> GetContentByteArrayAsync(string relativeUrl);

ValueTask PostContentWithNoResponseAsync<T>(
string relativeUrl,
T content,
Expand Down
1 change: 1 addition & 0 deletions RESTFulSense/Clients/IRESTFulApiFactoryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ ValueTask<T> GetContentAsync<T>(
ValueTask<string> GetContentStringAsync(string relativeUrl);

ValueTask<Stream> GetContentStreamAsync(string relativeUrl);
ValueTask<byte[]> GetContentByteArrayAsync(string relativeUrl);

ValueTask PostContentWithNoResponseAsync<T>(
string relativeUrl,
Expand Down
18 changes: 18 additions & 0 deletions RESTFulSense/Clients/RESTFulApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ public async ValueTask<string> GetContentStringAsync(string relativeUrl) =>
public async ValueTask<Stream> GetContentStreamAsync(string relativeUrl) =>
await GetStreamAsync(relativeUrl);

public async ValueTask<byte[]> GetContentByteArrayAsync(string relativeUrl)
{
HttpResponseMessage responseMessage =
await GetAsync(relativeUrl);

return responseMessage.IsSuccessStatusCode
? await responseMessage.Content.ReadAsByteArrayAsync()
: await ValidateAndReturnInvalidResponse(responseMessage);
}

public async ValueTask PostContentWithNoResponseAsync<T>(
string relativeUrl,
T content,
Expand Down Expand Up @@ -429,5 +439,13 @@ private static async ValueTask<T> DeserializeResponseContent<T>(
? JsonConvert.DeserializeObject<T>(responseString)
: await deserializationFunction(responseString);
}

private async static ValueTask<byte[]> ValidateAndReturnInvalidResponse(
HttpResponseMessage httpResponseMessage)
{
await ValidationService.ValidateHttpResponseAsync(httpResponseMessage);

return null;
}
}
}
18 changes: 18 additions & 0 deletions RESTFulSense/Clients/RESTFulApiFactoryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ public async ValueTask<string> GetContentStringAsync(string relativeUrl) =>
public async ValueTask<Stream> GetContentStreamAsync(string relativeUrl) =>
await this.httpClient.GetStreamAsync(relativeUrl);

public async ValueTask<byte[]> GetContentByteArrayAsync(string relativeUrl)
{
HttpResponseMessage responseMessage =
await this.httpClient.GetAsync(relativeUrl);

return responseMessage.IsSuccessStatusCode
? await responseMessage.Content.ReadAsByteArrayAsync()
: await ValidateAndReturnInvalidResponse(responseMessage);
}

public async ValueTask PostContentWithNoResponseAsync<T>(
string relativeUrl,
T content,
Expand Down Expand Up @@ -468,5 +478,13 @@ private static async ValueTask<T> DeserializeResponseContent<T>(
? JsonConvert.DeserializeObject<T>(responseString)
: await deserializationFunction(responseString);
}

private async static ValueTask<byte[]> ValidateAndReturnInvalidResponse(
HttpResponseMessage httpResponseMessage)
{
await ValidationService.ValidateHttpResponseAsync(httpResponseMessage);

return null;
}
}
}

0 comments on commit c832706

Please sign in to comment.