Refactoring related to (optionally) keeping config settings out of global scope, ISerializer

This commit is contained in:
tmenier 2015-09-08 21:56:49 -05:00
parent 5f4e4ee88b
commit 03445902c1
24 changed files with 347 additions and 222 deletions

View File

@ -58,16 +58,29 @@ namespace Flurl.Http.CodeGen
writer.WriteLine("public static Task<@0> @1@2(@3) {", xm.TaskArg, xm.Name, xm.IsGeneric ? "<T>" : "", string.Join(", ", args));
args.Clear();
args.Add(xm.HttpVerb == "Patch" ? "new HttpMethod(\"PATCH\")" : "HttpMethod." + xm.HttpVerb); // there's no HttpMethod.Patch
if (xm.BodyType != null)
args.Add(string.Format("content: new Captured{0}Content(data)", xm.BodyType));
if (xm.HasCancelationToken)
args.Add("cancellationToken: cancellationToken");
if (xm.ExtentionOfType == "FlurlClient") {
if (xm.BodyType != null) {
writer.WriteLine("var content = new Captured@0Content(@1);",
xm.BodyType,
xm.BodyType == "String" ? "data" : string.Format("client.Settings.{0}Serializer.Serialize(data)", xm.BodyType));
}
var client = (xm.ExtentionOfType == "FlurlClient") ? "client" : "new FlurlClient(url, false)";
var receive = (xm.DeserializeToType == null) ? "" : string.Format(".Receive{0}{1}()", xm.DeserializeToType, xm.IsGeneric ? "<T>" : "");
writer.WriteLine("return @0.SendAsync(@1)@2;", client, string.Join(", ", args), receive);
args.Clear();
args.Add(xm.HttpVerb == "Patch" ? "new HttpMethod(\"PATCH\")" : "HttpMethod." + xm.HttpVerb); // there's no HttpMethod.Patch
if (xm.BodyType != null)
args.Add("content: content");
if (xm.HasCancelationToken)
args.Add("cancellationToken: cancellationToken");
var client = (xm.ExtentionOfType == "FlurlClient") ? "client" : "new FlurlClient(url, false)";
var receive = (xm.DeserializeToType == null) ? "" : string.Format(".Receive{0}{1}()", xm.DeserializeToType, xm.IsGeneric ? "<T>" : "");
writer.WriteLine("return @0.SendAsync(@1)@2;", client, string.Join(", ", args), receive);
}
else {
writer.WriteLine("return new FlurlClient(url, false).@0(@1);",
xm.Name + (xm.IsGeneric ? "<T>" : ""),
string.Join(", ", args.Skip(1).Select(a => a.Split(' ').Last())));
}
writer.WriteLine("}").WriteLine();
}

View File

@ -260,7 +260,13 @@ namespace Flurl.Http
/// <param name="pattern">Examples: "3xx", "100,300,600", "100-299,6xx"</param>
/// <returns>The modified FlurlClient.</returns>
public static FlurlClient AllowHttpStatus(this FlurlClient client, string pattern) {
client.AllowedHttpStatusRanges.Add(pattern);
if (!string.IsNullOrWhiteSpace(pattern)) {
var current = client.Settings.AllowedHttpStatusRange;
if (string.IsNullOrWhiteSpace(current))
client.Settings.AllowedHttpStatusRange = pattern;
else
client.Settings.AllowedHttpStatusRange += "," + pattern;
}
return client;
}
@ -315,8 +321,7 @@ namespace Flurl.Http
/// </summary>
/// <returns>The modified FlurlClient.</returns>
public static FlurlClient AllowAnyHttpStatus(this FlurlClient client) {
client.AllowedHttpStatusRanges.Clear();
client.AllowedHttpStatusRanges.Add("*");
client.Settings.AllowedHttpStatusRange = "*";
return client;
}

View File

@ -15,9 +15,7 @@ namespace Flurl.Http.Configuration
/// customize the result.
/// </summary>
public virtual HttpClient CreateClient(Url url, HttpMessageHandler handler) {
return new HttpClient(new FlurlMessageHandler(handler)) {
Timeout = FlurlHttp.Configuration.DefaultTimeout
};
return new HttpClient(new FlurlMessageHandler(handler));
}
/// <summary>

View File

@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Flurl.Util;
namespace Flurl.Http.Configuration
{
public class DefaultUrlEncodedSerializer : ISerializer
{
public string Serialize(object obj) {
var sb = new StringBuilder();
foreach (var kv in obj.ToKeyValuePairs()) {
if (kv.Value == null)
continue;
if (sb.Length > 0)
sb.Append('&');
sb.Append(Url.EncodeQueryParamValue(kv.Key, true));
sb.Append('=');
sb.Append(Url.EncodeQueryParamValue(kv.Value, true));
}
return sb.ToString();
}
public T Deserialize<T>(string s) {
throw new NotImplementedException("Deserializing to UrlEncoded not supported.");
}
public T Deserialize<T>(Stream stream) {
throw new NotImplementedException("Deserializing to UrlEncoded not supported.");
}
}
}

View File

@ -7,9 +7,9 @@ namespace Flurl.Http.Configuration
/// <summary>
/// A set of properties that affect Flurl.Http behavior. Generally set via static FlurlHttp.Configure method.
/// </summary>
public class FlurlHttpConfigurationOptions
public class FlurlHttpSettings
{
internal FlurlHttpConfigurationOptions() {
public FlurlHttpSettings() {
ResetDefaults();
}
@ -33,6 +33,16 @@ namespace Flurl.Http.Configuration
/// </summary>
public IHttpClientFactory HttpClientFactory { get; set; }
/// <summary>
/// Gets or sets object used to serialize and deserialize JSON. Default implementation uses Newtonsoft Json.NET.
/// </summary>
public ISerializer JsonSerializer { get; set; }
/// <summary>
/// Gets or sets object used to serialize URL-encoded data. (Deserialization not supported in default implementation.)
/// </summary>
public DefaultUrlEncodedSerializer UrlEncodedSerializer { get; set; }
/// <summary>
/// Gets or sets a callback that is called immediately before every HTTP request is sent.
/// </summary>
@ -72,6 +82,8 @@ namespace Flurl.Http.Configuration
DefaultTimeout = new HttpClient().Timeout;
AllowedHttpStatusRange = null;
HttpClientFactory = new DefaultHttpClientFactory();
JsonSerializer = new NewtonsoftJsonSerializer(null);
UrlEncodedSerializer = new DefaultUrlEncodedSerializer();
BeforeCall = null;
BeforeCallAsync = null;
AfterCall = null;
@ -79,5 +91,9 @@ namespace Flurl.Http.Configuration
OnError = null;
OnErrorAsync = null;
}
public FlurlHttpSettings Clone() {
return (FlurlHttpSettings)this.MemberwiseClone();
}
}
}

View File

@ -14,6 +14,8 @@ namespace Flurl.Http.Configuration
public FlurlMessageHandler(HttpMessageHandler innerHandler) : base(innerHandler) { }
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {
var settings = request.GetFlurlSettings();
var call = new HttpCall {
Request = request
};
@ -22,7 +24,7 @@ namespace Flurl.Http.Configuration
if (stringContent != null)
call.RequestBody = stringContent.Content;
await RaiseGlobalEventAsync(FlurlHttp.Configuration.BeforeCall, FlurlHttp.Configuration.BeforeCallAsync, call);
await RaiseGlobalEventAsync(settings.BeforeCall, settings.BeforeCallAsync, call);
call.StartedUtc = DateTime.UtcNow;
@ -44,13 +46,14 @@ namespace Flurl.Http.Configuration
}
if (call.Exception != null)
await RaiseGlobalEventAsync(FlurlHttp.Configuration.OnError, FlurlHttp.Configuration.OnErrorAsync, call);
await RaiseGlobalEventAsync(settings.OnError, settings.OnErrorAsync, call);
await RaiseGlobalEventAsync(FlurlHttp.Configuration.AfterCall, FlurlHttp.Configuration.AfterCallAsync, call);
await RaiseGlobalEventAsync(settings.AfterCall, settings.AfterCallAsync, call);
if (call.Exception != null && !call.ExceptionHandled)
throw call.Exception;
call.Response.RequestMessage = request;
return call.Response;
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Flurl.Http.Configuration
{
public interface ISerializer
{
string Serialize(object obj);
T Deserialize<T>(string s);
T Deserialize<T>(Stream stream);
}
}

View File

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Newtonsoft.Json;
namespace Flurl.Http.Configuration
{
/// <summary>
/// ISerializer implementation that uses Newtonsoft Json.NET. Used as Flurl.Http's default JSON serializer.
/// </summary>
public class NewtonsoftJsonSerializer : ISerializer
{
private readonly JsonSerializerSettings _settings;
public NewtonsoftJsonSerializer(JsonSerializerSettings settings) {
_settings = settings;
}
public string Serialize(object obj) {
return JsonConvert.SerializeObject(obj, _settings);
}
public T Deserialize<T>(string s) {
return JsonConvert.DeserializeObject<T>(s, _settings);
}
public T Deserialize<T>(Stream stream) {
// http://james.newtonking.com/json/help/index.html?topic=html/Performance.htm
using (var sr = new StreamReader(stream))
using (var jr = new JsonTextReader(sr)) {
return JsonSerializer.CreateDefault(_settings).Deserialize<T>(jr);
}
}
}
}

View File

@ -1,5 +1,4 @@
using System.Text;
using Newtonsoft.Json;
namespace Flurl.Http.Content
{
@ -9,6 +8,6 @@ namespace Flurl.Http.Content
/// </summary>
public class CapturedJsonContent : CapturedStringContent
{
public CapturedJsonContent(object data) : base(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json") { }
public CapturedJsonContent(string json) : base(json, Encoding.UTF8, "application/json") { }
}
}

View File

@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Net.Http.Headers;
using System.Text;
using Flurl.Util;
namespace Flurl.Http.Content
{
@ -12,28 +10,8 @@ namespace Flurl.Http.Content
/// </summary>
public class CapturedUrlEncodedContent : CapturedStringContent
{
// This implementation was largely lifted from System.Net.Http.FormUrlEncodedContent, which unfortunately
// doesn't have the hooks needed for getting the content body as a string.
public CapturedUrlEncodedContent(object data) : base(GetContent(data)) {
public CapturedUrlEncodedContent(string data) : base(data) {
this.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
}
private static string GetContent(object data) {
if (data == null)
throw new ArgumentNullException("data");
var sb = new StringBuilder();
foreach (var kv in data.ToKeyValuePairs()) {
if (kv.Value == null)
continue;
if (sb.Length > 0)
sb.Append('&');
sb.Append(Url.EncodeQueryParamValue(kv.Key, true));
sb.Append('=');
sb.Append(Url.EncodeQueryParamValue(kv.Value, true));
}
return sb.ToString();
}
}
}

View File

@ -11,9 +11,12 @@
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)ClientConfigExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Configuration\DefaultHttpClientFactory.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Configuration\FlurlHttpConfigurationOptions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Configuration\FlurlHttpSettings.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Configuration\FlurlMessageHandler.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Configuration\IHttpClientFactory.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Configuration\ISerializer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Configuration\NewtonsoftJsonSerializer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Configuration\DefaultUrlEncodedSerializer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Content\CapturedUrlEncodedContent.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Content\CapturedJsonContent.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Content\CapturedStringContent.cs" />
@ -23,10 +26,10 @@
<Compile Include="$(MSBuildThisFileDirectory)FlurlHttpException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)HttpCall.cs" />
<Compile Include="$(MSBuildThisFileDirectory)HttpExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)HttpRequestMessageExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)HttpStatusRangeParser.cs" />
<Compile Include="$(MSBuildThisFileDirectory)JsonExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ResponseMessageExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)HttpResponseMessageExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Testing\FakeHttpMessageHandler.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Testing\HttpCallAssertException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Testing\HttpCallAssertion.cs" />

View File

@ -5,6 +5,7 @@ using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Flurl.Http.Configuration;
namespace Flurl.Http
{
@ -16,9 +17,7 @@ namespace Flurl.Http
public FlurlClient(Url url, bool autoDispose) {
this.Url = url;
this.AutoDispose = autoDispose;
this.AllowedHttpStatusRanges = new List<string>();
if (FlurlHttp.Configuration.AllowedHttpStatusRange != null)
this.AllowedHttpStatusRanges.Add(FlurlHttp.Configuration.AllowedHttpStatusRange);
this.Settings = FlurlHttp.GlobalSettings.Clone();
}
public FlurlClient(string url, bool autoDispose) : this(new Url(url), autoDispose) { }
@ -29,6 +28,11 @@ namespace Flurl.Http
private HttpClient _httpClient;
private HttpMessageHandler _httpMessageHandler;
/// <summary>
/// Gets or sets the FlurlHttpSettings object used by this client.
/// </summary>
public FlurlHttpSettings Settings { get; set; }
/// <summary>
/// Gets or sets the URL to be called.
/// </summary>
@ -46,8 +50,10 @@ namespace Flurl.Http
/// </summary>
public HttpClient HttpClient {
get {
if (_httpClient == null)
_httpClient = FlurlHttp.Configuration.HttpClientFactory.CreateClient(Url, HttpMessageHandler);
if (_httpClient == null) {
_httpClient = Settings.HttpClientFactory.CreateClient(Url, HttpMessageHandler);
_httpClient.Timeout = Settings.DefaultTimeout;
}
return _httpClient;
}
}
@ -60,8 +66,7 @@ namespace Flurl.Http
public async Task<HttpResponseMessage> SendAsync(HttpMethod verb, HttpContent content = null, CancellationToken? cancellationToken = null, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) {
try {
var request = new HttpRequestMessage(verb, this.Url) { Content = content };
// mechanism for passing ad-hoc data to the MessageHandler
request.Properties.Add("AllowedHttpStatusRanges", AllowedHttpStatusRanges);
request.SetFlurlSettings(this.Settings);
return await HttpClient.SendAsync(request, completionOption, cancellationToken ?? CancellationToken.None);
}
finally {
@ -76,19 +81,12 @@ namespace Flurl.Http
public HttpMessageHandler HttpMessageHandler {
get {
if (_httpMessageHandler == null)
_httpMessageHandler = FlurlHttp.Configuration.HttpClientFactory.CreateMessageHandler();
_httpMessageHandler = Settings.HttpClientFactory.CreateMessageHandler();
return _httpMessageHandler;
}
}
/// <summary>
/// Gets a collection of pattern representing HTTP status codes (or ranges) which, in addition to 2xx, will NOT result in FlurlHttpException being thrown.
/// Examples: "3xx", "100,300,600", "100-299,6xx", "*" (allow everything)
/// 2xx will never throw regardless of this setting.
/// </summary>
public ICollection<string> AllowedHttpStatusRanges { get; private set; }
/// <summary>
/// Disposes the underlying HttpClient and HttpMessageHandler, setting both properties to null.
/// This FlurlClient can still be reused, but those underlying objects will be re-created as needed. Previously set headers, etc, will be lost.

View File

@ -10,21 +10,23 @@ namespace Flurl.Http
{
private static readonly object _configLock = new object();
private static Lazy<FlurlHttpConfigurationOptions> _config =
new Lazy<FlurlHttpConfigurationOptions>(() => new FlurlHttpConfigurationOptions());
private static Lazy<FlurlHttpSettings> _settings =
new Lazy<FlurlHttpSettings>(() => new FlurlHttpSettings());
public static FlurlHttpConfigurationOptions Configuration {
get { return _config.Value; }
/// <summary>
/// Globally configured Flurl.Http settings. Should normally be written to by calling FlurlHttp.Configure once application at startup.
/// </summary>
public static FlurlHttpSettings GlobalSettings {
get { return _settings.Value; }
}
/// <summary>
/// Provides thread-safe accesss to Flurl.Http's global configuration options. Should only be
/// called once at application startup.
/// Provides thread-safe accesss to Flurl.Http's global configuration settings. Should only be called once at application startup.
/// </summary>
/// <param name="configAction"></param>
public static void Configure(Action<FlurlHttpConfigurationOptions> configAction) {
public static void Configure(Action<FlurlHttpSettings> configAction) {
lock (_configLock) {
configAction(Configuration);
configAction(GlobalSettings);
}
}
}

View File

@ -1,7 +1,6 @@
using System;
using System.Dynamic;
using System.Net.Http;
using Newtonsoft.Json;
namespace Flurl.Http
{
@ -56,7 +55,7 @@ namespace Flurl.Http
return
(Call == null) ? default(T) :
(Call.ErrorResponseBody == null) ? default(T) :
JsonConvert.DeserializeObject<T>(Call.ErrorResponseBody);
Call.Request.GetFlurlSettings().JsonSerializer.Deserialize<T>(Call.ErrorResponseBody);
}
/// <summary>

View File

@ -76,10 +76,7 @@ namespace Flurl.Http
get {
if (!Completed) return false;
if (Response.IsSuccessStatusCode) return true;
if (!Request.Properties.ContainsKey("AllowedHttpStatusRanges")) return false;
var allowedStatuses = Request.Properties["AllowedHttpStatusRanges"] as IEnumerable<string>;
if (allowedStatuses == null) return false;
return allowedStatuses.Any(s => HttpStatusRangeParser.IsMatch(s, Response.StatusCode));
return HttpStatusRangeParser.IsMatch(Request.GetFlurlSettings().AllowedHttpStatusRange, Response.StatusCode);
}
}

View File

@ -136,7 +136,7 @@ namespace Flurl.Http
/// <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> GetAsync(this Url url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get, cancellationToken: cancellationToken);
return new FlurlClient(url, false).GetAsync(cancellationToken);
}
/// <summary>
@ -144,7 +144,7 @@ namespace Flurl.Http
/// </summary>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> GetAsync(this Url url) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get);
return new FlurlClient(url, false).GetAsync();
}
/// <summary>
@ -153,7 +153,7 @@ namespace Flurl.Http
/// <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> GetJsonAsync<T>(this Url url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get, cancellationToken: cancellationToken).ReceiveJson<T>();
return new FlurlClient(url, false).GetJsonAsync<T>(cancellationToken);
}
/// <summary>
@ -161,7 +161,7 @@ namespace Flurl.Http
/// </summary>
/// <returns>A Task whose result is the JSON response body deserialized to an object of type T.</returns>
public static Task<T> GetJsonAsync<T>(this Url url) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get).ReceiveJson<T>();
return new FlurlClient(url, false).GetJsonAsync<T>();
}
/// <summary>
@ -170,7 +170,7 @@ namespace Flurl.Http
/// <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 dynamic.</returns>
public static Task<dynamic> GetJsonAsync(this Url url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get, cancellationToken: cancellationToken).ReceiveJson();
return new FlurlClient(url, false).GetJsonAsync(cancellationToken);
}
/// <summary>
@ -178,7 +178,7 @@ namespace Flurl.Http
/// </summary>
/// <returns>A Task whose result is the JSON response body deserialized to a dynamic.</returns>
public static Task<dynamic> GetJsonAsync(this Url url) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get).ReceiveJson();
return new FlurlClient(url, false).GetJsonAsync();
}
/// <summary>
@ -187,7 +187,7 @@ namespace Flurl.Http
/// <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>> GetJsonListAsync(this Url url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get, cancellationToken: cancellationToken).ReceiveJsonList();
return new FlurlClient(url, false).GetJsonListAsync(cancellationToken);
}
/// <summary>
@ -195,7 +195,7 @@ namespace Flurl.Http
/// </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 Url url) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get).ReceiveJsonList();
return new FlurlClient(url, false).GetJsonListAsync();
}
/// <summary>
@ -204,7 +204,7 @@ namespace Flurl.Http
/// <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 response body as a string.</returns>
public static Task<string> GetStringAsync(this Url url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get, cancellationToken: cancellationToken).ReceiveString();
return new FlurlClient(url, false).GetStringAsync(cancellationToken);
}
/// <summary>
@ -212,7 +212,7 @@ namespace Flurl.Http
/// </summary>
/// <returns>A Task whose result is the response body as a string.</returns>
public static Task<string> GetStringAsync(this Url url) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get).ReceiveString();
return new FlurlClient(url, false).GetStringAsync();
}
/// <summary>
@ -221,7 +221,7 @@ namespace Flurl.Http
/// <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 response body as a Stream.</returns>
public static Task<Stream> GetStreamAsync(this Url url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get, cancellationToken: cancellationToken).ReceiveStream();
return new FlurlClient(url, false).GetStreamAsync(cancellationToken);
}
/// <summary>
@ -229,7 +229,7 @@ namespace Flurl.Http
/// </summary>
/// <returns>A Task whose result is the response body as a Stream.</returns>
public static Task<Stream> GetStreamAsync(this Url url) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get).ReceiveStream();
return new FlurlClient(url, false).GetStreamAsync();
}
/// <summary>
@ -238,7 +238,7 @@ namespace Flurl.Http
/// <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 response body as a byte array.</returns>
public static Task<byte[]> GetBytesAsync(this Url url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get, cancellationToken: cancellationToken).ReceiveBytes();
return new FlurlClient(url, false).GetBytesAsync(cancellationToken);
}
/// <summary>
@ -246,7 +246,7 @@ namespace Flurl.Http
/// </summary>
/// <returns>A Task whose result is the response body as a byte array.</returns>
public static Task<byte[]> GetBytesAsync(this Url url) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get).ReceiveBytes();
return new FlurlClient(url, false).GetBytesAsync();
}
/// <summary>
@ -255,7 +255,7 @@ namespace Flurl.Http
/// <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> GetAsync(this string url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get, cancellationToken: cancellationToken);
return new FlurlClient(url, false).GetAsync(cancellationToken);
}
/// <summary>
@ -263,7 +263,7 @@ namespace Flurl.Http
/// </summary>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> GetAsync(this string url) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get);
return new FlurlClient(url, false).GetAsync();
}
/// <summary>
@ -272,7 +272,7 @@ namespace Flurl.Http
/// <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> GetJsonAsync<T>(this string url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get, cancellationToken: cancellationToken).ReceiveJson<T>();
return new FlurlClient(url, false).GetJsonAsync<T>(cancellationToken);
}
/// <summary>
@ -280,7 +280,7 @@ namespace Flurl.Http
/// </summary>
/// <returns>A Task whose result is the JSON response body deserialized to an object of type T.</returns>
public static Task<T> GetJsonAsync<T>(this string url) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get).ReceiveJson<T>();
return new FlurlClient(url, false).GetJsonAsync<T>();
}
/// <summary>
@ -289,7 +289,7 @@ namespace Flurl.Http
/// <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 dynamic.</returns>
public static Task<dynamic> GetJsonAsync(this string url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get, cancellationToken: cancellationToken).ReceiveJson();
return new FlurlClient(url, false).GetJsonAsync(cancellationToken);
}
/// <summary>
@ -297,7 +297,7 @@ namespace Flurl.Http
/// </summary>
/// <returns>A Task whose result is the JSON response body deserialized to a dynamic.</returns>
public static Task<dynamic> GetJsonAsync(this string url) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get).ReceiveJson();
return new FlurlClient(url, false).GetJsonAsync();
}
/// <summary>
@ -306,7 +306,7 @@ namespace Flurl.Http
/// <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>> GetJsonListAsync(this string url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get, cancellationToken: cancellationToken).ReceiveJsonList();
return new FlurlClient(url, false).GetJsonListAsync(cancellationToken);
}
/// <summary>
@ -314,7 +314,7 @@ namespace Flurl.Http
/// </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 string url) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get).ReceiveJsonList();
return new FlurlClient(url, false).GetJsonListAsync();
}
/// <summary>
@ -323,7 +323,7 @@ namespace Flurl.Http
/// <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 response body as a string.</returns>
public static Task<string> GetStringAsync(this string url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get, cancellationToken: cancellationToken).ReceiveString();
return new FlurlClient(url, false).GetStringAsync(cancellationToken);
}
/// <summary>
@ -331,7 +331,7 @@ namespace Flurl.Http
/// </summary>
/// <returns>A Task whose result is the response body as a string.</returns>
public static Task<string> GetStringAsync(this string url) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get).ReceiveString();
return new FlurlClient(url, false).GetStringAsync();
}
/// <summary>
@ -340,7 +340,7 @@ namespace Flurl.Http
/// <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 response body as a Stream.</returns>
public static Task<Stream> GetStreamAsync(this string url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get, cancellationToken: cancellationToken).ReceiveStream();
return new FlurlClient(url, false).GetStreamAsync(cancellationToken);
}
/// <summary>
@ -348,7 +348,7 @@ namespace Flurl.Http
/// </summary>
/// <returns>A Task whose result is the response body as a Stream.</returns>
public static Task<Stream> GetStreamAsync(this string url) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get).ReceiveStream();
return new FlurlClient(url, false).GetStreamAsync();
}
/// <summary>
@ -357,7 +357,7 @@ namespace Flurl.Http
/// <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 response body as a byte array.</returns>
public static Task<byte[]> GetBytesAsync(this string url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get, cancellationToken: cancellationToken).ReceiveBytes();
return new FlurlClient(url, false).GetBytesAsync(cancellationToken);
}
/// <summary>
@ -365,7 +365,7 @@ namespace Flurl.Http
/// </summary>
/// <returns>A Task whose result is the response body as a byte array.</returns>
public static Task<byte[]> GetBytesAsync(this string url) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Get).ReceiveBytes();
return new FlurlClient(url, false).GetBytesAsync();
}
/// <summary>
@ -391,7 +391,7 @@ namespace Flurl.Http
/// <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> PostAsync(this Url url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Post, cancellationToken: cancellationToken);
return new FlurlClient(url, false).PostAsync(cancellationToken);
}
/// <summary>
@ -399,7 +399,7 @@ namespace Flurl.Http
/// </summary>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PostAsync(this Url url) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Post);
return new FlurlClient(url, false).PostAsync();
}
/// <summary>
@ -408,7 +408,7 @@ namespace Flurl.Http
/// <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> PostAsync(this string url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Post, cancellationToken: cancellationToken);
return new FlurlClient(url, false).PostAsync(cancellationToken);
}
/// <summary>
@ -416,7 +416,7 @@ namespace Flurl.Http
/// </summary>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PostAsync(this string url) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Post);
return new FlurlClient(url, false).PostAsync();
}
/// <summary>
@ -426,7 +426,8 @@ namespace Flurl.Http
/// <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> PostJsonAsync(this FlurlClient client, object data, CancellationToken cancellationToken) {
return client.SendAsync(HttpMethod.Post, content: new CapturedJsonContent(data), cancellationToken: cancellationToken);
var content = new CapturedJsonContent(client.Settings.JsonSerializer.Serialize(data));
return client.SendAsync(HttpMethod.Post, content: content, cancellationToken: cancellationToken);
}
/// <summary>
@ -435,7 +436,8 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PostJsonAsync(this FlurlClient client, object data) {
return client.SendAsync(HttpMethod.Post, content: new CapturedJsonContent(data));
var content = new CapturedJsonContent(client.Settings.JsonSerializer.Serialize(data));
return client.SendAsync(HttpMethod.Post, content: content);
}
/// <summary>
@ -445,7 +447,7 @@ namespace Flurl.Http
/// <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> PostJsonAsync(this Url url, object data, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Post, content: new CapturedJsonContent(data), cancellationToken: cancellationToken);
return new FlurlClient(url, false).PostJsonAsync(data, cancellationToken);
}
/// <summary>
@ -454,7 +456,7 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PostJsonAsync(this Url url, object data) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Post, content: new CapturedJsonContent(data));
return new FlurlClient(url, false).PostJsonAsync(data);
}
/// <summary>
@ -464,7 +466,7 @@ namespace Flurl.Http
/// <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> PostJsonAsync(this string url, object data, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Post, content: new CapturedJsonContent(data), cancellationToken: cancellationToken);
return new FlurlClient(url, false).PostJsonAsync(data, cancellationToken);
}
/// <summary>
@ -473,7 +475,7 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PostJsonAsync(this string url, object data) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Post, content: new CapturedJsonContent(data));
return new FlurlClient(url, false).PostJsonAsync(data);
}
/// <summary>
@ -483,7 +485,8 @@ namespace Flurl.Http
/// <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> PostStringAsync(this FlurlClient client, string data, CancellationToken cancellationToken) {
return client.SendAsync(HttpMethod.Post, content: new CapturedStringContent(data), cancellationToken: cancellationToken);
var content = new CapturedStringContent(data);
return client.SendAsync(HttpMethod.Post, content: content, cancellationToken: cancellationToken);
}
/// <summary>
@ -492,7 +495,8 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PostStringAsync(this FlurlClient client, string data) {
return client.SendAsync(HttpMethod.Post, content: new CapturedStringContent(data));
var content = new CapturedStringContent(data);
return client.SendAsync(HttpMethod.Post, content: content);
}
/// <summary>
@ -502,7 +506,7 @@ namespace Flurl.Http
/// <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> PostStringAsync(this Url url, string data, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Post, content: new CapturedStringContent(data), cancellationToken: cancellationToken);
return new FlurlClient(url, false).PostStringAsync(data, cancellationToken);
}
/// <summary>
@ -511,7 +515,7 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PostStringAsync(this Url url, string data) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Post, content: new CapturedStringContent(data));
return new FlurlClient(url, false).PostStringAsync(data);
}
/// <summary>
@ -521,7 +525,7 @@ namespace Flurl.Http
/// <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> PostStringAsync(this string url, string data, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Post, content: new CapturedStringContent(data), cancellationToken: cancellationToken);
return new FlurlClient(url, false).PostStringAsync(data, cancellationToken);
}
/// <summary>
@ -530,7 +534,7 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PostStringAsync(this string url, string data) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Post, content: new CapturedStringContent(data));
return new FlurlClient(url, false).PostStringAsync(data);
}
/// <summary>
@ -540,7 +544,8 @@ namespace Flurl.Http
/// <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> PostUrlEncodedAsync(this FlurlClient client, object data, CancellationToken cancellationToken) {
return client.SendAsync(HttpMethod.Post, content: new CapturedUrlEncodedContent(data), cancellationToken: cancellationToken);
var content = new CapturedUrlEncodedContent(client.Settings.UrlEncodedSerializer.Serialize(data));
return client.SendAsync(HttpMethod.Post, content: content, cancellationToken: cancellationToken);
}
/// <summary>
@ -549,7 +554,8 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PostUrlEncodedAsync(this FlurlClient client, object data) {
return client.SendAsync(HttpMethod.Post, content: new CapturedUrlEncodedContent(data));
var content = new CapturedUrlEncodedContent(client.Settings.UrlEncodedSerializer.Serialize(data));
return client.SendAsync(HttpMethod.Post, content: content);
}
/// <summary>
@ -559,7 +565,7 @@ namespace Flurl.Http
/// <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> PostUrlEncodedAsync(this Url url, object data, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Post, content: new CapturedUrlEncodedContent(data), cancellationToken: cancellationToken);
return new FlurlClient(url, false).PostUrlEncodedAsync(data, cancellationToken);
}
/// <summary>
@ -568,7 +574,7 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PostUrlEncodedAsync(this Url url, object data) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Post, content: new CapturedUrlEncodedContent(data));
return new FlurlClient(url, false).PostUrlEncodedAsync(data);
}
/// <summary>
@ -578,7 +584,7 @@ namespace Flurl.Http
/// <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> PostUrlEncodedAsync(this string url, object data, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Post, content: new CapturedUrlEncodedContent(data), cancellationToken: cancellationToken);
return new FlurlClient(url, false).PostUrlEncodedAsync(data, cancellationToken);
}
/// <summary>
@ -587,7 +593,7 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PostUrlEncodedAsync(this string url, object data) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Post, content: new CapturedUrlEncodedContent(data));
return new FlurlClient(url, false).PostUrlEncodedAsync(data);
}
/// <summary>
@ -613,7 +619,7 @@ namespace Flurl.Http
/// <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> HeadAsync(this Url url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Head, cancellationToken: cancellationToken);
return new FlurlClient(url, false).HeadAsync(cancellationToken);
}
/// <summary>
@ -621,7 +627,7 @@ namespace Flurl.Http
/// </summary>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> HeadAsync(this Url url) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Head);
return new FlurlClient(url, false).HeadAsync();
}
/// <summary>
@ -630,7 +636,7 @@ namespace Flurl.Http
/// <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> HeadAsync(this string url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Head, cancellationToken: cancellationToken);
return new FlurlClient(url, false).HeadAsync(cancellationToken);
}
/// <summary>
@ -638,7 +644,7 @@ namespace Flurl.Http
/// </summary>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> HeadAsync(this string url) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Head);
return new FlurlClient(url, false).HeadAsync();
}
/// <summary>
@ -664,7 +670,7 @@ namespace Flurl.Http
/// <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> PutAsync(this Url url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Put, cancellationToken: cancellationToken);
return new FlurlClient(url, false).PutAsync(cancellationToken);
}
/// <summary>
@ -672,7 +678,7 @@ namespace Flurl.Http
/// </summary>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PutAsync(this Url url) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Put);
return new FlurlClient(url, false).PutAsync();
}
/// <summary>
@ -681,7 +687,7 @@ namespace Flurl.Http
/// <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> PutAsync(this string url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Put, cancellationToken: cancellationToken);
return new FlurlClient(url, false).PutAsync(cancellationToken);
}
/// <summary>
@ -689,7 +695,7 @@ namespace Flurl.Http
/// </summary>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PutAsync(this string url) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Put);
return new FlurlClient(url, false).PutAsync();
}
/// <summary>
@ -699,7 +705,8 @@ namespace Flurl.Http
/// <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> PutJsonAsync(this FlurlClient client, object data, CancellationToken cancellationToken) {
return client.SendAsync(HttpMethod.Put, content: new CapturedJsonContent(data), cancellationToken: cancellationToken);
var content = new CapturedJsonContent(client.Settings.JsonSerializer.Serialize(data));
return client.SendAsync(HttpMethod.Put, content: content, cancellationToken: cancellationToken);
}
/// <summary>
@ -708,7 +715,8 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PutJsonAsync(this FlurlClient client, object data) {
return client.SendAsync(HttpMethod.Put, content: new CapturedJsonContent(data));
var content = new CapturedJsonContent(client.Settings.JsonSerializer.Serialize(data));
return client.SendAsync(HttpMethod.Put, content: content);
}
/// <summary>
@ -718,7 +726,7 @@ namespace Flurl.Http
/// <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> PutJsonAsync(this Url url, object data, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Put, content: new CapturedJsonContent(data), cancellationToken: cancellationToken);
return new FlurlClient(url, false).PutJsonAsync(data, cancellationToken);
}
/// <summary>
@ -727,7 +735,7 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PutJsonAsync(this Url url, object data) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Put, content: new CapturedJsonContent(data));
return new FlurlClient(url, false).PutJsonAsync(data);
}
/// <summary>
@ -737,7 +745,7 @@ namespace Flurl.Http
/// <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> PutJsonAsync(this string url, object data, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Put, content: new CapturedJsonContent(data), cancellationToken: cancellationToken);
return new FlurlClient(url, false).PutJsonAsync(data, cancellationToken);
}
/// <summary>
@ -746,7 +754,7 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PutJsonAsync(this string url, object data) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Put, content: new CapturedJsonContent(data));
return new FlurlClient(url, false).PutJsonAsync(data);
}
/// <summary>
@ -756,7 +764,8 @@ namespace Flurl.Http
/// <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> PutStringAsync(this FlurlClient client, string data, CancellationToken cancellationToken) {
return client.SendAsync(HttpMethod.Put, content: new CapturedStringContent(data), cancellationToken: cancellationToken);
var content = new CapturedStringContent(data);
return client.SendAsync(HttpMethod.Put, content: content, cancellationToken: cancellationToken);
}
/// <summary>
@ -765,7 +774,8 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PutStringAsync(this FlurlClient client, string data) {
return client.SendAsync(HttpMethod.Put, content: new CapturedStringContent(data));
var content = new CapturedStringContent(data);
return client.SendAsync(HttpMethod.Put, content: content);
}
/// <summary>
@ -775,7 +785,7 @@ namespace Flurl.Http
/// <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> PutStringAsync(this Url url, string data, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Put, content: new CapturedStringContent(data), cancellationToken: cancellationToken);
return new FlurlClient(url, false).PutStringAsync(data, cancellationToken);
}
/// <summary>
@ -784,7 +794,7 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PutStringAsync(this Url url, string data) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Put, content: new CapturedStringContent(data));
return new FlurlClient(url, false).PutStringAsync(data);
}
/// <summary>
@ -794,7 +804,7 @@ namespace Flurl.Http
/// <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> PutStringAsync(this string url, string data, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Put, content: new CapturedStringContent(data), cancellationToken: cancellationToken);
return new FlurlClient(url, false).PutStringAsync(data, cancellationToken);
}
/// <summary>
@ -803,7 +813,7 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PutStringAsync(this string url, string data) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Put, content: new CapturedStringContent(data));
return new FlurlClient(url, false).PutStringAsync(data);
}
/// <summary>
@ -813,7 +823,8 @@ namespace Flurl.Http
/// <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> PutUrlEncodedAsync(this FlurlClient client, object data, CancellationToken cancellationToken) {
return client.SendAsync(HttpMethod.Put, content: new CapturedUrlEncodedContent(data), cancellationToken: cancellationToken);
var content = new CapturedUrlEncodedContent(client.Settings.UrlEncodedSerializer.Serialize(data));
return client.SendAsync(HttpMethod.Put, content: content, cancellationToken: cancellationToken);
}
/// <summary>
@ -822,7 +833,8 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PutUrlEncodedAsync(this FlurlClient client, object data) {
return client.SendAsync(HttpMethod.Put, content: new CapturedUrlEncodedContent(data));
var content = new CapturedUrlEncodedContent(client.Settings.UrlEncodedSerializer.Serialize(data));
return client.SendAsync(HttpMethod.Put, content: content);
}
/// <summary>
@ -832,7 +844,7 @@ namespace Flurl.Http
/// <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> PutUrlEncodedAsync(this Url url, object data, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Put, content: new CapturedUrlEncodedContent(data), cancellationToken: cancellationToken);
return new FlurlClient(url, false).PutUrlEncodedAsync(data, cancellationToken);
}
/// <summary>
@ -841,7 +853,7 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PutUrlEncodedAsync(this Url url, object data) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Put, content: new CapturedUrlEncodedContent(data));
return new FlurlClient(url, false).PutUrlEncodedAsync(data);
}
/// <summary>
@ -851,7 +863,7 @@ namespace Flurl.Http
/// <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> PutUrlEncodedAsync(this string url, object data, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Put, content: new CapturedUrlEncodedContent(data), cancellationToken: cancellationToken);
return new FlurlClient(url, false).PutUrlEncodedAsync(data, cancellationToken);
}
/// <summary>
@ -860,7 +872,7 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PutUrlEncodedAsync(this string url, object data) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Put, content: new CapturedUrlEncodedContent(data));
return new FlurlClient(url, false).PutUrlEncodedAsync(data);
}
/// <summary>
@ -886,7 +898,7 @@ namespace Flurl.Http
/// <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> DeleteAsync(this Url url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Delete, cancellationToken: cancellationToken);
return new FlurlClient(url, false).DeleteAsync(cancellationToken);
}
/// <summary>
@ -894,7 +906,7 @@ namespace Flurl.Http
/// </summary>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> DeleteAsync(this Url url) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Delete);
return new FlurlClient(url, false).DeleteAsync();
}
/// <summary>
@ -903,7 +915,7 @@ namespace Flurl.Http
/// <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> DeleteAsync(this string url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Delete, cancellationToken: cancellationToken);
return new FlurlClient(url, false).DeleteAsync(cancellationToken);
}
/// <summary>
@ -911,7 +923,7 @@ namespace Flurl.Http
/// </summary>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> DeleteAsync(this string url) {
return new FlurlClient(url, false).SendAsync(HttpMethod.Delete);
return new FlurlClient(url, false).DeleteAsync();
}
/// <summary>
@ -937,7 +949,7 @@ namespace Flurl.Http
/// <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> PatchAsync(this Url url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(new HttpMethod("PATCH"), cancellationToken: cancellationToken);
return new FlurlClient(url, false).PatchAsync(cancellationToken);
}
/// <summary>
@ -945,7 +957,7 @@ namespace Flurl.Http
/// </summary>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PatchAsync(this Url url) {
return new FlurlClient(url, false).SendAsync(new HttpMethod("PATCH"));
return new FlurlClient(url, false).PatchAsync();
}
/// <summary>
@ -954,7 +966,7 @@ namespace Flurl.Http
/// <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> PatchAsync(this string url, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(new HttpMethod("PATCH"), cancellationToken: cancellationToken);
return new FlurlClient(url, false).PatchAsync(cancellationToken);
}
/// <summary>
@ -962,7 +974,7 @@ namespace Flurl.Http
/// </summary>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PatchAsync(this string url) {
return new FlurlClient(url, false).SendAsync(new HttpMethod("PATCH"));
return new FlurlClient(url, false).PatchAsync();
}
/// <summary>
@ -972,7 +984,8 @@ namespace Flurl.Http
/// <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> PatchJsonAsync(this FlurlClient client, object data, CancellationToken cancellationToken) {
return client.SendAsync(new HttpMethod("PATCH"), content: new CapturedJsonContent(data), cancellationToken: cancellationToken);
var content = new CapturedJsonContent(client.Settings.JsonSerializer.Serialize(data));
return client.SendAsync(new HttpMethod("PATCH"), content: content, cancellationToken: cancellationToken);
}
/// <summary>
@ -981,7 +994,8 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PatchJsonAsync(this FlurlClient client, object data) {
return client.SendAsync(new HttpMethod("PATCH"), content: new CapturedJsonContent(data));
var content = new CapturedJsonContent(client.Settings.JsonSerializer.Serialize(data));
return client.SendAsync(new HttpMethod("PATCH"), content: content);
}
/// <summary>
@ -991,7 +1005,7 @@ namespace Flurl.Http
/// <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> PatchJsonAsync(this Url url, object data, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(new HttpMethod("PATCH"), content: new CapturedJsonContent(data), cancellationToken: cancellationToken);
return new FlurlClient(url, false).PatchJsonAsync(data, cancellationToken);
}
/// <summary>
@ -1000,7 +1014,7 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PatchJsonAsync(this Url url, object data) {
return new FlurlClient(url, false).SendAsync(new HttpMethod("PATCH"), content: new CapturedJsonContent(data));
return new FlurlClient(url, false).PatchJsonAsync(data);
}
/// <summary>
@ -1010,7 +1024,7 @@ namespace Flurl.Http
/// <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> PatchJsonAsync(this string url, object data, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(new HttpMethod("PATCH"), content: new CapturedJsonContent(data), cancellationToken: cancellationToken);
return new FlurlClient(url, false).PatchJsonAsync(data, cancellationToken);
}
/// <summary>
@ -1019,7 +1033,7 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PatchJsonAsync(this string url, object data) {
return new FlurlClient(url, false).SendAsync(new HttpMethod("PATCH"), content: new CapturedJsonContent(data));
return new FlurlClient(url, false).PatchJsonAsync(data);
}
/// <summary>
@ -1029,7 +1043,8 @@ namespace Flurl.Http
/// <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> PatchStringAsync(this FlurlClient client, string data, CancellationToken cancellationToken) {
return client.SendAsync(new HttpMethod("PATCH"), content: new CapturedStringContent(data), cancellationToken: cancellationToken);
var content = new CapturedStringContent(data);
return client.SendAsync(new HttpMethod("PATCH"), content: content, cancellationToken: cancellationToken);
}
/// <summary>
@ -1038,7 +1053,8 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PatchStringAsync(this FlurlClient client, string data) {
return client.SendAsync(new HttpMethod("PATCH"), content: new CapturedStringContent(data));
var content = new CapturedStringContent(data);
return client.SendAsync(new HttpMethod("PATCH"), content: content);
}
/// <summary>
@ -1048,7 +1064,7 @@ namespace Flurl.Http
/// <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> PatchStringAsync(this Url url, string data, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(new HttpMethod("PATCH"), content: new CapturedStringContent(data), cancellationToken: cancellationToken);
return new FlurlClient(url, false).PatchStringAsync(data, cancellationToken);
}
/// <summary>
@ -1057,7 +1073,7 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PatchStringAsync(this Url url, string data) {
return new FlurlClient(url, false).SendAsync(new HttpMethod("PATCH"), content: new CapturedStringContent(data));
return new FlurlClient(url, false).PatchStringAsync(data);
}
/// <summary>
@ -1067,7 +1083,7 @@ namespace Flurl.Http
/// <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> PatchStringAsync(this string url, string data, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(new HttpMethod("PATCH"), content: new CapturedStringContent(data), cancellationToken: cancellationToken);
return new FlurlClient(url, false).PatchStringAsync(data, cancellationToken);
}
/// <summary>
@ -1076,7 +1092,7 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PatchStringAsync(this string url, string data) {
return new FlurlClient(url, false).SendAsync(new HttpMethod("PATCH"), content: new CapturedStringContent(data));
return new FlurlClient(url, false).PatchStringAsync(data);
}
/// <summary>
@ -1086,7 +1102,8 @@ namespace Flurl.Http
/// <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> PatchUrlEncodedAsync(this FlurlClient client, object data, CancellationToken cancellationToken) {
return client.SendAsync(new HttpMethod("PATCH"), content: new CapturedUrlEncodedContent(data), cancellationToken: cancellationToken);
var content = new CapturedUrlEncodedContent(client.Settings.UrlEncodedSerializer.Serialize(data));
return client.SendAsync(new HttpMethod("PATCH"), content: content, cancellationToken: cancellationToken);
}
/// <summary>
@ -1095,7 +1112,8 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PatchUrlEncodedAsync(this FlurlClient client, object data) {
return client.SendAsync(new HttpMethod("PATCH"), content: new CapturedUrlEncodedContent(data));
var content = new CapturedUrlEncodedContent(client.Settings.UrlEncodedSerializer.Serialize(data));
return client.SendAsync(new HttpMethod("PATCH"), content: content);
}
/// <summary>
@ -1105,7 +1123,7 @@ namespace Flurl.Http
/// <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> PatchUrlEncodedAsync(this Url url, object data, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(new HttpMethod("PATCH"), content: new CapturedUrlEncodedContent(data), cancellationToken: cancellationToken);
return new FlurlClient(url, false).PatchUrlEncodedAsync(data, cancellationToken);
}
/// <summary>
@ -1114,7 +1132,7 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PatchUrlEncodedAsync(this Url url, object data) {
return new FlurlClient(url, false).SendAsync(new HttpMethod("PATCH"), content: new CapturedUrlEncodedContent(data));
return new FlurlClient(url, false).PatchUrlEncodedAsync(data);
}
/// <summary>
@ -1124,7 +1142,7 @@ namespace Flurl.Http
/// <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> PatchUrlEncodedAsync(this string url, object data, CancellationToken cancellationToken) {
return new FlurlClient(url, false).SendAsync(new HttpMethod("PATCH"), content: new CapturedUrlEncodedContent(data), cancellationToken: cancellationToken);
return new FlurlClient(url, false).PatchUrlEncodedAsync(data, cancellationToken);
}
/// <summary>
@ -1133,7 +1151,7 @@ namespace Flurl.Http
/// <param name="data">Contents of the request body.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public static Task<HttpResponseMessage> PatchUrlEncodedAsync(this string url, object data) {
return new FlurlClient(url, false).SendAsync(new HttpMethod("PATCH"), content: new CapturedUrlEncodedContent(data));
return new FlurlClient(url, false).PatchUrlEncodedAsync(data);
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using Flurl.Http.Configuration;
namespace Flurl.Http
{
public static class HttpRequestMessageExtensions
{
public static void SetFlurlSettings(this HttpRequestMessage request, FlurlHttpSettings settings) {
request.Properties["FlurlSettings"] = settings;
}
public static FlurlHttpSettings GetFlurlSettings(this HttpRequestMessage request) {
object settings;
return request.Properties.TryGetValue("FlurlSettings", out settings) ? (FlurlHttpSettings)settings : FlurlHttp.GlobalSettings;
}
}
}

View File

@ -3,14 +3,13 @@ using System.Dynamic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Flurl.Http
{
/// <summary>
/// Async extension methods that can be chained off Task&lt;HttpResponseMessage&gt;, avoiding nested awaits.
/// </summary>
public static class ResponseMessageExtensions
public static class HttpResponseMessageExtensions
{
/// <summary>
/// Deserializes JSON-formatted HTTP response body to object of type T. Intended to chain off an async HTTP.
@ -19,8 +18,10 @@ namespace Flurl.Http
/// <returns>A Task whose result is an object containing data in the response body.</returns>
/// <example>x = await url.PostAsync(data).ReceiveJson&lt;T&gt;()</example>
public static async Task<T> ReceiveJson<T>(this Task<HttpResponseMessage> response) {
using (var stream = await response.ReceiveStream())
return JsonSerializer.CreateDefault().Deserialize<T>(stream);
var resp = await response;
var settings = resp.RequestMessage.GetFlurlSettings();
using (var stream = await resp.Content.ReadAsStreamAsync())
return settings.JsonSerializer.Deserialize<T>(stream);
}
/// <summary>

View File

@ -1,19 +0,0 @@
using System.IO;
using Newtonsoft.Json;
namespace Flurl.Http
{
public static class JsonExtensions
{
/// <summary>
/// Deserializes the Json structure contained by the specified Stream into an instance of the specified type.
/// </summary>
public static T Deserialize<T>(this JsonSerializer serializer, Stream stream) {
// http://james.newtonking.com/json/help/index.html?topic=html/Performance.htm
using (var sr = new StreamReader(stream))
using (var jr = new JsonTextReader(sr)) {
return serializer.Deserialize<T>(jr);
}
}
}
}

View File

@ -19,9 +19,9 @@ namespace Flurl.Http.Testing
};
public HttpTest() {
FlurlHttp.Configure(opts => {
opts.HttpClientFactory = new TestHttpClientFactory(this);
opts.AfterCall = call => CallLog.Add(call);
FlurlHttp.Configure(settings => {
settings.HttpClientFactory = new TestHttpClientFactory(this);
settings.AfterCall = call => CallLog.Add(call);
});
ResponseQueue = new Queue<HttpResponseMessage>();
CallLog = new List<HttpCall>();
@ -51,7 +51,7 @@ namespace Flurl.Http.Testing
public HttpTest RespondWithJson(int status, object data) {
ResponseQueue.Enqueue(new HttpResponseMessage {
StatusCode = (HttpStatusCode)status,
Content = new CapturedJsonContent(data)
Content = new CapturedJsonContent(FlurlHttp.GlobalSettings.JsonSerializer.Serialize(data))
});
return this;
}
@ -103,7 +103,7 @@ namespace Flurl.Http.Testing
}
public void Dispose() {
FlurlHttp.Configuration.ResetDefaults();
FlurlHttp.GlobalSettings.ResetDefaults();
}
}
}

View File

@ -16,7 +16,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Http\RealHttpTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Http\FlurlClientTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Http\GetTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Http\GlobalConfigTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Http\SettingsTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Http\HeadTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Http\HttpTestFixtureBase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Http\PostTests.cs" />

View File

@ -111,7 +111,7 @@ namespace Flurl.Test.Http
// allow 4xx
var client = "http://www.api.com".AllowHttpStatus("4xx");
// but then disallow it
client.AllowedHttpStatusRanges.Clear();
client.Settings.AllowedHttpStatusRange = null;
await client.GetAsync();
}
}

View File

@ -15,7 +15,7 @@ namespace Flurl.Test.Http
public ClientLifetimeTests() {
_fac = new TestHttpClientFactoryWithCounter();
FlurlHttp.Configure(opts => opts.HttpClientFactory = _fac);
FlurlHttp.Configure(settings => settings.HttpClientFactory = _fac);
}
[SetUp]

View File

@ -13,12 +13,12 @@ namespace Flurl.Test.Http
{
[TearDown]
public void ResetDefaults() {
FlurlHttp.Configuration.ResetDefaults();
FlurlHttp.GlobalSettings.ResetDefaults();
}
[Test]
public void can_provide_custom_httpclient_factory() {
FlurlHttp.Configuration.HttpClientFactory = new SomeCustomHttpClientFactory();
FlurlHttp.GlobalSettings.HttpClientFactory = new SomeCustomHttpClientFactory();
var client = new FlurlClient("http://www.api.com");
Assert.IsInstanceOf<SomeCustomHttpClient>(client.HttpClient);
@ -27,8 +27,8 @@ namespace Flurl.Test.Http
[Test]
public async Task can_allow_non_success_status() {
FlurlHttp.Configuration.AllowedHttpStatusRange = "4xx";
using (var test = new HttpTest()) {
FlurlHttp.GlobalSettings.AllowedHttpStatusRange = "4xx";
test.RespondWith(418, "I'm a teapot");
try {
var result = await "http://www.api.com".GetAsync();
@ -45,7 +45,7 @@ namespace Flurl.Test.Http
var callbackCalled = false;
using (var test = new HttpTest()) {
test.RespondWith("ok");
FlurlHttp.Configuration.BeforeCall = req => {
FlurlHttp.GlobalSettings.BeforeCall = req => {
CollectionAssert.IsNotEmpty(test.ResponseQueue); // verifies that callback is running before HTTP call is made
callbackCalled = true;
};
@ -60,7 +60,7 @@ namespace Flurl.Test.Http
var callbackCalled = false;
using (var test = new HttpTest()) {
test.RespondWith("ok");
FlurlHttp.Configuration.AfterCall = call => {
FlurlHttp.GlobalSettings.AfterCall = call => {
CollectionAssert.IsEmpty(test.ResponseQueue); // verifies that callback is running after HTTP call is made
callbackCalled = true;
};
@ -76,7 +76,7 @@ namespace Flurl.Test.Http
var callbackCalled = false;
using (var test = new HttpTest()) {
test.RespondWith(500, "server error");
FlurlHttp.Configuration.OnError = call => {
FlurlHttp.GlobalSettings.OnError = call => {
CollectionAssert.IsEmpty(test.ResponseQueue); // verifies that callback is running after HTTP call is made
callbackCalled = true;
call.ExceptionHandled = markExceptionHandled;
@ -96,22 +96,33 @@ namespace Flurl.Test.Http
[Test]
public async Task can_disable_exception_behavior() {
FlurlHttp.Configuration.OnError = call => {
call.ExceptionHandled = true;
};
using (var test = new HttpTest()) {
FlurlHttp.GlobalSettings.OnError = call => {
call.ExceptionHandled = true;
};
test.RespondWith(500, "server error");
try {
var result = await "http://www.api.com".GetAsync();
Assert.IsFalse(result.IsSuccessStatusCode);
}
catch (Exception) {
Assert.Fail("Exception should not have been thrown.");
catch (FlurlHttpException) {
Assert.Fail("Flurl should not have thrown exception.");
}
}
}
[Test]
public async Task client_can_override_global_settings() {
var overridden = false;
using (new HttpTest()) {
FlurlHttp.GlobalSettings.AfterCall = _ => overridden = false;
var fc = new FlurlClient("http://www.api.com");
fc.Settings.AfterCall = _ => overridden = true;
await fc.GetAsync();
Assert.True(overridden);
}
}
private class SomeCustomHttpClientFactory : IHttpClientFactory
{
public HttpClient CreateClient(Url url, HttpMessageHandler handler) {