Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Net.Http;
  4. using System.Threading.Tasks;
  5. using CPOD.Helpers;
  6. using CPOD.Interfaces;
  7. using CPOD.Models;
  8. using Xamarin.Forms;
  9.  
  10. namespace CPOD.Services
  11. {
  12.     class LoginPageService : ILoginPageService
  13.     {
  14.  
  15.         private readonly HttpClient _httpClient;
  16.  
  17.         public LoginPageService()
  18.         {
  19.             _httpClient = new HttpClient { BaseAddress = new Uri("https://www.mycpod.com/api/") };
  20.  
  21.         }
  22.         public async Task<bool> LoginTask(string username, string password)
  23.         {
  24.             try
  25.             {
  26.                 var response = await _httpClient.GetAsync($"mobile?cmd=login&username={username}&password={password}");
  27.                 if (response.IsSuccessStatusCode)
  28.                 {
  29.                     var content = await response.Content.ReadAsStringAsync();
  30.                     //DataSingleton.Instance.ResultLogin = content.ParseXml<User>();
  31.                     Application.Current.Properties["LoginResult"] = content.ParseXml<User>();
  32.                     var user = (User) Application.Current.Properties["LoginResult"];
  33.                     if (user != null && !user.Status.Equals("nok")) //TODO: Implement error log
  34.                     {
  35.                         var authCode = user.AuthCode;
  36.                         var agentId = user.Agents.First().Id;    //TODO: Implement using several agents
  37.                         var downloadData = await AgentStatusTask(authCode, agentId);
  38.                         if (downloadData)
  39.                             return true;
  40.                     }
  41.                 }
  42.             }
  43.             catch (Exception exception)
  44.             {
  45.                 LoggerHelper.ShowErrorMessage(exception);
  46.             }
  47.             return false;
  48.         }
  49.  
  50.         public async Task<bool> AgentStatusTask(int authCode, int agentId)
  51.         {
  52.             try
  53.             {
  54.                 var response = await _httpClient.GetAsync($"mobile?cmd=agentstatus&authcode={authCode}&agentid={agentId}");
  55.                 if (response.IsSuccessStatusCode)
  56.                 {
  57.                     var content = await response.Content.ReadAsStringAsync();
  58.                     //DataSingleton.Instance.ResultAgentStatus = content.ParseXml<AgentStatus>();
  59.                     Application.Current.Properties["AgentStatus"] = content.ParseXml<AgentStatus>();
  60.                     return true;
  61.                 }
  62.             }
  63.             catch (Exception exception)
  64.             {
  65.                 LoggerHelper.ShowErrorMessage(exception);
  66.             }
  67.             return false;
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement