C# Client Library
A C# Client Library for the AnalyzeRe REST API
API.Parameters.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Linq.Expressions;
5 using System.Reflection;
6 using System.Runtime.Serialization;
7 
8 using AnalyzeRe.Rest;
9 using AnalyzeRe.Utilities;
10 using RestSharp;
11 
12 namespace AnalyzeRe
13 {
17  public static partial class API
18  {
22  public static class Parameters
23  {
25  public const string PATCHContentType = "application/offset+octet-stream";
26 
34  public static RequestParameters Search(string searchTerms)
35  {
36  return new RequestParameters().AddQueryParameter("search", searchTerms);
37  }
38 
159  public static RequestParameters AdvancedSearch(string advancedSearchTerms)
160  {
161  return new RequestParameters().AddQueryParameter("metaquery", advancedSearchTerms);
162  }
163 
170  public static RequestParameters Type(string apiTypeName) =>
171  new RequestParameters().AddQueryParameter("type", apiTypeName);
172 
180  public static RequestParameters Type(Type resourceType) =>
181  Type(TypeResolver.GetTypeNameForAPIType(resourceType));
182 
197  public static RequestParameters Order(string ordering)
198  {
199  return new RequestParameters().AddQueryParameter("ordering", ordering);
200  }
201 
214  public static RequestParameters Order<T>(Expression<Func<T, object>> propertyExpression,
215  bool ascending = true)
216  {
217  MemberInfo prop = ReflectionUtilities.GetMemberInfo(propertyExpression);
218  // The server-side property name for ordering comes from the DataMember attribute
219  // or the property if it has one, else it's the lower_case of the property name.
220  string name = prop.GetCustomAttributeFast<DataMemberAttribute>()?.Name ??
221  prop.Name.ToLower();
222  return Order((ascending ? "" : "-") + name);
223  }
224 
236  {
237  return new RequestParameters().AddHeader("References", "expand");
238  }
239 
251  public static RequestParameters Fields(IEnumerable<string> fieldNames)
252  {
253  if (fieldNames == null || !fieldNames.Any())
254  return null;
255  return new RequestParameters().AddQueryParameter("fields", String.Join(",", fieldNames));
256  }
257 
269  public static RequestParameters Omit(IEnumerable<string> fieldNames)
270  {
271  if (fieldNames == null || !fieldNames.Any())
272  return null;
273  return new RequestParameters().AddQueryParameter("omit", String.Join(",", fieldNames));
274  }
275 
289  public static RequestParameters Ids(IEnumerable<string> ids)
290  {
291  if (ids == null || !ids.Any())
292  return null;
293  return new RequestParameters().AddQueryParameter("ids", String.Join(",", ids));
294  }
295 
296  #region Pagination
297  public static RequestParameters Page(long offset, long limit)
309  {
310  return new RequestParameters()
311  .AddQueryParameter("offset", offset.ToString("0"))
312  .AddQueryParameter("limit", limit.ToString("0"));
313  }
314 
326  public static RequestParameters PageNumber(long pageNumber, long resultsPerPage)
327  {
328  if (pageNumber < 1)
329  throw new ArgumentException("Page number must be 1 or greater.");
330  return Page((pageNumber - 1) * resultsPerPage, resultsPerPage);
331  }
332  #endregion Pagination
333 
334  #region Large File Upload
335  public static RequestParameters DataEntityLength(long totalBytes)
346  {
347  return new RequestParameters()
348  .AddHeader("Entity-Length", totalBytes.ToString("0"));
349  }
350 
360  long offsetBytes, byte[] chunkData, int count)
361  {
362  // HACK: RESTSharp is dumb. It will use the buffer byte array and doesn't
363  // have a way to specify how much of that byte array is usable,
364  // so if it's not all usable we need to copy just the usable bit
365  // to a new, smaller byte array and use that instead.
366  if (chunkData.Length > count)
367  {
368  byte[] temp = new byte[count];
369  Array.Copy(chunkData, 0, temp, 0, count);
370  chunkData = temp;
371  }
372  return DataPatchChunk_Object(offsetBytes, chunkData, count);
373  }
374 
383  long offsetBytes, string chunkData)
384  {
385  return DataPatchChunk_Object(offsetBytes, chunkData, chunkData.Length);
386  }
387 
398  long offsetBytes, object chunkData, int content_length)
399  {
400  return new RequestParameters()
401  .AddHeader("Content-Type", PATCHContentType)
402  .AddHeader("Content-Length", content_length.ToString("0"))
403  .AddHeader("Offset", offsetBytes.ToString("0"))
404  .AddParameter(new Parameter
405  {
406  Name = PATCHContentType,
407  Value = chunkData,
408  Type = ParameterType.RequestBody
409  });
410  }
411  #endregion Large File Upload
412  }
413  }
414 }
static RequestParameters Page(long offset, long limit)
When getting a collection of items, these parameters can be added to restrict the number of results y...
Helper class which makes it easier to build a set of request parameters.
static RequestParameters Order(string ordering)
Can be added to collection GET requests to specify the way items should be ordered. Note: Some properties are not supported, in which case ordering will have no effect.
static RequestParameters PageNumber(long pageNumber, long resultsPerPage)
A variation of the &#39;Page&#39; parameters that achieves the same effect, but by specifying a page number r...
static RequestParameters DataPatchChunk(long offsetBytes, byte[] chunkData, int count)
Creates the RestSharp HTTP Header parameter required to PATCH a data endpoint with part of a file...
static RequestParameters DataPatchChunk(long offsetBytes, string chunkData)
Creates the RestSharp HTTP Header parameter required to PATCH a data endpoint with part of a file...
static RequestParameters DataEntityLength(long totalBytes)
Creates an HTTP Header RestSharp parameter that specifies the length of an entity to be uploaded in s...
static RequestParameters Search(string searchTerms)
Can be added to your GET requests to send a search request to the server. If the endpoint supports se...
static RequestParameters Type(string apiTypeName)
Can be added to your collection GET requests to filter by a specific sub-type when that resource is p...
static RequestParameters Ids(IEnumerable< string > ids)
Can be added to your collection GET requests to return only the set of resources whose id matches one...
static object GetCustomAttributeFast(this MemberInfo member, Type attributeType)
Gets the specified attribute from the member.
static string GetTypeNameForAPIType(Type type)
Gets the type name from the overriding type alias attribute or class name based using the supplied ty...
Definition: TypeResolver.cs:92
const string PATCHContentType
The contentType to set when submitting a PATCHED chunk data.
static RequestParameters AdvancedSearch(string advancedSearchTerms)
Can be added to your GET requests to send a field filtering request to the server. If the endpoint supports data filtering, the results will be a filtered subset. Supports key/value search with logical operators. Currently supports the &#39;meta_data&#39; field.
static RequestParameters Fields(IEnumerable< string > fieldNames)
Can be added to your GET requests to return only the specified fields in the response body...
static RequestParameters Order< T >(Expression< Func< T, object >> propertyExpression, bool ascending=true)
Helper function to create RequestParameters for ordering by the specified property. Note: Some properties are not supported, in which case ordering will have no effect.
Parameters that can be added to your REST requests to access additional API features.
static RequestParameters Omit(IEnumerable< string > fieldNames)
Similar to Fields, but rather than supplying a "white-list", allows one to specify a list of fields t...
static RequestParameters ExpandReferences()
Can be added to your GET requests to recursively resolve resource references in the response...
Utility for resolving types given only the types name (Useful for parsing ambiguous JSON objects)...
Definition: TypeResolver.cs:13
Utilities that reflect on a type or property expression.
static RequestParameters DataPatchChunk_Object(long offsetBytes, object chunkData, int content_length)
Creates the RestSharp HTTP Header parameter required to PATCH a data endpoint with part of a file...
RequestParameters AddQueryParameter(string name, string value)
Adds the specified name/value pair as a new request parameter with the type ParameterType.QueryString if the value is not null.
RequestParameters AddHeader(string name, string value=null)
Adds the specified name/value pair as a HEADER.