From c8327062813cffdf736c3334d7054da589795f2e Mon Sep 17 00:00:00 2001 From: ElbekDeveloper Date: Fri, 29 Mar 2024 21:28:18 +0300 Subject: [PATCH 1/3] 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 From 6bf966b76476a0a194bc1826a826dd572493814b Mon Sep 17 00:00:00 2001 From: ElbekDeveloper Date: Sun, 31 Mar 2024 08:53:43 +0300 Subject: [PATCH 2/3] CODE RUB: Resolve Comments --- RESTFulSense/Clients/RESTFulApiClient.cs | 9 ++++++--- RESTFulSense/Clients/RESTFulApiFactoryClient.cs | 17 ++++++----------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/RESTFulSense/Clients/RESTFulApiClient.cs b/RESTFulSense/Clients/RESTFulApiClient.cs index 2bfd295..0554251 100644 --- a/RESTFulSense/Clients/RESTFulApiClient.cs +++ b/RESTFulSense/Clients/RESTFulApiClient.cs @@ -67,9 +67,12 @@ public async ValueTask GetContentByteArrayAsync(string relativeUrl) HttpResponseMessage responseMessage = await GetAsync(relativeUrl); - return responseMessage.IsSuccessStatusCode - ? await responseMessage.Content.ReadAsByteArrayAsync() - : await ValidateAndReturnInvalidResponse(responseMessage); + if (responseMessage.IsSuccessStatusCode is false) + { + await ValidationService.ValidateHttpResponseAsync(responseMessage); + } + + return await responseMessage.Content.ReadAsByteArrayAsync(); } public async ValueTask PostContentWithNoResponseAsync( diff --git a/RESTFulSense/Clients/RESTFulApiFactoryClient.cs b/RESTFulSense/Clients/RESTFulApiFactoryClient.cs index 9063b13..ec90780 100644 --- a/RESTFulSense/Clients/RESTFulApiFactoryClient.cs +++ b/RESTFulSense/Clients/RESTFulApiFactoryClient.cs @@ -71,9 +71,12 @@ public async ValueTask GetContentByteArrayAsync(string relativeUrl) HttpResponseMessage responseMessage = await this.httpClient.GetAsync(relativeUrl); - return responseMessage.IsSuccessStatusCode - ? await responseMessage.Content.ReadAsByteArrayAsync() - : await ValidateAndReturnInvalidResponse(responseMessage); + if (responseMessage.IsSuccessStatusCode is false) + { + await ValidationService.ValidateHttpResponseAsync(responseMessage); + } + + return await responseMessage.Content.ReadAsByteArrayAsync(); } public async ValueTask PostContentWithNoResponseAsync( @@ -478,13 +481,5 @@ 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 From a138dfede4b0f88b18c6c361f60660f7b1bd3ad9 Mon Sep 17 00:00:00 2001 From: ElbekDeveloper Date: Sun, 31 Mar 2024 12:09:53 +0300 Subject: [PATCH 3/3] CODE RUB: Remove Unused Method --- RESTFulSense/Clients/RESTFulApiClient.cs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/RESTFulSense/Clients/RESTFulApiClient.cs b/RESTFulSense/Clients/RESTFulApiClient.cs index 0554251..afdedf0 100644 --- a/RESTFulSense/Clients/RESTFulApiClient.cs +++ b/RESTFulSense/Clients/RESTFulApiClient.cs @@ -442,13 +442,5 @@ 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