11using Microsoft . AspNetCore . Http ;
2+ using Microsoft . AspNetCore . Mvc ;
23using Microsoft . Extensions . Logging ;
3- using System ;
4- using System . Collections . Generic ;
5- using System . Linq ;
64using System . Net ;
75using System . Net . Http . Headers ;
86using System . Text ;
97using System . Text . Json ;
10- using System . Threading . Tasks ;
118
129namespace MZBase . Microservices . HttpServices
1310{
@@ -542,7 +539,7 @@ protected async Task ProcessNotSuccessfulResponse(HttpResponseMessage? response,
542539 , methodName
543540 , _serviceUniqueName
544541 , _httpClientBaseAddress + apiUrl ) ;
545- throw new Exception ( "Service address not found: '" + _httpClientBaseAddress + apiUrl + "', Reason Phrase:" + response . ReasonPhrase ) ;
542+ throw new Exception ( "Service address not found: '" + _httpClientBaseAddress + apiUrl + "', Reason Phrase:" + response . ReasonPhrase ) ;
546543 }
547544
548545 string ? responsContent = null ;
@@ -562,7 +559,7 @@ protected async Task ProcessNotSuccessfulResponse(HttpResponseMessage? response,
562559 , _serviceUniqueName
563560 , _httpClientBaseAddress + apiUrl ) ;
564561
565- throw new Exception ( "Failed to do post method:'" + _httpClientBaseAddress + apiUrl + "', Reason Phrase:" + response . ReasonPhrase ) ;
562+ throw new Exception ( "Failed to do post method:'" + _httpClientBaseAddress + apiUrl + "', Reason Phrase:" + response . ReasonPhrase ) ;
566563 }
567564 else
568565 {
@@ -576,7 +573,7 @@ protected async Task ProcessNotSuccessfulResponse(HttpResponseMessage? response,
576573 , _serviceUniqueName
577574 , _httpClientBaseAddress + apiUrl ) ;
578575
579- throw new Exception ( "Failed calling remote procedure:'" + _httpClientBaseAddress + apiUrl + "', Reason Phrase:" + response . ReasonPhrase + "," + responsContent ) ;
576+ throw new Exception ( "Failed calling remote procedure:'" + _httpClientBaseAddress + apiUrl + "', Reason Phrase:" + response . ReasonPhrase + "," + responsContent ) ;
580577 }
581578 }
582579
@@ -619,5 +616,70 @@ protected async Task ProcessNotSuccessfulResponse(HttpResponseMessage? response,
619616
620617
621618 }
619+ public async ValueTask < FileContentResult > GetFileContentResultAsync ( string address )
620+ {
621+ _logger . LogInformation ( "Called method '{ServiceMethod}' from service '{Category}' for remote address '{RemoteAddress}'"
622+ , "GetAsync"
623+ , _serviceUniqueName
624+ , _httpClientBaseAddress + address ) ;
625+
626+
627+ using var httpResponseMessage = await _httpClient . GetAsync ( address ) ;
628+
629+
630+ if ( ! httpResponseMessage . IsSuccessStatusCode )
631+ {
632+ await ProcessNotSuccessfulResponse ( httpResponseMessage , address , "GetAsync" ) ;
633+ }
634+
635+ if ( httpResponseMessage . StatusCode == HttpStatusCode . NoContent )
636+ {
637+
638+ return default ;
639+ }
640+
641+ _logger . LogInformation ( "Successfully called remote procedure: Method '{ServiceMethod}' from service '{Category}' for remote address '{RemoteAddress}'"
642+ , "GetAsync"
643+ , _serviceUniqueName
644+ , _httpClientBaseAddress + address ) ;
645+
646+ return new FileContentResult ( await httpResponseMessage . Content . ReadAsByteArrayAsync ( ) , httpResponseMessage . Content . Headers . ContentType . MediaType ) ;
647+
648+
649+ }
650+ public async ValueTask < FileStreamResult > GetFileStreamAsync ( string address )
651+ {
652+ _logger . LogInformation ( "Called method '{ServiceMethod}' from service '{Category}' for remote address '{RemoteAddress}'"
653+ , "GetAsync"
654+ , _serviceUniqueName
655+ , _httpClientBaseAddress + address ) ;
656+
657+
658+ using var httpResponseMessage = await _httpClient . GetAsync ( address ) ;
659+
660+
661+ if ( ! httpResponseMessage . IsSuccessStatusCode )
662+ {
663+ await ProcessNotSuccessfulResponse ( httpResponseMessage , address , "GetAsync" ) ;
664+ }
665+
666+ if ( httpResponseMessage . StatusCode == HttpStatusCode . NoContent )
667+ {
668+ return default ;
669+ }
670+
671+ _logger . LogInformation ( "Successfully called remote procedure: Method '{ServiceMethod}' from service '{Category}' for remote address '{RemoteAddress}'"
672+ , "GetAsync"
673+ , _serviceUniqueName
674+ , _httpClientBaseAddress + address ) ;
675+
676+ // Fix CS1503: Use stream, not byte[]
677+ var stream = await httpResponseMessage . Content . ReadAsStreamAsync ( ) . ConfigureAwait ( false ) ;
678+
679+ // Fix CS8602: Check for null before dereferencing
680+ var contentType = httpResponseMessage . Content . Headers . ContentType ? . MediaType ?? "application/octet-stream" ;
681+
682+ return new FileStreamResult ( stream , contentType ) ;
683+ }
622684 }
623685}
0 commit comments