@@ -68,7 +68,7 @@ public static void UpdateProxy(ProxyProperty property)
6868 var userName when string . IsNullOrEmpty ( userName ) =>
6969 ( new Uri ( $ "http://{ Proxy . Server } :{ Proxy . Port } ") , null ) ,
7070 _ => ( new Uri ( $ "http://{ Proxy . Server } :{ Proxy . Port } ") ,
71- new NetworkCredential ( Proxy . UserName , Proxy . Password ) )
71+ new NetworkCredential ( Proxy . UserName , Proxy . Password ) )
7272 } ,
7373 _ => ( null , null )
7474 } ,
@@ -79,7 +79,7 @@ var userName when string.IsNullOrEmpty(userName) =>
7979 _ => throw new ArgumentOutOfRangeException ( )
8080 } ;
8181 }
82- catch ( UriFormatException e )
82+ catch ( UriFormatException e )
8383 {
8484 API . ShowMsg ( "Please try again" , "Unable to parse Http Proxy" ) ;
8585 Log . Exception ( "Flow.Launcher.Infrastructure.Http" , "Unable to parse Uri" , e ) ;
@@ -94,7 +94,7 @@ public static async Task DownloadAsync([NotNull] string url, [NotNull] string fi
9494 if ( response . StatusCode == HttpStatusCode . OK )
9595 {
9696 await using var fileStream = new FileStream ( filePath , FileMode . CreateNew ) ;
97- await response . Content . CopyToAsync ( fileStream ) ;
97+ await response . Content . CopyToAsync ( fileStream , token ) ;
9898 }
9999 else
100100 {
@@ -117,7 +117,7 @@ public static async Task DownloadAsync([NotNull] string url, [NotNull] string fi
117117 public static Task < string > GetAsync ( [ NotNull ] string url , CancellationToken token = default )
118118 {
119119 Log . Debug ( $ "|Http.Get|Url <{ url } >") ;
120- return GetAsync ( new Uri ( url . Replace ( "#" , "%23" ) ) , token ) ;
120+ return GetAsync ( new Uri ( url ) , token ) ;
121121 }
122122
123123 /// <summary>
@@ -130,36 +130,57 @@ public static async Task<string> GetAsync([NotNull] Uri url, CancellationToken t
130130 {
131131 Log . Debug ( $ "|Http.Get|Url <{ url } >") ;
132132 using var response = await client . GetAsync ( url , token ) ;
133- var content = await response . Content . ReadAsStringAsync ( ) ;
134- if ( response . StatusCode == HttpStatusCode . OK )
135- {
136- return content ;
137- }
138- else
133+ var content = await response . Content . ReadAsStringAsync ( token ) ;
134+ if ( response . StatusCode != HttpStatusCode . OK )
139135 {
140136 throw new HttpRequestException (
141137 $ "Error code <{ response . StatusCode } > with content <{ content } > returned from <{ url } >") ;
142138 }
139+
140+ return content ;
143141 }
144142
145143 /// <summary>
146- /// Asynchrously get the result as stream from url.
144+ /// Send a GET request to the specified Uri with an HTTP completion option and a cancellation token as an asynchronous operation.
145+ /// </summary>
146+ /// <param name="url">The Uri the request is sent to.</param>
147+ /// <param name="completionOption">An HTTP completion option value that indicates when the operation should be considered completed.</param>
148+ /// <param name="token">A cancellation token that can be used by other objects or threads to receive notice of cancellation</param>
149+ /// <returns></returns>
150+ public static Task < Stream > GetStreamAsync ( [ NotNull ] string url ,
151+ CancellationToken token = default ) => GetStreamAsync ( new Uri ( url ) , token ) ;
152+
153+
154+ /// <summary>
155+ /// Send a GET request to the specified Uri with an HTTP completion option and a cancellation token as an asynchronous operation.
147156 /// </summary>
148157 /// <param name="url"></param>
158+ /// <param name="token"></param>
149159 /// <returns></returns>
150- public static async Task < Stream > GetStreamAsync ( [ NotNull ] string url , CancellationToken token = default )
160+ public static async Task < Stream > GetStreamAsync ( [ NotNull ] Uri url ,
161+ CancellationToken token = default )
162+ {
163+ Log . Debug ( $ "|Http.Get|Url <{ url } >") ;
164+ return await client . GetStreamAsync ( url , token ) ;
165+ }
166+
167+ public static async Task < HttpResponseMessage > GetResponseAsync ( string url , HttpCompletionOption completionOption = HttpCompletionOption . ResponseContentRead ,
168+ CancellationToken token = default )
169+ => await GetResponseAsync ( new Uri ( url ) , completionOption , token ) ;
170+
171+ public static async Task < HttpResponseMessage > GetResponseAsync ( [ NotNull ] Uri url , HttpCompletionOption completionOption = HttpCompletionOption . ResponseContentRead ,
172+ CancellationToken token = default )
151173 {
152174 Log . Debug ( $ "|Http.Get|Url <{ url } >") ;
153- var response = await client . GetAsync ( url , HttpCompletionOption . ResponseHeadersRead , token ) ;
154- return await response . Content . ReadAsStreamAsync ( ) ;
175+ return await client . GetAsync ( url , completionOption , token ) ;
155176 }
156177
157178 /// <summary>
158179 /// Asynchrously send an HTTP request.
159180 /// </summary>
160- public static async Task < HttpResponseMessage > SendAsync ( HttpRequestMessage request , CancellationToken token = default )
181+ public static async Task < HttpResponseMessage > SendAsync ( HttpRequestMessage request , HttpCompletionOption completionOption = HttpCompletionOption . ResponseContentRead , CancellationToken token = default )
161182 {
162- return await client . SendAsync ( request , HttpCompletionOption . ResponseHeadersRead , token ) ;
183+ return await client . SendAsync ( request , completionOption , token ) ;
163184 }
164185 }
165186}
0 commit comments