// +--------------------------------+ // | Creation / Destruction Helpers | // +--------------------------------+ static Account CreateAccount(CTM.AuthToken token) { Account settings = new Account(token); settings.name = "API Test Account"; settings.website = "API Test Website"; settings.timezone = "Eastern Standard Time"; settings.shared_billing = true; Account new_account = settings.create(); PrintAccount(new_account); return new_account; }
// +----------------------------+ // | Editing / Updating Helpers | // +----------------------------+ static void UpdateAccount(Account account) { if (account.error != null){ Console.WriteLine("Account has error: " + account.error); return; } PrintAccount(account); string oldname = account.name; string oldsite = account.website; string oldtz = account.timezone; account.name = "Changed the name!"; account.website = "http://www.example.com/ctm-api"; account.timezone = "UTC"; if (!account.save()){ Console.WriteLine("Error saving acount: " + account.error); account.reload(); return; } else{ account.reload(); } PrintAccount(account); account.name = oldname; account.website = oldsite; account.timezone = oldtz; if (!account.save()){ Console.WriteLine("Error saving acount: " + account.error); account.reload(); return; } else{ account.reload(); } PrintAccount(account); }
// +------------------+ // | Printing Helpers | // +------------------+ static void PrintAccount(Account account) { Console.WriteLine(); if (account.error == null){ Console.WriteLine("Account: " + account.id); Console.WriteLine(" Name: " + account.name); Console.WriteLine(" Status: " + account.status); Console.WriteLine(" Website: " + account.website); Console.WriteLine("Timezone: " + account.timezone); Console.WriteLine(" Billing: " + (account.shared_billing ? "Shared" : "Separate")); Console.WriteLine(" Balance: $" + (int)(account.balance / 100) + "." + (int)(account.balance % 100)); } else{ Console.WriteLine("Error in account: " + account.error); } }
/* * List accounts accessible to the given auth token */ public static Page<Account> list(CTM.AuthToken token, int page=0, string status="active") { CTM.Request request = new CTM.Request(CTM.Config.Endpoint() + "/accounts.json", token); Hashtable parameters = new Hashtable(); parameters["page"] = page.ToString(); parameters["status"] = status.ToString(); CTM.Response res = request.get(parameters); if (res.error != null){ return new Page<Account>(res.error); } else { int index = 0; Account[] accounts = new Account[res.data.accounts.Count]; foreach (JObject account in res.data.accounts.Children<JToken>()) { accounts[index++] = new Account(account, token); } return new Page<Account>(accounts, (int)res.data.page, (int)res.data.total_entries, (int)res.data.total_pages); } }
/* * Return a special account for dealing with error messages without crossing types */ public static Account ErrorAccount(CTM.AuthToken token, string error, string account_id = null) { Account account = new Account(account_id, "", "", token); account.error = error; return account; }