Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLIENTS: Get Byte Array #168

Merged
merged 3 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ public interface IRESTFulApiClient

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 @@ public interface IRESTFulApiFactoryClient
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 RESTFulApiClient()
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 @@ public async ValueTask DeleteContentAsync(string relativeUrl)
? JsonConvert.DeserializeObject<T>(responseString)
: await deserializationFunction(responseString);
}

private async static ValueTask<byte[]> ValidateAndReturnInvalidResponse(
hassanhabib marked this conversation as resolved.
Show resolved Hide resolved
HttpResponseMessage httpResponseMessage)
{
await ValidationService.ValidateHttpResponseAsync(httpResponseMessage);

return null;
hassanhabib marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
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 RESTFulApiFactoryClient(HttpClient httpClient)
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 @@ public async ValueTask DeleteContentAsync(string relativeUrl)
? JsonConvert.DeserializeObject<T>(responseString)
: await deserializationFunction(responseString);
}

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

return null;
}
}
}