/// <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"); } }
/// <summary> /// Example to Get available services, Add a shipment to the account and the book the collection /// </summary> /// <param name="args"></param> public static void Main(string[] args) { LoadConfiguration(); int count = 0; ServiceType[] availableServices = null; try { // First we need to build a shipment request object ShipmentRequestType Shipment = new ShipmentRequestType(); //shipment.ServiceID = null; //shipment.ClientReference = null; //shipment.FollowShipment = null; ParcelType Parcel = new ParcelType(); Parcel.Height = 10; // This is cm Parcel.Length = 10; // This is cm Parcel.Width = 10; // This is cm Parcel.Weight = 10; // This is kg Parcel.Value = 100; // This is GBP Parcel.Contents = "New Dress for Klingor"; // Obviously we could add more parcels here ParcelType[] Parcels = new ParcelType[1]; Parcels[0] = Parcel; AddressType Address = new AddressType(); // Receipient Details RecipientAddressType RecipientAddress = new RecipientAddressType(); RecipientAddress.RecipientName = "Cprl Klingor"; RecipientAddress.RecipientEmail = "*****@*****.**"; RecipientAddress.RecipientTelephone = "01522 76767676"; Address.CompanyName = "The SaleGroup"; Address.Street = "Unit 6 The Regatta"; Address.Locality = "Henley Way"; Address.TownCity = "Lincoln"; Address.County = "Lincolnshire"; Address.PostalCode = "LN6 3QR"; Address.CountryCode = "GB"; RecipientAddress.RecipientAddress = Address; SenderAddressType SenderAddress = new SenderAddressType(); SenderAddress.SenderName = "Hawkeye Pearce"; SenderAddress.SenderEmail = "*****@*****.**"; SenderAddress.SenderTelephone = "01522 000000"; Address.Street = "West Parade "; Address.TownCity = "Lincoln"; Address.PostalCode = "LN1 1YP"; Address.County = "Lincolnshire"; Address.CountryCode = "GB"; SenderAddress.SenderAddress = Address; // Put the Shipment together Shipment.Parcels = Parcels; Shipment.RecipientAddress = RecipientAddress; Shipment.SenderAddress = SenderAddress; // Call the service availableServices = GetAvailableServicesMethod(Shipment); // iterate though the list of returned services count = 0; foreach (ServiceType element in availableServices) { count += 1; if (count == 1) { // Manually apply the First service Shipment.ServiceID = element.ServiceID; // Not sure why, but errors with ServiceID = 0 // if ServiceIDSpecified is Not set to true; Shipment.ServiceIDSpecified = true; } System.Console.WriteLine("Service id:{0} - {1} £{2}", element.ServiceID, element.Name, element.Cost); } Shipment.ClientReference = "Dummy Client Ref"; // We need a collection Date CollectionDateType CollectionDate = new CollectionDateType(); // Set the Collection date for tomorrow CollectionDate.CollectionDate = DateTime.Today.AddDays(1).ToString("yyyy-MM-dd"); Shipment.CollectionDate = CollectionDate; // Add Shipment String ShipmentID = AddShipmentMethod(Shipment); Console.WriteLine(ShipmentID); // Book collection // This is deliberatly written to handle one booking request String [] Bookings = new String[1]; Bookings[0] = ShipmentID; ShipmentReturnType[] BookingResults = null; BookingResults = BookShipmentsMethod(Bookings); // iterate though the list of returned services count = 0; foreach (ShipmentReturnType element in BookingResults) { count += 1; System.Console.WriteLine("Service id:{0} - ShipmentID {1} - Label Url{2}", element.ServiceID, element.ShipmentID, element.LabelsURL); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }