示例#1
0
        static async Task <TokensResponse> AuthenticationMenu(ITesonetApi api)
        {
            TokensResponse        token       = null;
            CredentialsPayload    credentials = null;
            AuthenticationManager authManager = new AuthenticationManager(api);

            while (token == null)
            {
                credentials = UserInterface.AskForCredentials();

                token = await authManager.GetTokenAsync(credentials);

                if (token == null)
                {
                    UserInterface.InformAboutAuthorizationFailure();

                    if (UserInterface.AskIfExit())
                    {
                        break;
                    }
                }
            }

            return(token);
        }
示例#2
0
        static async Task RunAsync()
        {
            ITesonetApi tesonetApi = new TesonetApi();

            ServerManager serverManager = new ServerManager(tesonetApi);

            TokensResponse token = await AuthenticationMenu(tesonetApi);

            await AuthorizedUserMenu(serverManager, token);
        }
示例#3
0
        private static async Task AuthorizedUserMenu(ServerManager serverManager, TokensResponse token)
        {
            IServersRepository    serversRepository = new ServersRepository();
            List <ServerResponse> serversList;

            bool isUserExited = false;

            while (!isUserExited)
            {
                switch (UserInterface.AskWhichListToGet())
                {
                case 1:
                    serversList = await serverManager.GetAllRecordsAsync(token);

                    if (serversList != null)
                    {
                        UserInterface.DisplayListToConsole(serversList);
                        await serversRepository.StoreAllServersIntoDBAsync(serversList);
                    }
                    else
                    {
                        UserInterface.InformAboutAPIFailure();
                    }
                    break;

                case 2:
                    serversList = await serversRepository.GetAllServersFromDBAsync();

                    if (serversList != null)
                    {
                        UserInterface.DisplayListToConsole(serversList);
                    }
                    else
                    {
                        UserInterface.InformAboutDBFailure();
                    }
                    break;

                default:
                    UserInterface.InformAboutWrongInput();
                    break;
                }

                if (UserInterface.AskIfExit())
                {
                    isUserExited = true;
                }
            }
        }
示例#4
0
        public async Task <TokensResponse> GetTokenAsync(CredentialsPayload credentials)
        {
            TokensResponse      token    = null;
            HttpResponseMessage response = await api.PostTokensAsync(credentials);

            if (response.IsSuccessStatusCode)
            {
                token = await response.Content.ReadAsAsync <TokensResponse>();

                logger.Info("Authentication successful. Status code: {0}", (int)response.StatusCode);
            }
            else
            {
                logger.Error("Authentication is not successful. Status code: {0}", (int)response.StatusCode);
            }

            return(token);
        }
示例#5
0
        public async Task <List <ServerResponse> > GetAllRecordsAsync(TokensResponse token)
        {
            List <ServerResponse> recordList = null;

            HttpResponseMessage response = await api.GetServersAsync(token);

            if (response.IsSuccessStatusCode)
            {
                recordList = await response.Content.ReadAsAsync <List <ServerResponse> >();

                logger.Info("Successfully returned {0} records from API. Status code: {1}", recordList.Count, (int)response.StatusCode);
            }
            else
            {
                logger.Error("Failed to get servers from API. Status code: {0}", (int)response.StatusCode);
            }

            return(recordList.OrderByDescending(s => s.Distance).ToList());
        }