using RSG; namespace Proyecto26 { public static partial class RestClient { #region Promises /// /// Create an HTTP request and return a promise. /// /// Returns a promise for a value of type ResponseHelper. /// The options of the request. public static IPromise Request(RequestHelper options) { var promise = new Promise(); Request(options, promise.Promisify); return promise; } /// /// Create an HTTP request and convert the response. /// /// Returns a promise for a value of a specified type. /// The options of the request. /// The element type of the response. public static IPromise Request(RequestHelper options) { var promise = new Promise(); Request(options, promise.Promisify); return promise; } /// /// Load data from the server using a HTTP GET request. /// /// Returns a promise for a value of type ResponseHelper. /// A string containing the URL to which the request is sent. public static IPromise Get(string url) { return Get(new RequestHelper { Uri = url }); } /// /// Load data from the server using a HTTP GET request. /// /// Returns a promise for a value of type ResponseHelper. /// The options of the request. public static IPromise Get(RequestHelper options) { var promise = new Promise(); Get(options, promise.Promisify); return promise; } /// /// Load data from the server using a HTTP GET request. /// /// Returns a promise for a value of a specified type. /// A string containing the URL to which the request is sent. /// The element type of the response. public static IPromise Get(string url) { return Get(new RequestHelper { Uri = url }); } /// /// Load data from the server using a HTTP GET request. /// /// Returns a promise for a value of a specified type. /// The options of the request. /// The element type of the response. public static IPromise Get(RequestHelper options) { var promise = new Promise(); Get(options, promise.Promisify); return promise; } /// /// Load data from the server using a HTTP GET request. /// /// Returns a promise for an array of values. /// A string containing the URL to which the request is sent. /// The element type of the array. public static IPromise GetArray(string url) { return GetArray(new RequestHelper { Uri = url }); } /// /// Load data from the server using a HTTP GET request. /// /// Returns a promise for an array of values. /// The options of the request. /// The element type of the array. public static IPromise GetArray(RequestHelper options) { var promise = new Promise(); GetArray(options, promise.Promisify); return promise; } /// /// Load data from the server using a HTTP POST request. /// /// Returns a promise for a value of type ResponseHelper. /// A string containing the URL to which the request is sent. /// A plain object that is sent to the server with the request. public static IPromise Post(string url, object body) { return Post(new RequestHelper { Uri = url, Body = body }); } /// /// Load data from the server using a HTTP POST request. /// /// Returns a promise for a value of type ResponseHelper. /// A string containing the URL to which the request is sent. /// A string that is sent to the server with the request. public static IPromise Post(string url, string bodyString) { return Post(new RequestHelper { Uri = url, BodyString = bodyString }); } /// /// Load data from the server using a HTTP POST request. /// /// Returns a promise for a value of type ResponseHelper. /// The options of the request. public static IPromise Post(RequestHelper options) { var promise = new Promise(); Post(options, promise.Promisify); return promise; } /// /// Load data from the server using a HTTP POST request. /// /// Returns a promise for a value of a specified type. /// A string containing the URL to which the request is sent. /// A plain object that is sent to the server with the request. /// The element type of the response. public static IPromise Post(string url, object body) { return Post(new RequestHelper { Uri = url, Body = body }); } /// /// Load data from the server using a HTTP POST request. /// /// Returns a promise for a value of a specified type. /// A string containing the URL to which the request is sent. /// A string that is sent to the server with the request. /// The element type of the response. public static IPromise Post(string url, string bodyString) { return Post(new RequestHelper { Uri = url, BodyString = bodyString }); } /// /// Load data from the server using a HTTP POST request. /// /// Returns a promise for a value of a specified type. /// The options of the request. /// The element type of the response. public static IPromise Post(RequestHelper options) { var promise = new Promise(); Post(options, promise.Promisify); return promise; } /// /// Load a JSON array from the server using a HTTP POST request. /// /// Returns a promise for an array of values. /// A string containing the URL to which the request is sent. /// A plain object that is sent to the server with the request. /// The element type of the array. public static IPromise PostArray(string url, object body) { return PostArray(new RequestHelper { Uri = url, Body = body }); } /// /// Load a JSON array from the server using a HTTP POST request. /// /// Returns a promise for an array of values. /// A string containing the URL to which the request is sent. /// A string that is sent to the server with the request. /// The element type of the array. public static IPromise PostArray(string url, string bodyString) { return PostArray(new RequestHelper { Uri = url, BodyString = bodyString }); } /// /// Load a JSON array from the server using a HTTP POST request. /// /// Returns a promise for an array of values. /// The options of the request. /// The element type of the array. public static IPromise PostArray(RequestHelper options) { var promise = new Promise(); PostArray(options, promise.Promisify); return promise; } /// /// Load data from the server using a HTTP PUT request. /// /// Returns a promise for a value of type ResponseHelper. /// A string containing the URL to which the request is sent. /// A plain object that is sent to the server with the request. public static IPromise Put(string url, object body) { return Put(new RequestHelper { Uri = url, Body = body }); } /// /// Load data from the server using a HTTP PUT request. /// /// Returns a promise for a value of type ResponseHelper. /// A string containing the URL to which the request is sent. /// A string that is sent to the server with the request. public static IPromise Put(string url, string bodyString) { return Put(new RequestHelper { Uri = url, BodyString = bodyString }); } /// /// Load data from the server using a HTTP PUT request. /// /// Returns a promise for a value of type ResponseHelper. /// The options of the request. public static IPromise Put(RequestHelper options) { var promise = new Promise(); Put(options, promise.Promisify); return promise; } /// /// Load data from the server using a HTTP PUT request. /// /// Returns a promise for a value of a specified type. /// A string containing the URL to which the request is sent. /// A plain object that is sent to the server with the request. /// The element type of the response. public static IPromise Put(string url, object body) { return Put(new RequestHelper { Uri = url, Body = body }); } /// /// Load data from the server using a HTTP PUT request. /// /// Returns a promise for a value of a specified type. /// A string containing the URL to which the request is sent. /// A string that is sent to the server with the request. /// The element type of the response. public static IPromise Put(string url, string bodyString) { return Put(new RequestHelper { Uri = url, BodyString = bodyString }); } /// /// Load data from the server using a HTTP PUT request. /// /// Returns a promise for a value of a specified type. /// The options of the request. /// The element type of the response. public static IPromise Put(RequestHelper options) { var promise = new Promise(); Put(options, promise.Promisify); return promise; } /// /// Delete the specified resource identified by the URI. /// /// Returns a promise for a value of type ResponseHelper. /// A string containing the URL to which the request is sent. public static IPromise Delete(string url) { return Delete(new RequestHelper { Uri = url }); } /// /// Delete the specified resource identified by the URI. /// /// Returns a promise for a value of type ResponseHelper. /// The options of the request. public static IPromise Delete(RequestHelper options) { var promise = new Promise(); Delete(options, promise.Promisify); return promise; } /// /// Requests the headers that are returned from the server /// /// Returns a promise for a value of type ResponseHelper. /// A string containing the URL to which the request is sent. public static IPromise Head(string url) { return Delete(new RequestHelper { Uri = url }); } /// /// Requests the headers that are returned from the server /// /// Returns a promise for a value of type ResponseHelper. /// The options of the request. public static IPromise Head(RequestHelper options) { var promise = new Promise(); Head(options, promise.Promisify); return promise; } #endregion #region Helpers /// /// Promisify the specified callback. /// /// The promise to resolve. /// The exception of the request. /// The response of the request. /// The element type of the response. private static void Promisify(this Promise promise, RequestException error, T response) { if (error != null) { promise.Reject(error); } else { promise.Resolve(response); } } /// /// Promisify the specified callback ignoring the response. /// /// The promise to resolve. /// The exception of the request. /// The response of the request. /// A body of the response. /// The element type of the response. private static void Promisify(this Promise promise, RequestException error, ResponseHelper response, T body) { if (error != null && response != null) { error.ServerMessage = response.Error ?? error.Message; } promise.Promisify(error, body); } #endregion } }