C# Client Library
A C# Client Library for the AnalyzeRe REST API
ExtensionMethods.Rest.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using RestSharp;
4 
5 namespace AnalyzeRe
6 {
7  // Extension Methods requiring an explicitly provided response type
8  public static partial class ExtensionMethods
9  {
10  #region Response Type Specified at Compile Time as Generic Method Type Parameter
11  public static T Get_AsType<T>(
21  this IAPIResource obj,
22  IEnumerable<Parameter> requestParameters = null,
23  int? timeout = null)
24  {
25  return API.RequestAndParse<T>(obj.Path, Method.GET, requestParameters, timeout);
26  }
27 
38  public static T GetSubResource<T>(
39  this IAPIResource obj,
40  string subResource,
41  IEnumerable<Parameter> requestParameters = null,
42  int? timeout = null)
43  {
44  return API.RequestAndParse<T>(GetSubResourcePath(subResource, obj), Method.GET, requestParameters, timeout);
45  }
46 
56  public static T Post_AsType<T>(
57  this IAPIResource obj,
58  IEnumerable<Parameter> requestParameters = null,
59  int? timeout = null)
60  {
61  return API.RequestAndParse<T>(API.GetCollectionPath(obj),
62  Method.POST, API.AddRequestBodyParameters(requestParameters, obj), timeout);
63  }
64 
75  public static T PostSubResource<T>(
76  this IAPIResource obj,
77  string subResource,
78  IEnumerable<Parameter> requestParameters,
79  object data,
80  int? timeout = null)
81  {
82  return API.RequestAndParse<T>(GetSubResourcePath(subResource, obj),
83  Method.POST, API.AddRequestBodyParameters(requestParameters, data), timeout);
84  }
85 
95  public static T Put_AsType<T>(
96  this IAPIResource obj,
97  IEnumerable<Parameter> requestParameters = null,
98  int? timeout = null)
99  {
100  return API.RequestAndParse<T>(obj.Path, Method.PUT,
101  API.AddRequestBodyParameters(requestParameters, obj), timeout);
102  }
103 
114  public static T PutSubResource<T>(
115  this IAPIResource obj,
116  string subResource,
117  IEnumerable<Parameter> requestParameters,
118  object data,
119  int? timeout = null)
120  {
121  return API.RequestAndParse<T>(GetSubResourcePath(subResource, obj),
122  Method.PUT, API.AddRequestBodyParameters(requestParameters, data), timeout);
123  }
124 
133  public static T Delete<T>(
134  this IAPIResource obj,
135  IEnumerable<Parameter> requestParameters = null,
136  int? timeout = null)
137  {
138  return API.RequestAndParse<T>(obj.Path, Method.DELETE, requestParameters, timeout);
139  }
140  #endregion Response Type Specified at Compile Time as Generic Method Type Parameter
141 
142  #region No Response
143  public static void Delete(
148  this IAPIResource obj,
149  IEnumerable<Parameter> requestParameters = null,
150  int? timeout = null)
151  {
152  API.RequestAndParse<string>(obj.Path, Method.DELETE, requestParameters, timeout);
153  }
154  #endregion No Response
155 
156  #region Response Type Specified as run-time Type Parameter
157  public static object Get(
167  this IAPIResource obj,
168  Type deserializeType,
169  IEnumerable<Parameter> requestParameters = null,
170  int? timeout = null)
171  {
172  return API.RequestAndParse(deserializeType, obj.Path, Method.GET, requestParameters, timeout);
173  }
174 
185  public static object GetSubResource(
186  this IAPIResource obj,
187  Type deserializeType,
188  string subResource,
189  IEnumerable<Parameter> requestParameters = null,
190  int? timeout = null)
191  {
192  return API.RequestAndParse(deserializeType, GetSubResourcePath(subResource, obj),
193  Method.GET, requestParameters, timeout);
194  }
195 
205  public static object Post(
206  this IAPIResource obj,
207  Type deserializeType,
208  IEnumerable<Parameter> requestParameters = null,
209  int? timeout = null)
210  {
211  return API.RequestAndParse(deserializeType, API.GetCollectionPath(obj),
212  Method.POST, API.AddRequestBodyParameters(requestParameters, obj), timeout);
213  }
214 
225  public static object PostSubResource(
226  this IAPIResource obj,
227  Type deserializeType,
228  string subResource,
229  IEnumerable<Parameter> requestParameters,
230  object data,
231  int? timeout = null)
232  {
233  return API.RequestAndParse(deserializeType, GetSubResourcePath(subResource, obj),
234  Method.POST, API.AddRequestBodyParameters(requestParameters, data), timeout);
235  }
236 
246  public static object Put(
247  this IAPIResource obj,
248  Type deserializeType,
249  IEnumerable<Parameter> requestParameters = null,
250  int? timeout = null)
251  {
252  return API.RequestAndParse(deserializeType, obj.Path, Method.PUT,
253  API.AddRequestBodyParameters(requestParameters, obj), timeout);
254  }
255 
266  public static object PutSubResource(
267  this IAPIResource obj,
268  Type deserializeType,
269  string subResource,
270  IEnumerable<Parameter> requestParameters,
271  object data,
272  int? timeout = null)
273  {
274  return API.RequestAndParse(deserializeType, GetSubResourcePath(subResource, obj),
275  Method.PUT, API.AddRequestBodyParameters(requestParameters, data), timeout);
276  }
277 
286  public static object Delete(
287  this IAPIResource obj,
288  Type deserializeType,
289  IEnumerable<Parameter> requestParameters = null,
290  int? timeout = null)
291  {
292  return API.RequestAndParse(deserializeType, obj.Path, Method.DELETE, requestParameters, timeout);
293  }
294  #endregion Response Type Specified as run-time Type Parameter
295 
296  #region Helper Methods
297  private static string GetSubResourcePath(string subResource, IAPIResource res)
298  {
299  if (String.IsNullOrWhiteSpace(subResource))
300  throw new ArgumentException($"Invalid Request: {nameof(subResource)} argument cannot be blank.");
301  return $"{res.Path}/{subResource}";
302  }
303  #endregion Helper Methods
304  }
305 }
string Path
The relative path to this endpoint from the root URL.
Definition: IEndpoint.cs:13
API methods / requests made available to the user.
static void Delete(this IAPIResource obj, IEnumerable< Parameter > requestParameters=null, int?timeout=null)
Requests for the server to delete the specified object from its resource collection.
static object Get(this IAPIResource obj, Type deserializeType, IEnumerable< Parameter > requestParameters=null, int?timeout=null)
Perform a GET on the current resource to return the equivalent resource stored on the server...
static T Get_AsType< T >(this IAPIResource obj, IEnumerable< Parameter > requestParameters=null, int?timeout=null)
Perform a GET on the current resource to return the equivalent resource stored on the server...
static object Delete(this IAPIResource obj, Type deserializeType, IEnumerable< Parameter > requestParameters=null, int?timeout=null)
Requests for the server to delete the specified object from its resource collection.
static object RequestAndParse(Type deserializeType, string resource, Method method, IEnumerable< Parameter > requestParameters=null, int?timeout=null)
Perform a REST request on the server and serializes the response to the desired compile-time type...
static object GetSubResource(this IAPIResource obj, Type deserializeType, string subResource, IEnumerable< Parameter > requestParameters=null, int?timeout=null)
Perform a GET on a sub-resource of the current resource. Requires the id property to be specified...
static T Delete< T >(this IAPIResource obj, IEnumerable< Parameter > requestParameters=null, int?timeout=null)
Requests for the server to delete the specified object from its resource collection.
static object Put(this IAPIResource obj, Type deserializeType, IEnumerable< Parameter > requestParameters=null, int?timeout=null)
Perform a PUT on the current resource, returning the server response deserialized to a new object of ...
static T Put_AsType< T >(this IAPIResource obj, IEnumerable< Parameter > requestParameters=null, int?timeout=null)
Perform a PUT on the current resource, returning the server response deserialized to a new object of ...
static T GetSubResource< T >(this IAPIResource obj, string subResource, IEnumerable< Parameter > requestParameters=null, int?timeout=null)
Perform a GET on a sub-resource of the current resource. Requires the id property to be specified...
static object PostSubResource(this IAPIResource obj, Type deserializeType, string subResource, IEnumerable< Parameter > requestParameters, object data, int?timeout=null)
POSTs some data to a sub-resource of the current resource.
static T PostSubResource< T >(this IAPIResource obj, string subResource, IEnumerable< Parameter > requestParameters, object data, int?timeout=null)
POSTs some data to a sub-resource of the current resource.
static object PutSubResource(this IAPIResource obj, Type deserializeType, string subResource, IEnumerable< Parameter > requestParameters, object data, int?timeout=null)
Performs a PUT of some data on a sub-resource of the current resource.
static object Post(this IAPIResource obj, Type deserializeType, IEnumerable< Parameter > requestParameters=null, int?timeout=null)
Perform a POST on the current resource, returning the server response deserialized to a new object of...
static T PutSubResource< T >(this IAPIResource obj, string subResource, IEnumerable< Parameter > requestParameters, object data, int?timeout=null)
Performs a PUT of some data on a sub-resource of the current resource.
Interface for Base class used by all resources.
Definition: IAPIResource.cs:8
static T Post_AsType< T >(this IAPIResource obj, IEnumerable< Parameter > requestParameters=null, int?timeout=null)
Perform a POST on the current resource, returning the server response deserialized to a new object of...