/// <summary> /// Sends an asynchronous multipart/form-data POST request. /// </summary> /// <param name="buildContent">A delegate for building the content parts.</param> /// <param name="client">The Flurl client.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns>A Task whose result is the received HttpResponseMessage.</returns> public static Task <HttpResponseMessage> PostMultipartAsync(this FlurlClient client, Action <CapturedMultipartContent> buildContent, CancellationToken cancellationToken = default(CancellationToken)) { var cmc = new CapturedMultipartContent(client.Settings); buildContent(cmc); return(client.SendAsync(HttpMethod.Post, cmc, cancellationToken)); }
/// <summary> /// Asynchronously downloads a file at the specified URL. /// </summary> /// <param name="client">The flurl client.</param> /// <param name="localFolderPath">Path of local folder where file is to be downloaded.</param> /// <param name="localFileName">Name of local file. If not specified, the source filename (last segment of the URL) is used.</param> /// <param name="bufferSize">Buffer size in bytes. Default is 4096.</param> /// <returns>A Task whose result is the local path of the downloaded file.</returns> public static async Task <string> DownloadFileAsync(this FlurlClient client, string localFolderPath, string localFileName = null, int bufferSize = 4096) { if (localFileName == null) { localFileName = client.Url.Path.Split('/').Last(); } // need to temporarily disable autodispose if set, otherwise reading from stream will fail var autoDispose = client.AutoDispose; client.AutoDispose = false; try { var response = await client.SendAsync(HttpMethod.Get, completionOption : HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false); // http://codereview.stackexchange.com/a/18679 using (var httpStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) using (var filestream = await FileUtil.OpenWriteAsync(localFolderPath, localFileName, bufferSize).ConfigureAwait(false)) { await httpStream.CopyToAsync(filestream, bufferSize).ConfigureAwait(false); } return(FileUtil.CombinePath(localFolderPath, localFileName)); } finally { client.AutoDispose = autoDispose; if (client.AutoDispose) { client.Dispose(); } } }
/// <summary> /// Sends an asynchronous GET request. /// </summary> /// <returns>A Task whose result is the JSON response body deserialized to a list of dynamics.</returns> public static Task <IList <dynamic> > GetJsonListAsync(this FlurlClient client) { return(client.SendAsync(HttpMethod.Get).ReceiveJilList()); }
/// <summary> /// Sends an asynchronous GET request. /// </summary> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns>A Task whose result is the JSON response body deserialized to a list of dynamics.</returns> public static Task <IList <dynamic> > GetJilJsonListAsync(this FlurlClient client, CancellationToken cancellationToken) { return(client.SendAsync(HttpMethod.Get, cancellationToken: cancellationToken).ReceiveJilList()); }
/// <summary> /// Sends an asynchronous GET request. /// </summary> /// <returns>A Task whose result is the JSON response body deserialized to a dynamic.</returns> public static Task <dynamic> GetJilJsonAsync(this FlurlClient client) { return(client.SendAsync(HttpMethod.Get).ReceiveJil()); }
/// <summary> /// Sends an asynchronous PATCH request. /// </summary> /// <param name="data">Contents of the request body.</param> /// <returns>A Task whose result is the received HttpResponseMessage.</returns> public static Task <HttpResponseMessage> PatchJilJsonAsync(this FlurlClient client, object data) { return(client.SendAsync(new HttpMethod("PATCH"), content: new CapturedJilJsonContent(data))); }
/// <summary> /// Sends an asynchronous PUT request. /// </summary> /// <param name="data">Contents of the request body.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns>A Task whose result is the received HttpResponseMessage.</returns> public static Task <HttpResponseMessage> PutJilJsonAsync(this FlurlClient client, object data, CancellationToken cancellationToken) { return(client.SendAsync(HttpMethod.Put, content: new CapturedJilJsonContent(data), cancellationToken: cancellationToken)); }
/// <summary> /// Sends an asynchronous POST request. /// </summary> /// <param name="data">Contents of the request body.</param> /// <returns>A Task whose result is the received HttpResponseMessage.</returns> public static Task <HttpResponseMessage> PostJilJsonAsync(this FlurlClient client, object data) { return(client.SendAsync(HttpMethod.Post, content: new CapturedJilJsonContent(data))); }
/// <summary> /// Sends an asynchronous GET request. /// </summary> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns>A Task whose result is the JSON response body deserialized to an object of type T.</returns> public static Task <T> GetJilJsonAsync <T>(this FlurlClient client, CancellationToken cancellationToken) { return(client.SendAsync(HttpMethod.Get, cancellationToken: cancellationToken).ReceiveJil <T>()); }