Skip to content

Commit 21992a4

Browse files
committed
Removed unnecessary abstraction
1 parent 07a2cec commit 21992a4

File tree

7 files changed

+83
-73
lines changed

7 files changed

+83
-73
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using NUnit.Framework;
2+
3+
namespace RestSharp.Tests
4+
{
5+
[TestFixture]
6+
public class RequestConfiguratorTests
7+
{
8+
[Test]
9+
public void ConfiguresTheHttpProtocolVersion()
10+
{
11+
bool executed = false;
12+
13+
var restClient = new RestClient("http://localhost");
14+
restClient.ConfigureWebRequest(r => executed = true);
15+
16+
var req = new RestRequest("bob", Method.GET);
17+
18+
restClient.Execute(req);
19+
20+
Assert.IsTrue(executed);
21+
}
22+
}
23+
}

RestSharp/Http.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace RestSharp
3333
/// <summary>
3434
/// HttpWebRequest wrapper
3535
/// </summary>
36-
public partial class Http : IHttp, IHttpFactory
36+
public partial class Http : IHttp
3737
{
3838
private const string LINE_BREAK = "\r\n";
3939

@@ -233,10 +233,7 @@ public Http()
233233
/// Creates an IHttp
234234
/// </summary>
235235
/// <returns></returns>
236-
public IHttp Create()
237-
{
238-
return new Http();
239-
}
236+
public static IHttp Create() => new Http();
240237

241238
protected virtual HttpWebRequest CreateWebRequest(Uri url)
242239
{

RestSharp/IHttpFactory.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

RestSharp/RestClient.Async.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,8 @@ private RestRequestAsyncHandle ExecuteAsync(IRestRequest request,
326326
Action<IRestResponse, RestRequestAsyncHandle> callback, string httpMethod,
327327
Func<IHttp, Action<HttpResponse>, string, HttpWebRequest> getWebRequest)
328328
{
329-
var http = HttpFactory.Create();
330-
331329
AuthenticateIfNeeded(this, request);
332-
ConfigureHttp(request, http);
330+
var http = ConfigureHttp(request);
333331

334332
var asyncHandle = new RestRequestAsyncHandle();
335333
Action<HttpResponse> responseCb = r => ProcessResponse(request, r, asyncHandle, callback);

RestSharp/RestClient.Sync.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ private IRestResponse Execute(IRestRequest request, string httpMethod,
9595

9696
try
9797
{
98-
var http = HttpFactory.Create();
99-
100-
ConfigureHttp(request, http);
98+
var http = ConfigureHttp(request);
10199

102100
response = ConvertToRestResponse(request, getResponse(http, httpMethod));
103101
response.Request = request;

RestSharp/RestClient.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ public partial class RestClient : IRestClient
4545
private static readonly Regex StructuredSyntaxSuffixWildcardRegex =
4646
new Regex(@"^\*\+\w+$", RegexOptions.Compiled);
4747

48-
public IHttpFactory HttpFactory = new SimpleFactory<Http>();
49-
5048
/// <summary>
5149
/// Default constructor that registers default content handlers
5250
/// </summary>
@@ -421,8 +419,10 @@ private static string EncodeParameter(Parameter parameter, Encoding encoding) =>
421419
private static readonly ParameterType[] MultiParameterTypes =
422420
{ParameterType.QueryString, ParameterType.GetOrPost};
423421

424-
private void ConfigureHttp(IRestRequest request, IHttp http)
422+
private IHttp ConfigureHttp(IRestRequest request)
425423
{
424+
var http = Http.Create();
425+
426426
http.Encoding = Encoding;
427427
http.AlwaysMultipartFormData = request.AlwaysMultipartFormData;
428428
http.UseDefaultCredentials = request.UseDefaultCredentials;
@@ -488,7 +488,7 @@ private void ConfigureHttp(IRestRequest request, IHttp http)
488488
http.MaxRedirects = MaxRedirects;
489489
http.CachePolicy = CachePolicy;
490490
http.Pipelined = Pipelined;
491-
491+
492492
if (request.Credentials != null)
493493
http.Credentials = request.Credentials;
494494

@@ -579,6 +579,8 @@ private void ConfigureHttp(IRestRequest request, IHttp http)
579579
#endif
580580

581581
http.RemoteCertificateValidationCallback = RemoteCertificateValidationCallback;
582+
583+
return http;
582584
}
583585

584586
private static RestResponse ConvertToRestResponse(IRestRequest request, HttpResponse httpResponse)

RestSharp/RestRequest.cs

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ namespace RestSharp
3131
/// </summary>
3232
public class RestRequest : IRestRequest
3333
{
34+
/// <summary>
35+
/// Local list of Allowed Decompresison Methods
36+
/// </summary>
37+
private readonly IList<DecompressionMethods> alloweDecompressionMethods;
38+
3439
/// <summary>
3540
/// Default constructor
3641
/// </summary>
@@ -96,25 +101,20 @@ public RestRequest(Uri resource, Method method)
96101
//resource.PathAndQuery not supported by Silverlight :(
97102
}
98103

99-
/// <summary>
100-
/// Local list of Allowed Decompresison Methods
101-
/// </summary>
102-
private readonly IList<DecompressionMethods> alloweDecompressionMethods;
103-
104-
/// <summary>
105-
/// List of Allowed Decompresison Methods
106-
/// </summary>
107-
public IList<DecompressionMethods> AllowedDecompressionMethods =>
108-
alloweDecompressionMethods.Any()
109-
? alloweDecompressionMethods
110-
: new[] { DecompressionMethods.None, DecompressionMethods.Deflate, DecompressionMethods.GZip };
111-
112104
/// <summary>
113105
/// Gets or sets a user-defined state object that contains information about a request and which can be later
114106
/// retrieved when the request completes.
115107
/// </summary>
116108
public object UserState { get; set; }
117109

110+
/// <summary>
111+
/// List of Allowed Decompresison Methods
112+
/// </summary>
113+
public IList<DecompressionMethods> AllowedDecompressionMethods =>
114+
alloweDecompressionMethods.Any()
115+
? alloweDecompressionMethods
116+
: new[] {DecompressionMethods.None, DecompressionMethods.Deflate, DecompressionMethods.GZip};
117+
118118
/// <summary>
119119
/// Always send a multipart/form-data request - even when no Files are present.
120120
/// </summary>
@@ -465,7 +465,8 @@ public IRestRequest AddParameter(string name, object value, string contentType,
465465
}
466466

467467
/// <summary>
468-
/// Adds a parameter to the request or updates it with the given argument, if the parameter already exists in the request
468+
/// Adds a parameter to the request or updates it with the given argument, if the parameter already exists in the
469+
/// request
469470
/// </summary>
470471
/// <param name="p">Parameter to add</param>
471472
/// <returns></returns>
@@ -483,8 +484,9 @@ public IRestRequest AddOrUpdateParameter(Parameter p)
483484
}
484485

485486
/// <summary>
486-
/// Adds a HTTP parameter to the request or updates it with the given argument, if the parameter already exists in the request
487-
/// (QueryString for GET, DELETE, OPTIONS and HEAD; Encoded form for POST and PUT)
487+
/// Adds a HTTP parameter to the request or updates it with the given argument, if the parameter already exists in the
488+
/// request
489+
/// (QueryString for GET, DELETE, OPTIONS and HEAD; Encoded form for POST and PUT)
488490
/// </summary>
489491
/// <param name="name">Name of the parameter</param>
490492
/// <param name="value">Value of the parameter</param>
@@ -499,33 +501,37 @@ public IRestRequest AddOrUpdateParameter(string name, object value)
499501
});
500502
}
501503

504+
/// <inheritdoc />
502505
/// <summary>
503-
/// Adds a HTTP parameter to the request or updates it with the given argument, if the parameter already exists in the request
504-
/// - GetOrPost: Either a QueryString value or encoded form value based on method
505-
/// - HttpHeader: Adds the name/value pair to the HTTP request's Headers collection
506-
/// - UrlSegment: Inserted into URL if there is a matching url token e.g. {AccountId}
507-
/// - RequestBody: Used by AddBody() (not recommended to use directly)
506+
/// Adds a HTTP parameter to the request or updates it with the given argument, if the parameter already exists in the
507+
/// request
508+
/// - GetOrPost: Either a QueryString value or encoded form value based on method
509+
/// - HttpHeader: Adds the name/value pair to the HTTP request's Headers collection
510+
/// - UrlSegment: Inserted into URL if there is a matching url token e.g. {AccountId}
511+
/// - RequestBody: Used by AddBody() (not recommended to use directly)
508512
/// </summary>
509513
/// <param name="name">Name of the parameter</param>
510514
/// <param name="value">Value of the parameter</param>
511515
/// <param name="type">The type of parameter to add</param>
512516
/// <returns>This request</returns>
513517
public IRestRequest AddOrUpdateParameter(string name, object value, ParameterType type)
514518
{
515-
return AddOrUpdateParameter(new Parameter
516-
{
517-
Name = name,
518-
Value = value,
519-
Type = type
520-
});
519+
return AddOrUpdateParameter(
520+
new Parameter
521+
{
522+
Name = name,
523+
Value = value,
524+
Type = type
525+
});
521526
}
522527

523528
/// <summary>
524-
/// Adds a HTTP parameter to the request or updates it with the given argument, if the parameter already exists in the request
525-
/// - GetOrPost: Either a QueryString value or encoded form value based on method
526-
/// - HttpHeader: Adds the name/value pair to the HTTP request's Headers collection
527-
/// - UrlSegment: Inserted into URL if there is a matching url token e.g. {AccountId}
528-
/// - RequestBody: Used by AddBody() (not recommended to use directly)
529+
/// Adds a HTTP parameter to the request or updates it with the given argument, if the parameter already exists in the
530+
/// request
531+
/// - GetOrPost: Either a QueryString value or encoded form value based on method
532+
/// - HttpHeader: Adds the name/value pair to the HTTP request's Headers collection
533+
/// - UrlSegment: Inserted into URL if there is a matching url token e.g. {AccountId}
534+
/// - RequestBody: Used by AddBody() (not recommended to use directly)
529535
/// </summary>
530536
/// <param name="name">Name of the parameter</param>
531537
/// <param name="value">Value of the parameter</param>
@@ -543,6 +549,7 @@ public IRestRequest AddOrUpdateParameter(string name, object value, string conte
543549
});
544550
}
545551

552+
/// <inheritdoc />
546553
/// <summary>
547554
/// Shortcut to AddParameter(name, value, HttpHeader) overload
548555
/// </summary>
@@ -552,14 +559,18 @@ public IRestRequest AddOrUpdateParameter(string name, object value, string conte
552559
public IRestRequest AddHeader(string name, string value)
553560
{
554561
const string portSplit = @":\d+";
555-
Func<string, bool> invalidHost =
556-
host => Uri.CheckHostName(Regex.Split(host, portSplit)[0]) == UriHostNameType.Unknown;
557562

558-
if (name == "Host" && invalidHost(value))
563+
bool InvalidHost(string host)
564+
{
565+
return Uri.CheckHostName(Regex.Split(host, portSplit)[0]) == UriHostNameType.Unknown;
566+
}
567+
568+
if (name == "Host" && InvalidHost(value))
559569
throw new ArgumentException("The specified value is not a valid Host header string.", "value");
560570
return AddParameter(name, value, ParameterType.HttpHeader);
561571
}
562572

573+
/// <inheritdoc />
563574
/// <summary>
564575
/// Shortcut to AddParameter(name, value, Cookie) overload
565576
/// </summary>
@@ -594,17 +605,15 @@ public IRestRequest AddQueryParameter(string name, string value)
594605
}
595606

596607
/// <summary>
597-
/// Add a Decompression Method to the request
608+
/// Add a Decompression Method to the request
598609
/// </summary>
599610
/// <param name="decompressionMethod">None | GZip | Deflate</param>
600611
/// <returns></returns>
601612
public IRestRequest AddDecompressionMethod(DecompressionMethods decompressionMethod)
602613
{
603-
if (!this.alloweDecompressionMethods.Contains(decompressionMethod))
604-
{
605-
this.alloweDecompressionMethods.Add(decompressionMethod);
606-
}
607-
614+
if (!alloweDecompressionMethods.Contains(decompressionMethod))
615+
alloweDecompressionMethods.Add(decompressionMethod);
616+
608617
return this;
609618
}
610619

0 commit comments

Comments
 (0)