Exemplo n.º 1
0
        private async Task <bool> renewToken()
        {
            var user = User;

            if (user != null)
            {
                LoggerService.Instance.Log("INFO: RestService: user token has expired: renewing");
                MultipartFormDataContent content = new MultipartFormDataContent
                {
                    { new StringContent(user.AuthToken), "auth_token" }
                };

                try
                {
                    RestRequestResponse response = await MakeRecorderAPIRestRequest("login/at", RequestMethod.POST, content, false);

                    if ((response != null) && response.Success)
                    {
                        var result = response.Data.ToObject <User>();
                        if (result != null)
                        {
                            LoggerService.Instance.Log("INFO: RestService: token renewed");
                            user.Token = result.Token;
                            user.Email = result.Email;
                            user.Name  = result.FirstName + " " + result.LastName;
                            UserStoreService.Instance.SaveUserCredentials(user);
                            return(true);
                        }
                        else
                        {
                            LoggerService.Instance.Log("ERROR: RestService: unable to read user object from response");
                        }
                    }
                    else
                    {
                        LoggerService.Instance.Log("ERROR: RestService: failed to renew user token");
                    }
                }
                catch (Exception ex)
                {
                    LoggerService.Instance.Log("ERROR: RestService renewToken exception: " + ex);
                }
            }

            //allow only 5 requests per minute
            if (DateTime.UtcNow > RateLimitRetryAfter)
            {
                RateLimitRetryAfter = DateTime.UtcNow.Add(TimeSpan.FromSeconds(12));
            }

            return(false);
        }
Exemplo n.º 2
0
        public async Task <T> MakeRecorderAPIRestRequest <T>(string relativeUrl, RequestMethod requestMethod, HttpContent httpContent)
        {
            try
            {
                RestRequestResponse response = await MakeRecorderAPIRestRequest(relativeUrl, requestMethod, httpContent, true);

                if ((response != null) && response.Success)
                {
                    return(response.Data.ToObject <T>());
                }
            }
            catch (Exception ex)
            {
                LoggerService.Instance.Log("ERROR: MakeRecorderAPIRestRequest (Task<T>): Exception: " + ex + ". URL: " + relativeUrl);
            }

            return(default(T));
        }