-
-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bring back support for dynamics via Flurl.Http.Newtonsoft
- Loading branch information
Todd
committed
Dec 5, 2023
1 parent
2599242
commit 3ef55a3
Showing
6 changed files
with
242 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Dynamic; | ||
using System.Threading.Tasks; | ||
|
||
namespace Flurl.Http.Newtonsoft | ||
{ | ||
/// <summary> | ||
/// Extensions to Flurl objects that use Newtonsoft.Json. | ||
/// </summary> | ||
public static class ExtensionMethods | ||
{ | ||
/// <summary> | ||
/// Deserializes JSON-formatted HTTP response body to a dynamic object. | ||
/// </summary> | ||
/// <returns>A Task whose result is a dynamic object containing data in the response body.</returns> | ||
public static async Task<dynamic> GetJsonAsync(this IFlurlResponse resp) { | ||
dynamic d = await resp.GetJsonAsync<ExpandoObject>().ConfigureAwait(false); | ||
return d; | ||
} | ||
|
||
/// <summary> | ||
/// Deserializes JSON-formatted HTTP response body to a list of dynamic objects. | ||
/// </summary> | ||
/// <returns>A Task whose result is a list of dynamic objects containing data in the response body.</returns> | ||
public static async Task<IList<dynamic>> GetJsonListAsync(this IFlurlResponse resp) { | ||
dynamic[] d = await resp.GetJsonAsync<ExpandoObject[]>().ConfigureAwait(false); | ||
return d; | ||
} | ||
|
||
/// <summary> | ||
/// Deserializes JSON-formatted HTTP response body to a dynamic object. Intended to chain off an async call. | ||
/// </summary> | ||
/// <returns>A Task whose result is a dynamic object containing data in the response body.</returns> | ||
public static async Task<dynamic> ReceiveJson(this Task<IFlurlResponse> response) { | ||
using var resp = await response.ConfigureAwait(false); | ||
if (resp == null) return null; | ||
return await resp.GetJsonAsync().ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// Deserializes JSON-formatted HTTP response body to a list of dynamic objects. Intended to chain off an async call. | ||
/// </summary> | ||
/// <returns>A Task whose result is a list of dynamic objects containing data in the response body.</returns> | ||
public static async Task<IList<dynamic>> ReceiveJsonList(this Task<IFlurlResponse> response) { | ||
using var resp = await response.ConfigureAwait(false); | ||
if (resp == null) return null; | ||
return await resp.GetJsonListAsync().ConfigureAwait(false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// This file was auto-generated by Flurl.CodeGen. Do not edit directly. | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Flurl.Http.Newtonsoft | ||
{ | ||
/// <summary> | ||
/// Fluent extension methods on String, Url, Uri, and IFlurlRequest. | ||
/// </summary> | ||
public static class GeneratedExtensions | ||
{ | ||
/// <summary> | ||
/// Sends an asynchronous GET request. | ||
/// </summary> | ||
/// <param name="request">This IFlurlRequest</param> | ||
/// <param name="completionOption">The HttpCompletionOption used in the request. Optional.</param> | ||
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param> | ||
/// <returns>A Task whose result is the JSON response body deserialized to a dynamic.</returns> | ||
public static Task<dynamic> GetJsonAsync(this IFlurlRequest request, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, CancellationToken cancellationToken = default) { | ||
return request.SendAsync(HttpMethod.Get, null, completionOption, cancellationToken).ReceiveJson(); | ||
} | ||
|
||
/// <summary> | ||
/// Sends an asynchronous GET request. | ||
/// </summary> | ||
/// <param name="request">This IFlurlRequest</param> | ||
/// <param name="completionOption">The HttpCompletionOption used in the request. Optional.</param> | ||
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param> | ||
/// <returns>A Task whose result is the JSON response body deserialized to a list of dynamics.</returns> | ||
public static Task<IList<dynamic>> GetJsonListAsync(this IFlurlRequest request, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, CancellationToken cancellationToken = default) { | ||
return request.SendAsync(HttpMethod.Get, null, completionOption, cancellationToken).ReceiveJsonList(); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a FlurlRequest and sends an asynchronous GET request. | ||
/// </summary> | ||
/// <param name="url">This Flurl.Url.</param> | ||
/// <param name="completionOption">The HttpCompletionOption used in the request. Optional.</param> | ||
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param> | ||
/// <returns>A Task whose result is the JSON response body deserialized to a dynamic.</returns> | ||
public static Task<dynamic> GetJsonAsync(this Url url, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, CancellationToken cancellationToken = default) { | ||
return new FlurlRequest(url).GetJsonAsync(completionOption, cancellationToken); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a FlurlRequest and sends an asynchronous GET request. | ||
/// </summary> | ||
/// <param name="url">This Flurl.Url.</param> | ||
/// <param name="completionOption">The HttpCompletionOption used in the request. Optional.</param> | ||
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param> | ||
/// <returns>A Task whose result is the JSON response body deserialized to a list of dynamics.</returns> | ||
public static Task<IList<dynamic>> GetJsonListAsync(this Url url, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, CancellationToken cancellationToken = default) { | ||
return new FlurlRequest(url).GetJsonListAsync(completionOption, cancellationToken); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a FlurlRequest and sends an asynchronous GET request. | ||
/// </summary> | ||
/// <param name="url">This URL.</param> | ||
/// <param name="completionOption">The HttpCompletionOption used in the request. Optional.</param> | ||
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param> | ||
/// <returns>A Task whose result is the JSON response body deserialized to a dynamic.</returns> | ||
public static Task<dynamic> GetJsonAsync(this string url, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, CancellationToken cancellationToken = default) { | ||
return new FlurlRequest(url).GetJsonAsync(completionOption, cancellationToken); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a FlurlRequest and sends an asynchronous GET request. | ||
/// </summary> | ||
/// <param name="url">This URL.</param> | ||
/// <param name="completionOption">The HttpCompletionOption used in the request. Optional.</param> | ||
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param> | ||
/// <returns>A Task whose result is the JSON response body deserialized to a list of dynamics.</returns> | ||
public static Task<IList<dynamic>> GetJsonListAsync(this string url, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, CancellationToken cancellationToken = default) { | ||
return new FlurlRequest(url).GetJsonListAsync(completionOption, cancellationToken); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a FlurlRequest and sends an asynchronous GET request. | ||
/// </summary> | ||
/// <param name="uri">This System.Uri.</param> | ||
/// <param name="completionOption">The HttpCompletionOption used in the request. Optional.</param> | ||
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param> | ||
/// <returns>A Task whose result is the JSON response body deserialized to a dynamic.</returns> | ||
public static Task<dynamic> GetJsonAsync(this Uri uri, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, CancellationToken cancellationToken = default) { | ||
return new FlurlRequest(uri).GetJsonAsync(completionOption, cancellationToken); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a FlurlRequest and sends an asynchronous GET request. | ||
/// </summary> | ||
/// <param name="uri">This System.Uri.</param> | ||
/// <param name="completionOption">The HttpCompletionOption used in the request. Optional.</param> | ||
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param> | ||
/// <returns>A Task whose result is the JSON response body deserialized to a list of dynamics.</returns> | ||
public static Task<IList<dynamic>> GetJsonListAsync(this Uri uri, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead, CancellationToken cancellationToken = default) { | ||
return new FlurlRequest(uri).GetJsonListAsync(completionOption, cancellationToken); | ||
} | ||
|
||
} | ||
} |