1
+ // Copyright © 2009-2021 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+
16
+ using System . Net ;
17
+ using RestSharp . Extensions ;
18
+
19
+ namespace RestSharp ;
20
+
21
+ public static partial class RestClientExtensions {
22
+ /// <summary>
23
+ /// Calls the URL specified in the <code>resource</code> parameter, expecting a JSON response back. Deserializes and returns the response.
24
+ /// </summary>
25
+ /// <param name="client">RestClient instance</param>
26
+ /// <param name="resource">Resource URL</param>
27
+ /// <typeparam name="TResponse">Response object type</typeparam>
28
+ /// <returns>Deserialized response object</returns>
29
+ public static TResponse ? GetJson < TResponse > (
30
+ this RestClient client ,
31
+ string resource )
32
+ => AsyncHelpers . RunSync ( ( ) => client . GetJsonAsync < TResponse > ( resource ) ) ;
33
+
34
+ /// <summary>
35
+ /// Calls the URL specified in the <code>resource</code> parameter, expecting a JSON response back. Deserializes and returns the response.
36
+ /// </summary>
37
+ /// <param name="client">RestClient instance</param>
38
+ /// <param name="resource">Resource URL</param>
39
+ /// <param name="parameters">Parameters to pass to the request</param>
40
+ /// <typeparam name="TResponse">Response object type</typeparam>
41
+ /// <returns>Deserialized response object</returns>
42
+ public static TResponse ? GetJson < TResponse > (
43
+ this RestClient client ,
44
+ string resource ,
45
+ object parameters )
46
+ => AsyncHelpers . RunSync ( ( ) => client . GetJsonAsync < TResponse > ( resource , parameters ) ) ;
47
+
48
+ /// <summary>
49
+ /// Serializes the <code>request</code> object to JSON and makes a POST call to the resource specified in the <code>resource</code> parameter.
50
+ /// Expects a JSON response back, deserializes it to <code>TResponse</code> type and returns it.
51
+ /// </summary>
52
+ /// <param name="client">RestClient instance</param>
53
+ /// <param name="resource">Resource URL</param>
54
+ /// <param name="request">Request object, must be serializable to JSON</param>
55
+ /// <typeparam name="TRequest">Request object type</typeparam>
56
+ /// <typeparam name="TResponse">Response object type</typeparam>
57
+ /// <returns>Deserialized response object</returns>
58
+ public static TResponse ? PostJson < TRequest , TResponse > (
59
+ this RestClient client ,
60
+ string resource ,
61
+ TRequest request
62
+ ) where TRequest : class
63
+ => AsyncHelpers . RunSync ( ( ) => client . PostJsonAsync < TRequest , TResponse > ( resource , request ) ) ;
64
+
65
+ /// <summary>
66
+ /// Serializes the <code>request</code> object to JSON and makes a POST call to the resource specified in the <code>resource</code> parameter.
67
+ /// Expects no response back, just the status code.
68
+ /// </summary>
69
+ /// <param name="client">RestClient instance</param>
70
+ /// <param name="resource">Resource URL</param>
71
+ /// <param name="request">Request object, must be serializable to JSON</param>
72
+ /// <typeparam name="TRequest">Request object type</typeparam>
73
+ /// <returns>Response status code</returns>
74
+ public static HttpStatusCode PostJson < TRequest > (
75
+ this RestClient client ,
76
+ string resource ,
77
+ TRequest request
78
+ ) where TRequest : class
79
+ => AsyncHelpers . RunSync ( ( ) => client . PostJsonAsync ( resource , request ) ) ;
80
+
81
+ /// <summary>
82
+ /// Serializes the <code>request</code> object to JSON and makes a PUT call to the resource specified in the <code>resource</code> parameter.
83
+ /// Expects a JSON response back, deserializes it to <code>TResponse</code> type and returns it.
84
+ /// </summary>
85
+ /// <param name="client">RestClient instance</param>
86
+ /// <param name="resource">Resource URL</param>
87
+ /// <param name="request">Request object, must be serializable to JSON</param>
88
+ /// <typeparam name="TRequest">Request object type</typeparam>
89
+ /// <typeparam name="TResponse">Response object type</typeparam>
90
+ /// <returns>Deserialized response object</returns>
91
+ public static TResponse ? PutJson < TRequest , TResponse > (
92
+ this RestClient client ,
93
+ string resource ,
94
+ TRequest request
95
+ ) where TRequest : class
96
+ => AsyncHelpers . RunSync ( ( ) => client . PutJsonAsync < TRequest , TResponse > ( resource , request ) ) ;
97
+
98
+ /// <summary>
99
+ /// Serializes the <code>request</code> object to JSON and makes a PUT call to the resource specified in the <code>resource</code> parameter.
100
+ /// Expects no response back, just the status code.
101
+ /// </summary>
102
+ /// <param name="client">RestClient instance</param>
103
+ /// <param name="resource">Resource URL</param>
104
+ /// <param name="request">Request object, must be serializable to JSON</param>
105
+ /// <typeparam name="TRequest">Request object type</typeparam>
106
+ /// <returns>Response status code</returns>
107
+ public static HttpStatusCode PutJson < TRequest > (
108
+ this RestClient client ,
109
+ string resource ,
110
+ TRequest request
111
+ ) where TRequest : class
112
+ => AsyncHelpers . RunSync ( ( ) => client . PutJsonAsync ( resource , request ) ) ;
113
+ }
0 commit comments