Example #1
0
        public async Task <IMbankResponse <IList <Transaction> > > GetTransactions(CancellationToken cancellationToken)
        {
            if (Client.DefaultParameters.All(p => p.Name != _tokenParameterName) || Client.DefaultParameters.All(p => p.Name != _tabIdParameterName))
            {
                throw new InvalidOperationException("You must Login before using this method");
            }

            var transactionHistoryRequest  = new RestRequest("/Pfm/TransactionHistory", Method.GET);
            var transactionHistoryResponse = await Client.ExecuteTaskAsync(transactionHistoryRequest, cancellationToken);

            if (transactionHistoryResponse.StatusCode != HttpStatusCode.OK)
            {
                return(MbankResponse <IList <Transaction> > .Failed(transactionHistoryResponse));
            }

            var transactionHistoryHtmlDocument = new HtmlDocument();

            transactionHistoryHtmlDocument.LoadHtml(transactionHistoryResponse.Content);
            var nodes = transactionHistoryHtmlDocument.DocumentNode.SelectNodes("//ul[@class=\"content-list-body\"]/li");

            var transactions = nodes.Select(node => new Transaction()
            {
                Id       = node.GetAttributeValue("data-id", null),
                Type     = HttpUtility.HtmlDecode(node.SelectSingleNode("header/div[@class=\"column type\"]")?.InnerText?.Trim('\r', '\n', ' ')),
                Date     = JToken.FromObject(node.GetAttributeValue("data-timestamp", null)).ToObject <DateTime>(),
                Title    = HttpUtility.HtmlDecode(node.SelectSingleNode("header/div[@class=\"column description\"]/span/span/@data-original-title")?.InnerText),
                Category = HttpUtility.HtmlDecode(node.SelectSingleNode("header/div[@class=\"column category\"]/div[1]/span")?.InnerText),
                Amount   = double.Parse(node.GetAttributeValue("data-amount", null).Replace(',', '.'), CultureInfo.InvariantCulture),
                Currency = node.GetAttributeValue("data-currency", null)
            });

            return(new MbankResponse <IList <Transaction> >(transactionHistoryResponse, true, new List <Transaction>(transactions)));
        }
Example #2
0
        public async Task <IMbankResponse <AccountInfo> > GetAccountInfo(CancellationToken cancellationToken)
        {
            if (Client.DefaultParameters.All(p => p.Name != _tokenParameterName) || Client.DefaultParameters.All(p => p.Name != _tabIdParameterName))
            {
                throw new InvalidOperationException("You must Login before using this method");
            }
            var accountsListRequest = new RestRequest("/Accounts/Accounts/List", Method.POST);

            accountsListRequest.AddParameter("X-Requested-With", "XMLHttpRequest", ParameterType.HttpHeader);

            var accountsListResponse = await Client.ExecuteTaskAsync(accountsListRequest, cancellationToken);

            if (accountsListResponse.StatusCode != HttpStatusCode.OK)
            {
                return(MbankResponse <AccountInfo> .Failed(accountsListResponse));
            }

            var jobject     = JsonConvert.DeserializeObject <JObject>(accountsListResponse.Content);
            var accountInfo = jobject["properties"].ToObject <AccountInfo>();

            accountInfo.Name = jobject["name"].Value <string>();
            return(new MbankResponse <AccountInfo>(accountsListResponse, true, accountInfo));
        }
Example #3
0
        public async Task <IMbankResponse <LoginInfo> > Login(string login, string password, AccountType accountType, CancellationToken cancellationToken)
        {
            Client.DefaultParameters.Clear();
            var loginRequest = new RestRequest("/Account/JsonLogin", Method.POST);

            loginRequest.AddParameter("UserName", login, ParameterType.QueryString);
            loginRequest.AddParameter("Password", password, ParameterType.QueryString);
            loginRequest.AddParameter("Seed", "", ParameterType.QueryString);
            loginRequest.AddParameter("Lang", "", ParameterType.QueryString);
            var loginResponse = await Client.ExecuteTaskAsync(loginRequest, cancellationToken);

            if (loginResponse.StatusCode != HttpStatusCode.OK)
            {
                return(MbankResponse <LoginInfo> .Failed(loginResponse));
            }

            var loginInfo = JsonConvert.DeserializeObject <LoginInfo>(loginResponse.Content,
                                                                      new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });

            if (!loginInfo.Successful)
            {
                return(new MbankResponse <LoginInfo>(loginResponse, false, loginInfo));
            }

            Client.BaseUrl = new Uri($"{Client.BaseUrl.GetLeftPart(UriPartial.Scheme | UriPartial.Authority)}{loginInfo.RedirectUrl}");

            var tokenRequest  = new RestRequest();
            var tokenResponse = await Client.ExecuteTaskAsync(tokenRequest, cancellationToken);

            if (tokenResponse.StatusCode != HttpStatusCode.OK)
            {
                return(MbankResponse <LoginInfo> .Failed(tokenResponse));
            }

            var htmlDocument = new HtmlDocument();

            htmlDocument.LoadHtml(tokenResponse.Content);
            var tokenNode = htmlDocument.DocumentNode.SelectSingleNode("//meta[@name='__AjaxRequestVerificationToken']");
            var token     = tokenNode.GetAttributeValue("content", null);

            loginInfo.Token = token;

            Client.AddDefaultParameter("X-Request-Verification-Token", token, ParameterType.HttpHeader);
            Client.AddDefaultParameter("X-Tab-Id", loginInfo.TabId, ParameterType.HttpHeader);


            var activateAccountRequest = new RestRequest("/LoginMain/Account/JsonActivateProfile", Method.POST);

            switch (accountType)
            {
            case AccountType.Individual:
                activateAccountRequest.AddParameter("profileCode", "I", ParameterType.QueryString);
                break;

            case AccountType.Business:
                activateAccountRequest.AddParameter("profileCode", "B", ParameterType.QueryString);
                break;

            default:
                throw new NotImplementedException();
            }
            var activateAccountResponse = await Client.ExecuteTaskAsync(activateAccountRequest, cancellationToken);

            if (activateAccountResponse.StatusCode != HttpStatusCode.OK)
            {
                return(MbankResponse <LoginInfo> .Failed(activateAccountResponse));
            }

            return(new MbankResponse <LoginInfo>(tokenResponse, true, loginInfo));
        }