public void FetchSessionToken(Action <FizzException> onFetch)
        {
            _requestQueue.Enqueue(onFetch);
            if (_requestQueue.Count > 1)
            {
                return;
            }

            _sessionRepository.FetchToken((session, ex) => {
                if (ex != null)
                {
                    while (_requestQueue.Count > 0)
                    {
                        FizzUtils.DoCallback(ex, _requestQueue.Dequeue());
                    }
                }
                else
                {
                    while (_requestQueue.Count > 0)
                    {
                        FizzUtils.DoCallback(null, _requestQueue.Dequeue());
                    }
                }
            });
        }
 private void ProcessResponse(string response,
                              FizzException ex,
                              Action <string, FizzException> onResult,
                              Action onRetry)
 {
     if (ex != null)
     {
         if (ex.Code == FizzError.ERROR_AUTH_FAILED)
         {
             FetchSessionToken(authEx =>
             {
                 if (authEx != null)
                 {
                     FizzUtils.DoCallback <string>(null, ex, onResult);
                 }
                 else
                 {
                     if (onRetry != null)
                     {
                         onRetry();
                     }
                 }
             });
         }
         else
         {
             FizzUtils.DoCallback <string>(null, ex, onResult);
         }
     }
     else
     {
         FizzUtils.DoCallback <string>(response, ex, onResult);
     }
 }
Esempio n. 3
0
        private static void FormatResponse(HttpStatusCode status, byte[] body, Action <string, FizzException> callback)
        {
            try
            {
                string buffer = Encoding.UTF8.GetString(body);
                switch (status)
                {
                case HttpStatusCode.OK:
                    FizzUtils.DoCallback(buffer, null, callback);
                    break;

                case HttpStatusCode.BadRequest:
                    FizzUtils.DoCallback(null, new FizzException(FizzError.ERROR_BAD_ARGUMENT, JSONNode.Parse(buffer)["reason"]), callback);
                    break;

                case HttpStatusCode.Unauthorized:
                    FizzUtils.DoCallback(null, new FizzException(FizzError.ERROR_AUTH_FAILED, JSONNode.Parse(buffer)["reason"]), callback);
                    break;

                default:
                    FizzUtils.DoCallback(null, new FizzException(FizzError.ERROR_REQUEST_FAILED, buffer), callback);
                    break;
                }
            }
            catch (Exception responseEx)
            {
                FizzUtils.DoCallback <string>(
                    null,
                    new FizzException(FizzError.ERROR_REQUEST_FAILED, responseEx.Message),
                    callback
                    );
            }
        }
        public void FetchToken(Action <FizzSession, FizzException> callback)
        {
            _sessionProvider.FetchToken(_userId, _locale, (session, exception) => {
                if (exception == null)
                {
                    Session = session;

                    FizzUtils.DoCallback(OnSessionUpdate);
                }

                FizzUtils.DoCallback <FizzSession>(session, exception, callback);
            });
        }
Esempio n. 5
0
        public void Get(string host,
                        string path,
                        IDictionary <string, string> headers,
                        Action <string, FizzException> callback)
        {
            Action <string, FizzException> onDone = FizzUtils.SafeCallback(callback);

            SendRequestAsync(host, path, "GET", headers, CONTENT_TYPE, 0, null, (status, response, ex) =>
            {
                if (ex != null)
                {
                    FizzUtils.DoCallback(null, ex, onDone);
                }
                else
                {
                    FormatResponse(status, response, onDone);
                }
            });
        }
Esempio n. 6
0
        public void FetchToken(string userId, string locale, Action <FizzSession, FizzException> callback)
        {
            if (string.IsNullOrEmpty(locale))
            {
                FizzUtils.DoCallback <FizzSession>(new FizzSession(null, null, 0), ERROR_INVALID_LOCALE, callback);
                return;
            }

            JSONClass node = new JSONClass();

            node["user_id"] = userId;
            node["locale"]  = locale;
            node["app_id"]  = _appId;

            string body    = node.ToString();
            string digest  = GenerateHmac(body, _appSecret);
            var    headers = new Dictionary <string, string>()
            {
                { "Authorization", "HMAC-SHA256 " + digest }
            };

            _restClient.Post(FizzConfig.API_BASE_URL, FizzConfig.API_PATH_SESSIONS, body, headers, (response, ex) =>
            {
                if (ex != null)
                {
                    FizzUtils.DoCallback(new FizzSession("", "", FizzUtils.Now()), ex, callback);
                }
                else
                {
                    try
                    {
                        FizzSession session = ParseSession(JSONNode.Parse(response));
                        FizzUtils.DoCallback(session, null, callback);
                    }
                    catch (Exception responseEx)
                    {
                        FizzLogger.E(responseEx.Message);
                        FizzUtils.DoCallback(new FizzSession("", "", FizzUtils.Now()), ERROR_SESSION_CREATION_FAILED, callback);
                    }
                }
            });
        }
Esempio n. 7
0
 public void FetchSessionToken(Action <FizzException> callback)
 {
     FizzUtils.DoCallback(null, callback);
 }