Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Net.Http;
- using System.Threading.Tasks;
- using CPOD.Helpers;
- using CPOD.Interfaces;
- using CPOD.Models;
- using Xamarin.Forms;
- namespace CPOD.Services
- {
- class LoginPageService : ILoginPageService
- {
- private readonly HttpClient _httpClient;
- public LoginPageService()
- {
- _httpClient = new HttpClient { BaseAddress = new Uri("https://www.mycpod.com/api/") };
- }
- public async Task<bool> LoginTask(string username, string password)
- {
- try
- {
- var response = await _httpClient.GetAsync($"mobile?cmd=login&username={username}&password={password}");
- if (response.IsSuccessStatusCode)
- {
- var content = await response.Content.ReadAsStringAsync();
- //DataSingleton.Instance.ResultLogin = content.ParseXml<User>();
- Application.Current.Properties["LoginResult"] = content.ParseXml<User>();
- var user = (User) Application.Current.Properties["LoginResult"];
- if (user != null && !user.Status.Equals("nok")) //TODO: Implement error log
- {
- var authCode = user.AuthCode;
- var agentId = user.Agents.First().Id; //TODO: Implement using several agents
- var downloadData = await AgentStatusTask(authCode, agentId);
- if (downloadData)
- return true;
- }
- }
- }
- catch (Exception exception)
- {
- LoggerHelper.ShowErrorMessage(exception);
- }
- return false;
- }
- public async Task<bool> AgentStatusTask(int authCode, int agentId)
- {
- try
- {
- var response = await _httpClient.GetAsync($"mobile?cmd=agentstatus&authcode={authCode}&agentid={agentId}");
- if (response.IsSuccessStatusCode)
- {
- var content = await response.Content.ReadAsStringAsync();
- //DataSingleton.Instance.ResultAgentStatus = content.ParseXml<AgentStatus>();
- Application.Current.Properties["AgentStatus"] = content.ParseXml<AgentStatus>();
- return true;
- }
- }
- catch (Exception exception)
- {
- LoggerHelper.ShowErrorMessage(exception);
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement