From 8a5936834dbd2cf536ae720af4e499e429310d15 Mon Sep 17 00:00:00 2001 From: Tim Date: Sun, 25 Sep 2022 11:39:39 -0400 Subject: [PATCH] Switch RSG to ProtoPromise. --- .../Proyecto26.RestClient.csproj | 8 +- .../Proyecto26.RestClient.nuspec | 3 +- .../RestClientPromise.cs | 189 +++++++++--------- src/Proyecto26.RestClient/packages.config | 3 +- 4 files changed, 101 insertions(+), 102 deletions(-) diff --git a/src/Proyecto26.RestClient/Proyecto26.RestClient.csproj b/src/Proyecto26.RestClient/Proyecto26.RestClient.csproj index 9c255c1..fc2e89e 100644 --- a/src/Proyecto26.RestClient/Proyecto26.RestClient.csproj +++ b/src/Proyecto26.RestClient/Proyecto26.RestClient.csproj @@ -53,8 +53,12 @@ - - ..\packages\RSG.Promise.3.0.1\lib\net35\RSG.Promise.dll + + ..\packages\ProtoPromise.2.3.0\lib\net35\Release\ProtoPromise.dll + False + + + ..\packages\ProtoPromiseUnityHelpers.2.3.0\lib\net35\Release\ProtoPromiseUnityHelpers.dll False diff --git a/src/Proyecto26.RestClient/Proyecto26.RestClient.nuspec b/src/Proyecto26.RestClient/Proyecto26.RestClient.nuspec index 0a35221..dfd754d 100644 --- a/src/Proyecto26.RestClient/Proyecto26.RestClient.nuspec +++ b/src/Proyecto26.RestClient/Proyecto26.RestClient.nuspec @@ -21,7 +21,8 @@ Copyright ©2019 Proyecto 26 Unity Promises Http Rest http-client HttpClient rest-client RestClient Request API Requests APIs JSON Networking - + + diff --git a/src/Proyecto26.RestClient/RestClientPromise.cs b/src/Proyecto26.RestClient/RestClientPromise.cs index b0e92f2..ee2b972 100644 --- a/src/Proyecto26.RestClient/RestClientPromise.cs +++ b/src/Proyecto26.RestClient/RestClientPromise.cs @@ -1,4 +1,5 @@ -using RSG; +using Proto.Promises; +using System; namespace Proyecto26 { @@ -12,11 +13,11 @@ public static partial class RestClient /// /// Returns a promise for a value of type ResponseHelper. /// The options of the request. - public static IPromise Request(RequestHelper options) + public static Promise Request(RequestHelper options) { - var promise = new Promise(); - Request(options, promise.Promisify); - return promise; + var deferred = Promise.NewDeferred(); + Request(options, GetCallback(deferred)); + return deferred.Promise; } /// @@ -25,11 +26,11 @@ public static IPromise Request(RequestHelper options) /// 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) + public static Promise Request(RequestHelper options) { - var promise = new Promise(); - Request(options, promise.Promisify); - return promise; + var deferred = Promise.NewDeferred(); + Request(options, GetCallback(deferred)); + return deferred.Promise; } /// @@ -37,7 +38,7 @@ public static IPromise Request(RequestHelper options) /// /// 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) + public static Promise Get(string url) { return Get(new RequestHelper { Uri = url }); } @@ -47,11 +48,11 @@ public static IPromise Get(string url) /// /// Returns a promise for a value of type ResponseHelper. /// The options of the request. - public static IPromise Get(RequestHelper options) + public static Promise Get(RequestHelper options) { - var promise = new Promise(); - Get(options, promise.Promisify); - return promise; + var deferred = Promise.NewDeferred(); + Get(options, GetCallback(deferred)); + return deferred.Promise; } /// @@ -60,7 +61,7 @@ public static IPromise Get(RequestHelper options) /// 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) + public static Promise Get(string url) { return Get(new RequestHelper { Uri = url }); } @@ -71,11 +72,11 @@ public static IPromise Get(string url) /// 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) + public static Promise Get(RequestHelper options) { - var promise = new Promise(); - Get(options, promise.Promisify); - return promise; + var deferred = Promise.NewDeferred(); + Get(options, GetCallback(deferred)); + return deferred.Promise; } /// @@ -84,7 +85,7 @@ public static IPromise Get(RequestHelper options) /// 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) + public static Promise GetArray(string url) { return GetArray(new RequestHelper { Uri = url }); } @@ -95,11 +96,11 @@ public static IPromise GetArray(string url) /// 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) + public static Promise GetArray(RequestHelper options) { - var promise = new Promise(); - GetArray(options, promise.Promisify); - return promise; + var deferred = Promise.NewDeferred(); + GetArray(options, GetCallback(deferred)); + return deferred.Promise; } /// @@ -108,7 +109,7 @@ public static IPromise GetArray(RequestHelper options) /// 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) + public static Promise Post(string url, object body) { return Post(new RequestHelper { Uri = url, Body = body }); } @@ -119,7 +120,7 @@ public static IPromise Post(string url, object body) /// 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) + public static Promise Post(string url, string bodyString) { return Post(new RequestHelper { Uri = url, BodyString = bodyString }); } @@ -129,11 +130,11 @@ public static IPromise Post(string url, string bodyString) /// /// Returns a promise for a value of type ResponseHelper. /// The options of the request. - public static IPromise Post(RequestHelper options) + public static Promise Post(RequestHelper options) { - var promise = new Promise(); - Post(options, promise.Promisify); - return promise; + var deferred = Promise.NewDeferred(); + Post(options, GetCallback(deferred)); + return deferred.Promise; } /// @@ -143,7 +144,7 @@ public static IPromise Post(RequestHelper options) /// 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) + public static Promise Post(string url, object body) { return Post(new RequestHelper { Uri = url, Body = body }); } @@ -155,7 +156,7 @@ public static IPromise Post(string url, object body) /// 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) + public static Promise Post(string url, string bodyString) { return Post(new RequestHelper { Uri = url, BodyString = bodyString }); } @@ -166,11 +167,11 @@ public static IPromise Post(string url, string bodyString) /// 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) + public static Promise Post(RequestHelper options) { - var promise = new Promise(); - Post(options, promise.Promisify); - return promise; + var deferred = Promise.NewDeferred(); + Post(options, GetCallback(deferred)); + return deferred.Promise; } /// @@ -180,7 +181,7 @@ public static IPromise Post(RequestHelper options) /// 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) + public static Promise PostArray(string url, object body) { return PostArray(new RequestHelper { Uri = url, Body = body }); } @@ -192,7 +193,7 @@ public static IPromise PostArray(string url, object body) /// 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) + public static Promise PostArray(string url, string bodyString) { return PostArray(new RequestHelper { Uri = url, BodyString = bodyString }); } @@ -203,11 +204,11 @@ public static IPromise PostArray(string url, string bodyString) /// 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) + public static Promise PostArray(RequestHelper options) { - var promise = new Promise(); - PostArray(options, promise.Promisify); - return promise; + var deferred = Promise.NewDeferred(); + PostArray(options, GetCallback(deferred)); + return deferred.Promise; } /// @@ -216,7 +217,7 @@ public static IPromise PostArray(RequestHelper options) /// 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) + public static Promise Put(string url, object body) { return Put(new RequestHelper { Uri = url, Body = body }); } @@ -227,7 +228,7 @@ public static IPromise Put(string url, object body) /// 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) + public static Promise Put(string url, string bodyString) { return Put(new RequestHelper { Uri = url, BodyString = bodyString }); } @@ -237,11 +238,11 @@ public static IPromise Put(string url, string bodyString) /// /// Returns a promise for a value of type ResponseHelper. /// The options of the request. - public static IPromise Put(RequestHelper options) + public static Promise Put(RequestHelper options) { - var promise = new Promise(); - Put(options, promise.Promisify); - return promise; + var deferred = Promise.NewDeferred(); + Put(options, GetCallback(deferred)); + return deferred.Promise; } /// @@ -251,7 +252,7 @@ public static IPromise Put(RequestHelper options) /// 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) + public static Promise Put(string url, object body) { return Put(new RequestHelper { Uri = url, Body = body }); } @@ -263,7 +264,7 @@ public static IPromise Put(string url, object body) /// 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) + public static Promise Put(string url, string bodyString) { return Put(new RequestHelper { Uri = url, BodyString = bodyString }); } @@ -274,11 +275,11 @@ public static IPromise Put(string url, string bodyString) /// 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) + public static Promise Put(RequestHelper options) { - var promise = new Promise(); - Put(options, promise.Promisify); - return promise; + var deferred = Promise.NewDeferred(); + Put(options, GetCallback(deferred)); + return deferred.Promise; } /// @@ -287,7 +288,7 @@ public static IPromise Put(RequestHelper options) /// 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 Patch(string url, object body) + public static Promise Patch(string url, object body) { return Patch(new RequestHelper { Uri = url, Body = body }); } @@ -298,7 +299,7 @@ public static IPromise Patch(string url, object body) /// 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 Patch(string url, string bodyString) + public static Promise Patch(string url, string bodyString) { return Patch(new RequestHelper { Uri = url, BodyString = bodyString }); } @@ -308,11 +309,11 @@ public static IPromise Patch(string url, string bodyString) /// /// Returns a promise for a value of type ResponseHelper. /// The options of the request. - public static IPromise Patch(RequestHelper options) + public static Promise Patch(RequestHelper options) { - var promise = new Promise(); - Patch(options, promise.Promisify); - return promise; + var deferred = Promise.NewDeferred(); + Patch(options, GetCallback(deferred)); + return deferred.Promise; } /// @@ -322,7 +323,7 @@ public static IPromise Patch(RequestHelper options) /// 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 Patch(string url, object body) + public static Promise Patch(string url, object body) { return Patch(new RequestHelper { Uri = url, Body = body }); } @@ -334,7 +335,7 @@ public static IPromise Patch(string url, object body) /// 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 Patch(string url, string bodyString) + public static Promise Patch(string url, string bodyString) { return Patch(new RequestHelper { Uri = url, BodyString = bodyString }); } @@ -345,11 +346,11 @@ public static IPromise Patch(string url, string bodyString) /// Returns a promise for a value of a specified type. /// The options of the request. /// The element type of the response. - public static IPromise Patch(RequestHelper options) + public static Promise Patch(RequestHelper options) { - var promise = new Promise(); - Patch(options, promise.Promisify); - return promise; + var deferred = Promise.NewDeferred(); + Patch(options, GetCallback(deferred)); + return deferred.Promise; } /// @@ -357,7 +358,7 @@ public static IPromise Patch(RequestHelper options) /// /// 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) + public static Promise Delete(string url) { return Delete(new RequestHelper { Uri = url }); } @@ -367,11 +368,11 @@ public static IPromise Delete(string url) /// /// Returns a promise for a value of type ResponseHelper. /// The options of the request. - public static IPromise Delete(RequestHelper options) + public static Promise Delete(RequestHelper options) { - var promise = new Promise(); - Delete(options, promise.Promisify); - return promise; + var deferred = Promise.NewDeferred(); + Delete(options, GetCallback(deferred)); + return deferred.Promise; } /// @@ -379,7 +380,7 @@ public static IPromise Delete(RequestHelper options) /// /// 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) + public static Promise Head(string url) { return Delete(new RequestHelper { Uri = url }); } @@ -389,43 +390,35 @@ public static IPromise Head(string url) /// /// Returns a promise for a value of type ResponseHelper. /// The options of the request. - public static IPromise Head(RequestHelper options) + public static Promise Head(RequestHelper options) { - var promise = new Promise(); - Head(options, promise.Promisify); - return promise; + var deferred = Promise.NewDeferred(); + Head(options, GetCallback(deferred)); + return deferred.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) + private static Action GetCallback(Promise.Deferred deferred) { - if (error != null) { promise.Reject(error); } else { promise.Resolve(response); } + return (RequestException error, ResponseHelper response) => + { + if (error != null) { deferred.Reject(error); } else { deferred.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) + private static Action GetCallback(Promise.Deferred deferred) { - if (error != null && response != null) { - error.ServerMessage = response.Error ?? error.Message; - } - promise.Promisify(error, body); + return (RequestException error, ResponseHelper response, T body) => + { + if (error != null && response != null) + { + error.ServerMessage = response.Error ?? error.Message; + } + if (error != null) { deferred.Reject(error); } else { deferred.Resolve(body); } + }; } #endregion diff --git a/src/Proyecto26.RestClient/packages.config b/src/Proyecto26.RestClient/packages.config index 1055811..057458f 100644 --- a/src/Proyecto26.RestClient/packages.config +++ b/src/Proyecto26.RestClient/packages.config @@ -1,4 +1,5 @@  - + + \ No newline at end of file