Skip to content

Commit d70f9c9

Browse files
committed
Change methods to be extension methods
Makes implementing a custom IRestClient easier since these methods don't need to be reimplemented. They don't need access to any private methods or fields.
1 parent db44f42 commit d70f9c9

File tree

3 files changed

+81
-70
lines changed

3 files changed

+81
-70
lines changed

RestSharp/RestClient.cs

Lines changed: 2 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,13 @@
1616

1717
using System;
1818
using System.Collections.Generic;
19-
using System.IO;
2019
using System.Linq;
2120
using System.Net;
22-
using System.Xml;
23-
using System.Xml.Linq;
21+
using System.Reflection;
2422
using System.Security.Cryptography.X509Certificates;
23+
using System.Text;
2524
using RestSharp.Deserializers;
2625
using RestSharp.Extensions;
27-
using System.Text;
28-
using System.Reflection;
2926

3027
namespace RestSharp
3128
{
@@ -85,71 +82,6 @@ public RestClient(string baseUrl)
8582
/// </summary>
8683
public IList<Parameter> DefaultParameters { get; private set; }
8784

88-
/// <summary>
89-
/// Add a parameter to use on every request made with this client instance
90-
/// </summary>
91-
/// <param name="p">Parameter to add</param>
92-
/// <returns></returns>
93-
public void AddDefaultParameter(Parameter p)
94-
{
95-
if (p.Type == ParameterType.RequestBody)
96-
{
97-
throw new NotSupportedException("Cannot set request body from default headers. Use Request.AddBody() instead.");
98-
}
99-
100-
DefaultParameters.Add(p);
101-
}
102-
103-
/// <summary>
104-
/// Adds a HTTP parameter (QueryString for GET, DELETE, OPTIONS and HEAD; Encoded form for POST and PUT)
105-
/// Used on every request made by this client instance
106-
/// </summary>
107-
/// <param name="name">Name of the parameter</param>
108-
/// <param name="value">Value of the parameter</param>
109-
/// <returns>This request</returns>
110-
public void AddDefaultParameter(string name, object value)
111-
{
112-
AddDefaultParameter(new Parameter { Name = name, Value = value, Type = ParameterType.GetOrPost });
113-
}
114-
115-
/// <summary>
116-
/// Adds a parameter to the request. There are four types of parameters:
117-
/// - GetOrPost: Either a QueryString value or encoded form value based on method
118-
/// - HttpHeader: Adds the name/value pair to the HTTP request's Headers collection
119-
/// - UrlSegment: Inserted into URL if there is a matching url token e.g. {AccountId}
120-
/// - RequestBody: Used by AddBody() (not recommended to use directly)
121-
/// </summary>
122-
/// <param name="name">Name of the parameter</param>
123-
/// <param name="value">Value of the parameter</param>
124-
/// <param name="type">The type of parameter to add</param>
125-
/// <returns>This request</returns>
126-
public void AddDefaultParameter(string name, object value, ParameterType type)
127-
{
128-
AddDefaultParameter(new Parameter { Name = name, Value = value, Type = type });
129-
}
130-
131-
/// <summary>
132-
/// Shortcut to AddDefaultParameter(name, value, HttpHeader) overload
133-
/// </summary>
134-
/// <param name="name">Name of the header to add</param>
135-
/// <param name="value">Value of the header to add</param>
136-
/// <returns></returns>
137-
public void AddDefaultHeader(string name, string value)
138-
{
139-
AddDefaultParameter(name, value, ParameterType.HttpHeader);
140-
}
141-
142-
/// <summary>
143-
/// Shortcut to AddDefaultParameter(name, value, UrlSegment) overload
144-
/// </summary>
145-
/// <param name="name">Name of the segment to add</param>
146-
/// <param name="value">Value of the segment to add</param>
147-
/// <returns></returns>
148-
public void AddDefaultUrlSegment(string name, string value)
149-
{
150-
AddDefaultParameter(name, value, ParameterType.UrlSegment);
151-
}
152-
15385
/// <summary>
15486
/// Registers a content handler to process response content
15587
/// </summary>

RestSharp/RestClientExtensions.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
3+
namespace RestSharp
4+
{
5+
public static class RestClientExtensions
6+
{
7+
/// <summary>
8+
/// Add a parameter to use on every request made with this client instance
9+
/// </summary>
10+
/// <param name="restClient">The IRestClient instance</param>
11+
/// <param name="p">Parameter to add</param>
12+
/// <returns></returns>
13+
public static void AddDefaultParameter(this IRestClient restClient, Parameter p)
14+
{
15+
if (p.Type == ParameterType.RequestBody)
16+
{
17+
throw new NotSupportedException(
18+
"Cannot set request body from default headers. Use Request.AddBody() instead.");
19+
}
20+
21+
restClient.DefaultParameters.Add(p);
22+
}
23+
24+
/// <summary>
25+
/// Adds a HTTP parameter (QueryString for GET, DELETE, OPTIONS and HEAD; Encoded form for POST and PUT)
26+
/// Used on every request made by this client instance
27+
/// </summary>
28+
/// <param name="restClient">The IRestClient instance</param>
29+
/// <param name="name">Name of the parameter</param>
30+
/// <param name="value">Value of the parameter</param>
31+
/// <returns>This request</returns>
32+
public static void AddDefaultParameter(this IRestClient restClient, string name, object value)
33+
{
34+
restClient.AddDefaultParameter(new Parameter { Name = name, Value = value, Type = ParameterType.GetOrPost });
35+
}
36+
37+
/// <summary>
38+
/// Adds a parameter to the request. There are four types of parameters:
39+
/// - GetOrPost: Either a QueryString value or encoded form value based on method
40+
/// - HttpHeader: Adds the name/value pair to the HTTP request's Headers collection
41+
/// - UrlSegment: Inserted into URL if there is a matching url token e.g. {AccountId}
42+
/// - RequestBody: Used by AddBody() (not recommended to use directly)
43+
/// </summary>
44+
/// <param name="restClient">The IRestClient instance</param>
45+
/// <param name="name">Name of the parameter</param>
46+
/// <param name="value">Value of the parameter</param>
47+
/// <param name="type">The type of parameter to add</param>
48+
/// <returns>This request</returns>
49+
public static void AddDefaultParameter(this IRestClient restClient, string name, object value, ParameterType type)
50+
{
51+
restClient.AddDefaultParameter(new Parameter { Name = name, Value = value, Type = type });
52+
}
53+
54+
/// <summary>
55+
/// Shortcut to AddDefaultParameter(name, value, HttpHeader) overload
56+
/// </summary>
57+
/// <param name="restClient">The IRestClient instance</param>
58+
/// <param name="name">Name of the header to add</param>
59+
/// <param name="value">Value of the header to add</param>
60+
/// <returns></returns>
61+
public static void AddDefaultHeader(this IRestClient restClient, string name, string value)
62+
{
63+
restClient.AddDefaultParameter(name, value, ParameterType.HttpHeader);
64+
}
65+
66+
/// <summary>
67+
/// Shortcut to AddDefaultParameter(name, value, UrlSegment) overload
68+
/// </summary>
69+
/// <param name="restClient">The IRestClient instance</param>
70+
/// <param name="name">Name of the segment to add</param>
71+
/// <param name="value">Value of the segment to add</param>
72+
/// <returns></returns>
73+
public static void AddDefaultUrlSegment(this IRestClient restClient, string name, string value)
74+
{
75+
restClient.AddDefaultParameter(name, value, ParameterType.UrlSegment);
76+
}
77+
}
78+
}

RestSharp/RestSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
<Compile Include="Extensions\MonoHttp\Helpers.cs" />
108108
<Compile Include="Extensions\MonoHttp\HtmlEncoder.cs" />
109109
<Compile Include="Extensions\MonoHttp\HttpUtility.cs" />
110+
<Compile Include="RestClientExtensions.cs" />
110111
<Compile Include="Extensions\StringExtensions.cs" />
111112
<Compile Include="Http.Sync.cs" />
112113
<Compile Include="HttpCookie.cs" />

0 commit comments

Comments
 (0)