2using System.Collections;
3using System.Collections.Generic;
4using System.Diagnostics;
8using System.Net.Sockets;
18 public static partial class API
40 #region Explicit Get Methods
57 ValidateResourceId(
id);
78 ValidateResourceId(
id);
130 #endregion Explicit Get Methods
132 #region GET Resource Collection
183 #region Private Helper Methods
205 if (
baseType.ContainsGenericParameters)
222 .Where(
i =>
i.ContainsGenericParameters);
230 baseType.GetGenericArguments()[0].GetGenericParameterConstraints()[0]);
281 private static string DetermineCollectionNameAndBaseType(
294 "Could not resolve the collection name for this Resource, it must be " +
295 "provided manually via the 'collectionName' optional parameter.",
e);
304 #endregion Private Helper Methods
305 #endregion GET Resource Collection
307 #region Search Resource Collection
380 #endregion Search Resource Collection
382 #region Batch Requests
438 #endregion Batch Requests
440 #region Combined Request and Deserialization
491 #endregion Combined Request and Deserialization
493 #region Basic REST Request
535 request.ReadWriteTimeout =
request.Timeout;
577 "The initial request could not be completed due to a temporary " +
578 "communication error. Please reset the stream and try again. " +
579 "The error message was: " +
response.ErrorMessage);
591 "The request encountered what appears to be a temporary error, " +
593 "The final error message was: " +
response.ErrorMessage);
611 #endregion Basic REST Request
613 #region IRestResponse deserialization
625 () => Converter.Deserialize<
T>(
response));
641 #endregion IRestResponse deserialization
643 #region Private Helper Methods
644 #region Request Helpers
651 private static void PrepareRequestParameters(
660 p.Type ==
ParameterType.HttpHeader && p.Name.Equals(AUTH_HEADER)))
663 RestSharp.ParameterType.HttpHeader);
672 throw new ArgumentException(
"More than one RequestBody parameter was supplied.");
685 Name = JSON_CONTENT_TYPE,
686 ContentType = JSON_CONTENT_TYPE,
688 Type = ParameterType.RequestBody
709 request.Parameters.AddRange(parameters);
711 #endregion Request Helpers
713 #region Response Helpers
727 h =>
h.Name.Equals(
"Location",
StringComparison.OrdinalIgnoreCase))?.Value?.ToString();
728 string message =
"The server returned a redirect status (" + response.StatusCode +
")";
729 Debug.WriteLine(message);
731 throw ServerErrorExceptionHelper(
response,
null,
null, message +
732 ", but did specify a redirect location.");
743 throw ServerErrorExceptionHelper(
response,
null,
null, message +
744 ", but wants to switch to a new host URL (" +
newUri.Host +
"). " +
745 "You will have to manually reconfigure your client to use this " +
746 "new address if you trust it.");
748 return newUri.AbsolutePath;
750 throw ServerErrorExceptionHelper(
response,
null,
null, message +
751 ", but the redirect location was not well formed: " +
newLocation);
788 Debug.WriteLine(
"WebException Occurred with Status: " +
789 $"{asWebException.Status}\n{asWebException}");
802 "connected party did not properly respond after a period of time, " +
803 "or established connection failed because connected host has failed to respond";
814 else if (
response.ErrorMessage.Contains(
"The operation has timed out"))
818 $"Server Communication Error - {response.ErrorMessage}");
831 string logPrefix =
$"The request timed out after {elapsed.TotalMilliseconds} ms";
837 Debug.WriteLine(
$"{logPrefix}, but the request object is empty. " +
838 "This likely indicates that a low-level timeout occurred before " +
839 "the response could be instantiated. The request will be retried.");
846 Debug.WriteLine(
$"{logPrefix}, but the configured timeout period " +
847 $"({response.Request.Timeout} ms) hasn't elapsed. " +
848 "This likely indicates a timeout occurred in the underlying TCP protocol. " +
849 "The request will be retried.");
854 Debug.WriteLine(
$"{logPrefix}. Re-raising the error returned (" +
855 $"{response.ErrorException.GetType().NiceTypeName()}) as a RequestTimeoutException.");
856 string message =
$"{logPrefix}. The requested timeout was {response.Request.Timeout} ms. " +
857 "If this is a large request, you may need to wait longer for a response. " +
858 "Check your API.DefaultRequestTimeout or set a timeout directly in your API call.";
900 Debug.WriteLine(
"The response was successfully parsed and returned.\n");
908 "The response could not be parsed into the desired type: \"" +
921 Debug.WriteLine(
"The following request failed:");
947 response.StatusCode != HttpStatusCode.ServiceUnavailable &&
954 if (String.IsNullOrWhiteSpace(
response.Content))
961 "The server response was an error but no error details were supplied.");
968 Debug.WriteLine(
"The response was received but contained a server error.");
985 Debug.WriteLine(
"The response was received but could not be parsed.");
991 #endregion Response Helpers
992 #endregion Private Helper Methods
Used to deserialize server OPTIONS response.
Describes a collection of resources which can be listed.
Used to deserialize server error messages.
Parameters that can be added to your REST requests to access additional API features.
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 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 Page(long offset, long limit)
When getting a collection of items, these parameters can be added to restrict the number of results y...
static RequestParameters AdvancedSearch(string advancedSearchTerms)
Can be added to your GET requests to send a field filtering request to the server....
API methods / requests made available to the user.
static object GetUri(Type deserializeType, Uri href, IEnumerable< Parameter > requestParameters=null, int? timeout=null)
Perform a GET on an arbitrary resource by extracting the path and query parameters from a manually sp...
static OptionsResponse Options(IEnumerable< Parameter > requestParameters=null, int? timeout=null)
Perform an OPTIONS request on the API, which returns a list of features supported for the current aut...
static IAccessToken AuthenticationToken
The AccessToken storing authentication information for requests made to the server.
static int DefaultRequestTimeout
The default timeout used for all simple server requests, in milliseconds.
static volatile int MaxRequestRetries
When a temporary communication failure occurs (such as a socket error or an authentication failure fo...
static T RequestAndParse< T >(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 T GetUri< T >(Uri href, IEnumerable< Parameter > requestParameters=null, int? timeout=null)
Perform a GET on an arbitrary resource by extracting the path and query parameters from a manually sp...
static string GetCollectionName< T >()
Gets the collection name for the given APIResource type, whether it's instantiable or not.
static ICollectionResponse< IAPIResource > GetResourceList(Type resourceType, IEnumerable< Parameter > requestParameters=null, string collectionNameOverride=null, int? timeout=null)
Get a collection of resources from the server.
static ICollectionResponse< T > BatchGet< T >(IEnumerable< string > ids, IEnumerable< Parameter > requestParameters=null, string collectionNameOverride=null, int? timeout=null)
Get a collection of resources from the server that match the set of resource ids specified.
static bool TryHandleAuthenticationErrors(IRestResponse response, int sequentialAttempts=0)
Checks an IRestResponse object for authentication errors, then invokes configured authentication dele...
static string GetCurrentAuthorizationString()
Return the Authorization header string formed by the current AuthenticationToken.
static T ExtractRestResponse< T >(IRestResponse response)
Attempts to deserialize an IRestResponse content to the specified type T.
static int DefaultRequestTimeoutCollections
The default timeout used when requesting a resource collection from the server, in milliseconds.
static object ExtractRestResponse(IRestResponse response, Type deserializeType)
Attempts to deserialize an IRestResponse content to the specified run-time type.
static ICollectionResponse< IAPIResource > SearchResourceList(Type resourceType, string searchTerms, IEnumerable< Parameter > requestParameters=null, string collectionNameOverride=null, int? timeout=null, SearchType searchType=SearchType.Basic)
Get a collection of resources from the server that match the specified search criteria.
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 run-time type.
static ICollectionResponse< T > SearchResourceList< T >(string searchTerms, IEnumerable< Parameter > requestParameters=null, string collectionNameOverride=null, int? timeout=null, SearchType searchType=SearchType.Basic)
Get a collection of resources from the server that match the specified search criteria.
static IAPIResource Get(Type resourceType, string id, IEnumerable< Parameter > requestParameters=null, int? timeout=null)
Get the selected resource by ID. The appropriate collection is automatically determined by the suppli...
SearchType
The search type determines the interpretation of the query string.
@ Basic
Search by keyword on predefined field.
@ Advanced
Search by complex query on key-value metadata. See Parameters.AdvancedSearch for details about advanc...
static string GetCollectionName(Type requestType)
Gets the collection name for the given APIResource type, whether it's instantiable or not.
static ICollectionResponse< IAPIResource > BatchGet(Type resourceType, IEnumerable< string > ids, IEnumerable< Parameter > requestParameters=null, string collectionNameOverride=null, int? timeout=null)
Get a collection of resources from the server that match the set of resource ids specified.
static IRestResponse ExecuteRestRequest(string resource, Method method, IEnumerable< Parameter > requestParameters=null, int? timeout=null, bool returnRawResponse=false, Action< Stream > responseStreamReader=null)
Perform a REST request on the server.
static T Get< T >(string id, IEnumerable< Parameter > requestParameters=null, int? timeout=null)
Get the selected resource by ID. The appropriate collection is automatically determined by the resour...
static ICollectionResponse< T > GetResourceList< T >(IEnumerable< Parameter > requestParameters=null, string collectionNameOverride=null, int? timeout=null)
Get a collection of resources from the server.
Extends the TimeoutException class to contain the IRestResponse that timed out.
Helper class which makes it easier to build a set of request parameters.
Utility for resolving types given only the types name (Useful for parsing ambiguous JSON objects).
static Type GetGenericCovariantBase(Type T)
Returns the most strongly typed covariant base type which is assignable from the specified type T.
Interface for Base class used by all resources.