@@ -66,34 +66,106 @@ public static RestRequest AddOrUpdateParameter(this RestRequest request, string
66
66
public static RestRequest AddOrUpdateParameter < T > ( this RestRequest request , string name , T value , bool encode = true ) where T : struct
67
67
=> request . AddOrUpdateParameter ( name , value . ToString ( ) , encode ) ;
68
68
69
+ /// <summary>
70
+ /// Adds a URL segment parameter to the request. The resource URL must have a placeholder for the parameter for it to work.
71
+ /// For example, if you add a URL segment parameter with the name "id", the resource URL should contain {id} in its path.
72
+ /// </summary>
73
+ /// <param name="request">Request instance</param>
74
+ /// <param name="name">Name of the parameter, must be matching a placeholder in the resource URL as {name}</param>
75
+ /// <param name="value">Value of the parameter</param>
76
+ /// <param name="encode">Encode the value or not, default true</param>
77
+ /// <returns></returns>
69
78
public static RestRequest AddUrlSegment ( this RestRequest request , string name , string value , bool encode = true )
70
79
=> request . AddParameter ( new UrlSegmentParameter ( name , value , encode ) ) ;
71
80
81
+ /// <summary>
82
+ /// Adds a URL segment parameter to the request. The resource URL must have a placeholder for the parameter for it to work.
83
+ /// For example, if you add a URL segment parameter with the name "id", the resource URL should contain {id} in its path.
84
+ /// </summary>
85
+ /// <param name="request">Request instance</param>
86
+ /// <param name="name">Name of the parameter, must be matching a placeholder in the resource URL as {name}</param>
87
+ /// <param name="value">Value of the parameter</param>
88
+ /// <param name="encode">Encode the value or not, default true</param>
89
+ /// <returns></returns>
72
90
public static RestRequest AddUrlSegment < T > ( this RestRequest request , string name , T value , bool encode = true ) where T : struct
73
91
=> request . AddUrlSegment ( name , Ensure . NotNull ( value . ToString ( ) , nameof ( value ) ) , encode ) ;
74
92
93
+ /// <summary>
94
+ /// Adds a query string parameter to the request. The request resource should not contain any placeholders for this parameter.
95
+ /// The parameter will be added to the request URL as a query string using name=value format.
96
+ /// </summary>
97
+ /// <param name="request">Request instance</param>
98
+ /// <param name="name">Parameter name</param>
99
+ /// <param name="value">Parameter value</param>
100
+ /// <param name="encode">Encode the value or not, default true</param>
101
+ /// <returns></returns>
75
102
public static RestRequest AddQueryParameter ( this RestRequest request , string name , string ? value , bool encode = true )
76
103
=> request . AddParameter ( new QueryParameter ( name , value , encode ) ) ;
77
104
105
+ /// <summary>
106
+ /// Adds a query string parameter to the request. The request resource should not contain any placeholders for this parameter.
107
+ /// The parameter will be added to the request URL as a query string using name=value format.
108
+ /// </summary>
109
+ /// <param name="request">Request instance</param>
110
+ /// <param name="name">Parameter name</param>
111
+ /// <param name="value">Parameter value</param>
112
+ /// <param name="encode">Encode the value or not, default true</param>
113
+ /// <returns></returns>
78
114
public static RestRequest AddQueryParameter < T > ( this RestRequest request , string name , T value , bool encode = true ) where T : struct
79
115
=> request . AddQueryParameter ( name , value . ToString ( ) , encode ) ;
80
116
117
+ /// <summary>
118
+ /// Adds a header to the request. RestSharp will try to separate request and content headers when calling the resource.
119
+ /// </summary>
120
+ /// <param name="request">Request instance</param>
121
+ /// <param name="name">Header name</param>
122
+ /// <param name="value">Header value</param>
123
+ /// <returns></returns>
81
124
public static RestRequest AddHeader ( this RestRequest request , string name , string value ) {
82
125
CheckAndThrowsForInvalidHost ( name , value ) ;
83
126
return request . AddParameter ( new HeaderParameter ( name , value ) ) ;
84
127
}
85
128
129
+ /// <summary>
130
+ /// Adds a header to the request. RestSharp will try to separate request and content headers when calling the resource.
131
+ /// </summary>
132
+ /// <param name="request">Request instance</param>
133
+ /// <param name="name">Header name</param>
134
+ /// <param name="value">Header value</param>
135
+ /// <returns></returns>
86
136
public static RestRequest AddHeader < T > ( this RestRequest request , string name , T value ) where T : struct
87
137
=> request . AddHeader ( name , Ensure . NotNull ( value . ToString ( ) , nameof ( value ) ) ) ;
88
138
139
+ /// <summary>
140
+ /// Adds or updates the request header. RestSharp will try to separate request and content headers when calling the resource.
141
+ /// Existing header with the same name will be replaced.
142
+ /// </summary>
143
+ /// <param name="request">Request instance</param>
144
+ /// <param name="name">Header name</param>
145
+ /// <param name="value">Header value</param>
146
+ /// <returns></returns>
89
147
public static RestRequest AddOrUpdateHeader ( this RestRequest request , string name , string value ) {
90
148
CheckAndThrowsForInvalidHost ( name , value ) ;
91
149
return request . AddOrUpdateParameter ( new HeaderParameter ( name , value ) ) ;
92
150
}
93
151
152
+ /// <summary>
153
+ /// Adds or updates the request header. RestSharp will try to separate request and content headers when calling the resource.
154
+ /// Existing header with the same name will be replaced.
155
+ /// </summary>
156
+ /// <param name="request">Request instance</param>
157
+ /// <param name="name">Header name</param>
158
+ /// <param name="value">Header value</param>
159
+ /// <returns></returns>
94
160
public static RestRequest AddOrUpdateHeader < T > ( this RestRequest request , string name , T value ) where T : struct
95
161
=> request . AddOrUpdateHeader ( name , Ensure . NotNull ( value . ToString ( ) , nameof ( value ) ) ) ;
96
162
163
+ /// <summary>
164
+ /// Adds multiple headers to the request, using the key-value pairs provided.
165
+ /// </summary>
166
+ /// <param name="request">Request instance</param>
167
+ /// <param name="headers">Collection of key-value pairs, where key will be used as header name, and value as header value</param>
168
+ /// <returns></returns>
97
169
public static RestRequest AddHeaders ( this RestRequest request , ICollection < KeyValuePair < string , string > > headers ) {
98
170
CheckAndThrowsDuplicateKeys ( headers ) ;
99
171
@@ -104,6 +176,12 @@ public static RestRequest AddHeaders(this RestRequest request, ICollection<KeyVa
104
176
return request ;
105
177
}
106
178
179
+ /// <summary>
180
+ /// Adds or updates multiple headers to the request, using the key-value pairs provided. Existing headers with the same name will be replaced.
181
+ /// </summary>
182
+ /// <param name="request">Request instance</param>
183
+ /// <param name="headers">Collection of key-value pairs, where key will be used as header name, and value as header value</param>
184
+ /// <returns></returns>
107
185
public static RestRequest AddOrUpdateHeaders ( this RestRequest request , ICollection < KeyValuePair < string , string > > headers ) {
108
186
CheckAndThrowsDuplicateKeys ( headers ) ;
109
187
@@ -114,9 +192,42 @@ public static RestRequest AddOrUpdateHeaders(this RestRequest request, ICollecti
114
192
return request ;
115
193
}
116
194
195
+ /// <summary>
196
+ /// Adds a parameter of a given type to the request. It will create a typed parameter instance based on the type argument.
197
+ /// It is not recommended to use this overload unless you must, as it doesn't provide any restrictions, and if the name-value-type
198
+ /// combination doesn't match, it will throw.
199
+ /// </summary>
200
+ /// <param name="request">Request instance</param>
201
+ /// <param name="name">Name of the parameter, must be matching a placeholder in the resource URL as {name}</param>
202
+ /// <param name="value">Value of the parameter</param>
203
+ /// <param name="type">Enum value specifying what kind of parameter is being added</param>
204
+ /// <param name="encode">Encode the value or not, default true</param>
205
+ /// <returns></returns>
117
206
public static RestRequest AddParameter ( this RestRequest request , string ? name , object value , ParameterType type , bool encode = true )
118
207
=> request . AddParameter ( Parameter . CreateParameter ( name , value , type , encode ) ) ;
119
208
209
+ /// <summary>
210
+ /// Adds or updates request parameter of a given type. It will create a typed parameter instance based on the type argument.
211
+ /// Parameter will be added or updated based on its name. If the request has a parameter with the same name, it will be updated.
212
+ /// It is not recommended to use this overload unless you must, as it doesn't provide any restrictions, and if the name-value-type
213
+ /// combination doesn't match, it will throw.
214
+ /// </summary>
215
+ /// <param name="request">Request instance</param>
216
+ /// <param name="name">Name of the parameter, must be matching a placeholder in the resource URL as {name}</param>
217
+ /// <param name="value">Value of the parameter</param>
218
+ /// <param name="type">Enum value specifying what kind of parameter is being added</param>
219
+ /// <param name="encode">Encode the value or not, default true</param>
220
+ /// <returns></returns>
221
+ public static RestRequest AddOrUpdateParameter ( this RestRequest request , string name , object value , ParameterType type , bool encode = true )
222
+ => request . AddOrUpdateParameter ( Parameter . CreateParameter ( name , value , type , encode ) ) ;
223
+
224
+ /// <summary>
225
+ /// Adds or updates request parameter, given the parameter instance, for example <see cref="QueryParameter"/> or <see cref="UrlSegmentParameter"/>.
226
+ /// It will replace an existing parameter with the same name.
227
+ /// </summary>
228
+ /// <param name="request">Request instance</param>
229
+ /// <param name="parameter">Parameter instance</param>
230
+ /// <returns></returns>
120
231
public static RestRequest AddOrUpdateParameter ( this RestRequest request , Parameter parameter ) {
121
232
var p = request . Parameters . FirstOrDefault ( x => x . Name == parameter . Name && x . Type == parameter . Type ) ;
122
233
@@ -126,16 +237,20 @@ public static RestRequest AddOrUpdateParameter(this RestRequest request, Paramet
126
237
return request ;
127
238
}
128
239
240
+ /// <summary>
241
+ /// Adds or updates multiple request parameters, given the parameter instance, for example
242
+ /// <see cref="QueryParameter"/> or <see cref="UrlSegmentParameter"/>. Parameters with the same name will be replaced.
243
+ /// </summary>
244
+ /// <param name="request">Request instance</param>
245
+ /// <param name="parameters">Collection of parameter instances</param>
246
+ /// <returns></returns>
129
247
public static RestRequest AddOrUpdateParameters ( this RestRequest request , IEnumerable < Parameter > parameters ) {
130
248
foreach ( var parameter in parameters )
131
249
request . AddOrUpdateParameter ( parameter ) ;
132
250
133
251
return request ;
134
252
}
135
253
136
- public static RestRequest AddOrUpdateParameter ( this RestRequest request , string name , object value , ParameterType type , bool encode = true )
137
- => request . AddOrUpdateParameter ( Parameter . CreateParameter ( name , value , type , encode ) ) ;
138
-
139
254
/// <summary>
140
255
/// Adds a file parameter to the request body. The file will be read from disk as a stream.
141
256
/// </summary>
@@ -159,15 +274,23 @@ public static RestRequest AddFile(this RestRequest request, string name, string
159
274
public static RestRequest AddFile ( this RestRequest request , string name , byte [ ] bytes , string filename , string ? contentType = null )
160
275
=> request . AddFile ( FileParameter . Create ( name , bytes , filename , contentType ) ) ;
161
276
277
+ /// <summary>
278
+ /// Adds a file attachment to the request, where the file content will be retrieved from a given stream
279
+ /// </summary>
280
+ /// <param name="request">Request instance</param>
281
+ /// <param name="name">Parameter name</param>
282
+ /// <param name="getFile">Function that returns a stream with the file content</param>
283
+ /// <param name="fileName">File name</param>
284
+ /// <param name="contentType">Optional: content type. Default is "application/octet-stream"</param>
285
+ /// <returns></returns>
162
286
public static RestRequest AddFile (
163
287
this RestRequest request ,
164
288
string name ,
165
289
Func < Stream > getFile ,
166
290
string fileName ,
167
- long contentLength ,
168
291
string ? contentType = null
169
292
)
170
- => request . AddFile ( FileParameter . Create ( name , getFile , contentLength , fileName , contentType ) ) ;
293
+ => request . AddFile ( FileParameter . Create ( name , getFile , fileName , contentType ) ) ;
171
294
172
295
/// <summary>
173
296
/// Adds a body parameter to the request
@@ -201,7 +324,7 @@ public static RestRequest AddBody(this RestRequest request, object obj, string?
201
324
/// <param name="obj">Object that will be serialized to JSON</param>
202
325
/// <param name="contentType">Optional: content type. Default is "application/json"</param>
203
326
/// <returns></returns>
204
- public static RestRequest AddJsonBody ( this RestRequest request , object obj , string contentType = ContentType . Json ) {
327
+ public static RestRequest AddJsonBody < T > ( this RestRequest request , T obj , string contentType = ContentType . Json ) where T : class {
205
328
request . RequestFormat = DataFormat . Json ;
206
329
return request . AddParameter ( new JsonParameter ( "" , obj , contentType ) ) ;
207
330
}
@@ -214,7 +337,8 @@ public static RestRequest AddJsonBody(this RestRequest request, object obj, stri
214
337
/// <param name="contentType">Optional: content type. Default is "application/xml"</param>
215
338
/// <param name="xmlNamespace">Optional: XML namespace</param>
216
339
/// <returns></returns>
217
- public static RestRequest AddXmlBody ( this RestRequest request , object obj , string contentType = ContentType . Xml , string xmlNamespace = "" ) {
340
+ public static RestRequest AddXmlBody < T > ( this RestRequest request , T obj , string contentType = ContentType . Xml , string xmlNamespace = "" )
341
+ where T : class {
218
342
request . RequestFormat = DataFormat . Xml ;
219
343
request . AddParameter ( new XmlParameter ( "" , obj , xmlNamespace , contentType ) ) ;
220
344
return request ;
@@ -227,7 +351,7 @@ public static RestRequest AddXmlBody(this RestRequest request, object obj, strin
227
351
/// <param name="obj">Object to add as form data</param>
228
352
/// <param name="includedProperties">Properties to include, or nothing to include everything</param>
229
353
/// <returns></returns>
230
- public static RestRequest AddObject ( this RestRequest request , object obj , params string [ ] includedProperties ) {
354
+ public static RestRequest AddObject < T > ( this RestRequest request , T obj , params string [ ] includedProperties ) where T : class {
231
355
var props = obj . GetProperties ( includedProperties ) ;
232
356
233
357
foreach ( var ( name , value ) in props ) {
0 commit comments