From c8327062813cffdf736c3334d7054da589795f2e Mon Sep 17 00:00:00 2001 From: ElbekDeveloper Date: Fri, 29 Mar 2024 21:28:18 +0300 Subject: [PATCH] CLIENTS: Get Byte Array --- RESTFulSense/Clients/IRESTFulApiClient.cs | 2 ++ .../Clients/IRESTFulApiFactoryClient.cs | 1 + RESTFulSense/Clients/RESTFulApiClient.cs | 18 ++++++++++++++++++ .../Clients/RESTFulApiFactoryClient.cs | 18 ++++++++++++++++++ 4 files changed, 39 insertions(+) diff --git a/RESTFulSense/Clients/IRESTFulApiClient.cs b/RESTFulSense/Clients/IRESTFulApiClient.cs index 0aba2a9..6080b7d 100644 --- a/RESTFulSense/Clients/IRESTFulApiClient.cs +++ b/RESTFulSense/Clients/IRESTFulApiClient.cs @@ -27,6 +27,8 @@ ValueTask GetContentAsync( ValueTask GetContentStreamAsync(string relativeUrl); + ValueTask GetContentByteArrayAsync(string relativeUrl); + ValueTask PostContentWithNoResponseAsync( string relativeUrl, T content, diff --git a/RESTFulSense/Clients/IRESTFulApiFactoryClient.cs b/RESTFulSense/Clients/IRESTFulApiFactoryClient.cs index 6769709..6c10ba6 100644 --- a/RESTFulSense/Clients/IRESTFulApiFactoryClient.cs +++ b/RESTFulSense/Clients/IRESTFulApiFactoryClient.cs @@ -25,6 +25,7 @@ ValueTask GetContentAsync( ValueTask GetContentStringAsync(string relativeUrl); ValueTask GetContentStreamAsync(string relativeUrl); + ValueTask GetContentByteArrayAsync(string relativeUrl); ValueTask PostContentWithNoResponseAsync( string relativeUrl, diff --git a/RESTFulSense/Clients/RESTFulApiClient.cs b/RESTFulSense/Clients/RESTFulApiClient.cs index fedb0de..2bfd295 100644 --- a/RESTFulSense/Clients/RESTFulApiClient.cs +++ b/RESTFulSense/Clients/RESTFulApiClient.cs @@ -62,6 +62,16 @@ public async ValueTask GetContentStringAsync(string relativeUrl) => public async ValueTask GetContentStreamAsync(string relativeUrl) => await GetStreamAsync(relativeUrl); + public async ValueTask GetContentByteArrayAsync(string relativeUrl) + { + HttpResponseMessage responseMessage = + await GetAsync(relativeUrl); + + return responseMessage.IsSuccessStatusCode + ? await responseMessage.Content.ReadAsByteArrayAsync() + : await ValidateAndReturnInvalidResponse(responseMessage); + } + public async ValueTask PostContentWithNoResponseAsync( string relativeUrl, T content, @@ -429,5 +439,13 @@ private static async ValueTask DeserializeResponseContent( ? JsonConvert.DeserializeObject(responseString) : await deserializationFunction(responseString); } + + private async static ValueTask ValidateAndReturnInvalidResponse( + HttpResponseMessage httpResponseMessage) + { + await ValidationService.ValidateHttpResponseAsync(httpResponseMessage); + + return null; + } } } \ No newline at end of file diff --git a/RESTFulSense/Clients/RESTFulApiFactoryClient.cs b/RESTFulSense/Clients/RESTFulApiFactoryClient.cs index c5b8b5b..9063b13 100644 --- a/RESTFulSense/Clients/RESTFulApiFactoryClient.cs +++ b/RESTFulSense/Clients/RESTFulApiFactoryClient.cs @@ -66,6 +66,16 @@ public async ValueTask GetContentStringAsync(string relativeUrl) => public async ValueTask GetContentStreamAsync(string relativeUrl) => await this.httpClient.GetStreamAsync(relativeUrl); + public async ValueTask GetContentByteArrayAsync(string relativeUrl) + { + HttpResponseMessage responseMessage = + await this.httpClient.GetAsync(relativeUrl); + + return responseMessage.IsSuccessStatusCode + ? await responseMessage.Content.ReadAsByteArrayAsync() + : await ValidateAndReturnInvalidResponse(responseMessage); + } + public async ValueTask PostContentWithNoResponseAsync( string relativeUrl, T content, @@ -468,5 +478,13 @@ private static async ValueTask DeserializeResponseContent( ? JsonConvert.DeserializeObject(responseString) : await deserializationFunction(responseString); } + + private async static ValueTask ValidateAndReturnInvalidResponse( + HttpResponseMessage httpResponseMessage) + { + await ValidationService.ValidateHttpResponseAsync(httpResponseMessage); + + return null; + } } } \ No newline at end of file