Exemplo n.º 1
0
        public async Task <T> PerformAsync <T>(Method paramMethod, Object paramObject) where T : new()
        {
            RestClient localClient = RestClientHelper.GetDefaultRestClient(new Uri(TerodoViewApiBase.UrlEndpoint));

            localClient.AddDefaultHeaders();
            if (String.IsNullOrEmpty(TerodoViewApiBase.Token?.token))
            {
                await(new TerodoViewApiToken()).GetTokenAsync();
            }

            RestRequest localRequest = RestClientHelper.GetDefaultRestRequest(Resource, paramMethod, paramObject);

            localRequest.AddAuthencationHeaders();

            try
            {
                IRestResponse localResponse = await RestClientBase.ExecuteCallAsync(localClient, localRequest);

                return(JsonConvert.DeserializeObject <T>(localResponse.Content, RestClientHelper.DefaultJsonSerializerSettings));
            }
            finally
            {
                localClient  = null;
                localRequest = null;
            }
        }
Exemplo n.º 2
0
        public async Task Logout()
        {
            var localRestClient = RestClientHelper.GetDefaultRestClient(new Uri(TerodoViewApiBase.UrlEndpoint));
            var localTerodoViewOAuth2Request = new TerodoViewOAuth2Request()
            {
                email = TerodoViewApiBase.Username, password = TerodoViewApiBase.Password
            };
            var localRequest = RestClientHelper.GetDefaultRestRequest(TerodoViewApiBase.LogoutEndPoint, Method.DELETE, null);

            localRequest.AddAuthencationHeaders();
            await RestClientBase.ExecuteCallAsync(localRestClient, localRequest);
        }
Exemplo n.º 3
0
        public async Task GetTokenAsync()
        {
            var localRestClient  = RestClientHelper.GetDefaultRestClient(new Uri(TerodoViewApiBase.UrlEndpoint));
            var localCredentials = new TerodoViewOAuth2Request()
            {
                email = TerodoViewApiBase.Username, password = TerodoViewApiBase.Password
            };
            var           localRequest      = RestClientHelper.GetDefaultRestRequest(TerodoViewApiBase.LoginEndPoint, Method.POST, localCredentials);
            IRestResponse localRestResponse = await RestClientBase.ExecuteCallAsync(localRestClient, localRequest);

            TerodoViewApiBase.Token = new TerodoViewOAuth2Token();
            try
            {
                TerodoViewApiBase.Token.token       = (string)localRestResponse.Headers.FirstOrDefault(paramX => paramX.Name == "Access-Token")?.Value.ToString();
                TerodoViewApiBase.Token.tokenclient = (string)localRestResponse.Headers.FirstOrDefault(paramX => paramX.Name == "Client")?.Value.ToString();
                TerodoViewApiBase.Token.tokenexpiry = int.Parse(localRestResponse.Headers.FirstOrDefault(paramX => paramX.Name == "Expiry")?.Value.ToString() ?? throw new InvalidOperationException());
                TerodoViewApiBase.Token.tokentype   = (string)localRestResponse.Headers.FirstOrDefault(paramX => paramX.Name == "Token-Type")?.Value.ToString();
                TerodoViewApiBase.Token.tokenuid    = (string)localRestResponse.Headers.FirstOrDefault(paramX => paramX.Name == "Uid")?.Value.ToString();
            }
            catch (Exception localEx)
            {
                throw new Exception($"Error setting token from request headers: {localEx.GetBaseException().Message}");
            }
        }