private void btn_Test_Click(object sender, EventArgs e) { DespatchBayAPI despatchBay = new DespatchBayAPI(txt_ApiUser.Text, txt_ApiKey.Text); AccountService MyAccountService = new AccountService(); MyAccountService = despatchBay.AuthenticateWithDespatchBay(MyAccountService); string message = "Successfully Connected"; try { AccountType MyAccount = new AccountType(); MyAccount = MyAccountService.GetAccount(); } catch { message = "Connection Failed"; } MessageBox.Show(message); }
/// <summary> /// This function connects to DespatchBay and pulls regularily used data and stores it in sqlite /// </summary> public void BuildApplicationCache() { // Create our local sqlite cache database SQLiteConnection sQLiteConnection; string ApiUser = ""; string ApiKey = ""; string dbPath = System.IO.Path.Combine( System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData), "DespatchBayCache.db3"); sQLiteConnection = new SQLiteConnection(dbPath); sQLiteConnection.CreateTable <Account>(); sQLiteConnection.DeleteAll <Account>(); sQLiteConnection.CreateTable <SenderAddress>(); sQLiteConnection.DeleteAll <SenderAddress>(); sQLiteConnection.CreateTable <AddressService>(); sQLiteConnection.DeleteAll <AddressService>(); sQLiteConnection.CreateTable <DespatchBayCredentials>(); sQLiteConnection.CreateTable <Service>(); sQLiteConnection.DeleteAll <Service>(); sQLiteConnection.CreateTable <Courier>(); sQLiteConnection.DeleteAll <Courier>(); List <DespatchBayCredentials> credentials = sQLiteConnection.Query <DespatchBayCredentials>("SELECT * FROM DespatchBayCredentials WHERE `IsEnabled`= 1 ORDER BY `UpdatedDateTime` DESC LIMIT 1"); if (credentials.Count == 1) { foreach (var latestCreds in credentials) { ApiUser = latestCreds.ApiUser; ApiKey = latestCreds.ApiKey; } } else { MessageBox.Show("No valid credentials saved"); return; } AccountService MyAccountService = new AccountService(); DespatchBayAPI despatchBay = new DespatchBayAPI(ApiUser, ApiKey); MyAccountService = despatchBay.AuthenticateWithDespatchBay(MyAccountService); AccountType MyAccount = new AccountType(); try { // Fetch an populate account table MyAccount = MyAccountService.GetAccount(); AccountBalanceType accountBalanceType = new AccountBalanceType(); accountBalanceType = MyAccount.AccountBalance; Account accountRecord = new Account { AccountID = MyAccount.AccountID, AccountName = MyAccount.AccountName, Balance = accountBalanceType.Balance, AvailableBalance = accountBalanceType.AvailableBalance, CreatedDateTime = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss"), UpdatedDateTime = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") }; sQLiteConnection.Insert(accountRecord); // Update tool strip toolStripAccountIDLabel.Text = "Account ID: " + MyAccount.AccountID.ToString(); toolStripBalanceLabel.Text = "Balance: " + accountBalanceType.Balance.ToString() + "CR"; toolStripAvailableBalanceLabel.Text = "Available Balance: " + accountBalanceType.AvailableBalance.ToString() + "CR"; // Fetch and populate Sender Addresses SenderAddressType[] SenderAddresses = new SenderAddressType[] { }; SenderAddresses = MyAccountService.GetSenderAddresses(); SenderAddress senderAddressRecord = new SenderAddress(); foreach (var addressDetail in SenderAddresses) { senderAddressRecord.ExternalID = addressDetail.SenderAddressID; senderAddressRecord.SenderEmail = addressDetail.SenderEmail; senderAddressRecord.SenderName = addressDetail.SenderName; senderAddressRecord.SenderTelephone = addressDetail.SenderTelephone; com.despatchbay.api.account.AddressType address = new com.despatchbay.api.account.AddressType(); address = addressDetail.SenderAddress; AddressService addressRecord = new AddressService(); addressRecord.CompanyName = address.CompanyName; addressRecord.CountryCode = address.CountryCode; addressRecord.County = address.County; addressRecord.Locality = address.Locality; addressRecord.PostalCode = address.PostalCode; addressRecord.Street = address.Street; addressRecord.TownCity = address.Street; addressRecord.ID = senderAddressRecord.ExternalID; sQLiteConnection.Insert(addressRecord); sQLiteConnection.Insert(senderAddressRecord); } // Fetch and populate Services ServiceType[] Services = new ServiceType[] { }; Services = MyAccountService.GetServices(); Service serviceRecord = new Service(); foreach (var service in Services) { serviceRecord.ServiceID = service.ServiceID; serviceRecord.Format = service.Format; serviceRecord.Name = service.Name; serviceRecord.Cost = service.Cost; // serviceRecord.CourierID = service.Courier; CourierType courier = new CourierType(); courier = service.Courier; serviceRecord.CourierID = courier.CourierID; try { Courier courierRecord = new Courier(); courierRecord.CourierID = courier.CourierID; courierRecord.CourierName = courier.CourierName; courierRecord.CreatedDateTime = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss"); courierRecord.UpdatedDateTime = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss"); sQLiteConnection.Insert(courierRecord); } catch { } serviceRecord.CreatedDateTime = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss"); serviceRecord.UpdatedDateTime = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss"); sQLiteConnection.Insert(serviceRecord); } } catch { MessageBox.Show("Connection Error - Check credentials and try again"); } }