/
DataRetriever.cs
116 lines (98 loc) · 3.34 KB
/
DataRetriever.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Linq;
using System.Net.Http;
using CMcG.Bankr.Data;
using System.Threading.Tasks;
using CMcG.Bankr.Logic.Queries;
namespace CMcG.Bankr.Logic
{
public class DataRetriever
{
const string Url = "https://www.my.commbank.com.au/mobile/i/default.aspx";
public AppStatus Status { get; set; }
public Action Callback { get; set; }
string m_sessionId;
public bool CanLoadData
{
get { return LogonQuery.GetLogonDetails() != null; }
}
static Uri m_uri;
async Task<Uri> GetUri()
{
if (m_uri != null)
return m_uri;
var findResult = await new HttpClient().GetAsync(Url);
m_uri = new Uri("https://" + findResult.RequestMessage.RequestUri.Host);
return m_uri;
}
public async Task<HttpClient> Login()
{
var uri = await GetUri();
var client = new HttpClient { BaseAddress = uri };
m_sessionId = await RunQuery<LogonQuery>(client);
return client;
}
public async void LoadData()
{
var client = await Login();
if (m_sessionId != null)
{
await RunQuery<GetUpcomingTransactionsQuery>(client);
await RunQuery<GetTransactionsQuery>(client);
}
Callback();
}
public bool CanLoadTransferAccounts
{
get { return LogonQuery.GetLogonDetails() != null; }
}
public async void LoadTransferAccounts()
{
var client = await Login();
if (m_sessionId != null)
{
await RunQuery<GetTransferAccountsQuery>(client);
}
Callback();
}
async Task<string> RunQuery<T>(HttpClient client) where T : CommBankQuery, new()
{
return await new T
{
Status = Status,
SessionId = m_sessionId
}.Start(client);
}
public bool CanTransferMoney
{
get { return LogonQuery.GetLogonDetails() != null; }
}
public async Task<string> TransferMoney(int fromAccount, int toAccount, string description, decimal amount)
{
var client = await Login();
if (m_sessionId == null)
return null;
await RunQuery<GetTransferAccountsQuery>(client);
using (var store = new DataStoreContext())
{
var fromData = store.Accounts.First(x => x.Id == fromAccount);
var fromSendData = store.TransferToAccounts.First(x => x.Id == fromAccount && x.AccountName == fromData.AccountName);
fromAccount = fromSendData.SenderId ?? -1;
}
var query = new TransferMoneyQuery
{
Status = Status,
SessionId = m_sessionId,
FromAccountId = fromAccount,
ToAccountId = toAccount,
Description = description,
Amount = amount,
IsValidate = true
};
await query.Start(client);
query.IsValidate = false;
await query.Start(client);
return query.ReceiptNo;
}
}
}