Exemplo n.º 1
0
        public async Task <bool> UpdateDeviceAsync(string devicePushToken = null, bool?isProduction = true)
        {
            var parameters = new Dictionary <string, object>();

            if (devicePushToken != null)
            {
                parameters["pushToken"] = devicePushToken;
            }

            if (isProduction != null)
            {
                parameters["isProduction"] = isProduction.Value;
            }

            if (parameters.Count() == 0)
            {
                return(false);
            }

            BuddyResult <IDictionary <string, object> > result = await ResultConversionHelper <IDictionary <string, object>, IDictionary <string, object> > (
                PatchAsync <IDictionary <string, object> > (
                    "/devices/current",
                    parameters));

            return(result.IsSuccess);
        }
Exemplo n.º 2
0
        internal BuddyResult <T2> Convert <T2>(Func <T, T2> map)
        {
            var br = new BuddyResult <T2>(RequestID, Error);

            if (IsSuccess && map != null)
            {
                br.Value = map(Value);
            }

            return(br);
        }
Exemplo n.º 3
0
        internal Task <BuddyResult <T2> > ResultConversionHelper <T1, T2>(
            Task <BuddyResult <T1> > result,
            Func <T1, T2> map = null,
            Action <BuddyResult <T1>, BuddyResult <T2> > completed = null)
        {
            BuddyResult <T1>         r1Result = null;
            Task <BuddyResult <T2> > task;

            if (typeof(T1) == typeof(T2))
            {
                task = result as Task <BuddyResult <T2> >;
            }
            else
            {
                task = result.ContinueWith <BuddyResult <T2> >(r1 =>
                {
                    r1Result = r1.Result;

                    if (map == null)
                    {
                        map = (t1) =>
                        {
                            return((T2)(object)t1);
                        };
                    }

                    return(r1Result.Convert <T2>(map));
                });
            }

            var tcs = new TaskCompletionSource <BuddyResult <T2> >();

            task.ContinueWith(r2 =>
            {
                if (completed == null)
                {
                    tcs.SetResult(r2.Result);
                }
                else
                {
                    PlatformAccess.Current.InvokeOnUiThread(() => { completed(r1Result, r2.Result); tcs.SetResult(r2.Result); });
                }
            });
            return(tcs.Task);
        }
Exemplo n.º 4
0
        internal async Task <BuddyResult <T> > HandleServiceResult <T>(BuddyCallResult <T> serviceResult, bool allowThrow = false)
        {
            var result = new BuddyResult <T> ();

            result.RequestID = serviceResult.RequestID;
            if (serviceResult.Error != null)
            {
                BuddyServiceException buddyException = null;
                switch (serviceResult.StatusCode)
                {
                case 0:
                    buddyException = new BuddyNoInternetException(serviceResult.Error);
                    break;

                case 401:
                case 403:
                    buddyException = new BuddyUnauthorizedException(serviceResult.Error, serviceResult.Message, serviceResult.ErrorNumber);
                    break;

                default:
                    buddyException = new BuddySDK.BuddyServiceException(serviceResult.Error, serviceResult.Message, serviceResult.ErrorNumber);
                    break;
                }
                TaskCompletionSource <bool> uiPromise = new TaskCompletionSource <bool> ();
                PlatformAccess.Current.InvokeOnUiThread(async() => {
                    var r = false;
                    if (await OnServiceException(this, buddyException))
                    {
                        r = true;
                    }
                    uiPromise.TrySetResult(r);
                });
                if (await uiPromise.Task && allowThrow)
                {
                    throw buddyException;
                }
                buddyException.StatusCode = serviceResult.StatusCode;
                result.Error = buddyException;
            }
            else
            {
                result.Value = serviceResult.Result;
            }
            return(result);
        }