Skip to content

Instantly share code, notes, and snippets.

@RupertAvery
Created April 29, 2021 00:36
Show Gist options
  • Select an option

  • Save RupertAvery/cc7f40cbb325833d4b0a20da03129b9d to your computer and use it in GitHub Desktop.

Select an option

Save RupertAvery/cc7f40cbb325833d4b0a20da03129b9d to your computer and use it in GitHub Desktop.
EasyTrip Rest Client
public class Account
{
public string Result { get; set; }
public long CustAccountID { get; set; }
public int AccountType { get; set; }
public int CustomerID { get; set; }
public string Status { get; set; }
public float Balance { get; set; }
public float InitBalance { get; set; }
public float ThresholdAmount { get; set; }
public bool AutoDebit { get; set; }
//public object BankName { get; set; }
//public object BankCardNo { get; set; }
//public object CardExpiryMonth { get; set; }
//public object CardExpiryYear { get; set; }
//public object CVC2 { get; set; }
public float RPSThresholdAmount { get; set; }
//public object RPSAmount { get; set; }
//public object BillingCycle { get; set; }
//public object BillingCycleDayFrom { get; set; }
//public object BillingCycleDayTo { get; set; }
//public object CreditLimit { get; set; }
public string AccType { get; set; }
public string AccStatus { get; set; }
public string AccountStatus { get; set; }
public string FullName { get; set; }
public int TagCount { get; set; }
}
public class EasyTripClient
{
private readonly RestClient _client;
public string CustomerNumber { get; private set; }
public bool IsLoggedIn { get; private set; }
public EasyTripClient()
{
_client = new RestClient("https://myeasytripcams.easytrip.ph");
_client.CookieContainer = new CookieContainer();
}
public void Login(string username, string password)
{
var pageRequest = new RestRequest("CAMS");
var pageResponse = _client.Get(pageRequest);
var rvtRegex = new Regex("<input name=\"__RequestVerificationToken\" type=\"hidden\" value=\"(.*?)\" />");
var rvtMatch = rvtRegex.Match(pageResponse.Content);
string requestVerifcationToken = null;
if (rvtMatch.Success)
{
requestVerifcationToken = rvtMatch.Groups[1].Value;
}
var request = new RestRequest("CAMS/?ReturnUrl=%2FCAMS%2FCustomer%2FIndex");
request.AddParameter("__RequestVerificationToken", requestVerifcationToken);
request.AddParameter("UserName", username);
request.AddParameter("Password", password);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
var response = _client.Post(request);
if (response.IsSuccessful)
{
CustomerNumber = null;
var custNumRegex = new Regex(@"getIndividualCustomer\((\d+)\)");
var custNumMatch = custNumRegex.Match(response.Content);
if (custNumMatch.Success)
{
CustomerNumber = custNumMatch.Groups[1].Value;
}
}
}
public void LogOut()
{
var logoffRequest = new RestRequest("CAMS/Account/Logoff");
_client.Get(logoffRequest);
}
public IEnumerable<Account> GetAccounts()
{
var accountRequest = new RestRequest("CAMS/Account/GetAccounts");
accountRequest.AddJsonBody(new { CustomerID = CustomerNumber });
accountRequest.AddHeader("content-type", "application/json");
var accountResponse = _client.Post(accountRequest);
if (accountResponse.IsSuccessful)
{
var jsonResponse = JObject.Parse(accountResponse.Content);
var payload = jsonResponse["data"].Value<string>();
var accounts = JArray.Parse(payload);
foreach (var account in accounts)
{
yield return account.ToObject<Account>();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment