From edda4977379fec731bd28719e06a155e83d41a33 Mon Sep 17 00:00:00 2001 From: Jake Moening Date: Wed, 28 Aug 2019 21:02:22 -0500 Subject: [PATCH] #3 Add some more examples and Synchronous versions --- YNAB.SDK.Tests/YNAB_ExamplesShould.cs | 38 ++++- YNAB.SDK/examples/BudgetList.cs | 35 ----- YNAB.SDK/examples/BudgetListExample.cs | 61 ++++++++ YNAB.SDK/examples/BudgetMonth.cs | 50 ------- YNAB.SDK/examples/BudgetMonthExample.cs | 91 ++++++++++++ YNAB.SDK/examples/BulkTransactionCreate.cs | 21 +++ YNAB.SDK/examples/CategoryBalanceExample.cs | 55 +++++++ .../examples/CreateMultipleTransactions.cs | 21 +++ YNAB.SDK/examples/CreateTransaction.cs | 21 +++ YNAB.SDK/examples/DeltaRequestExample.cs | 134 ++++++++++++++++++ YNAB.SDK/examples/UpdateCategoryBudgeted.cs | 21 +++ YNAB.SDK/examples/UpdateTransaction.cs | 22 +++ 12 files changed, 483 insertions(+), 87 deletions(-) delete mode 100644 YNAB.SDK/examples/BudgetList.cs create mode 100644 YNAB.SDK/examples/BudgetListExample.cs delete mode 100644 YNAB.SDK/examples/BudgetMonth.cs create mode 100644 YNAB.SDK/examples/BudgetMonthExample.cs create mode 100644 YNAB.SDK/examples/BulkTransactionCreate.cs create mode 100644 YNAB.SDK/examples/CategoryBalanceExample.cs create mode 100644 YNAB.SDK/examples/CreateMultipleTransactions.cs create mode 100644 YNAB.SDK/examples/CreateTransaction.cs create mode 100644 YNAB.SDK/examples/DeltaRequestExample.cs create mode 100644 YNAB.SDK/examples/UpdateCategoryBudgeted.cs create mode 100644 YNAB.SDK/examples/UpdateTransaction.cs diff --git a/YNAB.SDK.Tests/YNAB_ExamplesShould.cs b/YNAB.SDK.Tests/YNAB_ExamplesShould.cs index 405075c..acb3c4a 100644 --- a/YNAB.SDK.Tests/YNAB_ExamplesShould.cs +++ b/YNAB.SDK.Tests/YNAB_ExamplesShould.cs @@ -22,11 +22,45 @@ public async Task Examples_RunSuccessfully() { }); var budgetMonthId = "2019-08-01"; var budgetId = new Guid("14235236-8085-4cf6-9fa6-92c34ed44b0c"); + var categoryId = new Guid("3b89df53-1869-4d2f-a636-d09eadc3f0ca"); // Act // Assert try { - await new BudgetList(_token).FetchBudgets(); - await new BudgetMonth(_token).FetchBudgetMonth(budgetId, budgetMonthId); + var ble = new BudgetListExample(_token); + ble.Execute(); + await ble.ExecuteAsync(); + + var bme = new BudgetMonthExample(_token); + bme.Execute(budgetId, budgetMonthId); + await bme.ExecuteAsync(budgetId, budgetMonthId); + + var btc = new BulkTransactionCreate(_token); + // btc.Execute(); + // await btc.ExecuteAsync(); + + var cbe = new CategoryBalanceExample(_token); + cbe.Execute(budgetId, categoryId); + await cbe.ExecuteAsync(budgetId, categoryId); + + var cmt = new CreateMultipleTransactions(_token); + // cmt.Execute(); + // await cmt.ExecuteAsync(); + + var ct = new CreateTransaction(_token); + // ct.Execute(); + // await ct.ExecuteAsync(); + + var dre = new DeltaRequestExample(_token); + dre.Execute(); + await dre.ExecuteAsync(); + + var ucb = new UpdateCategoryBudgeted(_token); + // ucb.Execute(); + // await ucb.ExecuteAsync(); + + var ut = new UpdateTransaction(_token); + // ut.Execute(); + // await ut.ExecuteAsync(); } catch (Exception ex) { Assert.True(false, ex.Message); } diff --git a/YNAB.SDK/examples/BudgetList.cs b/YNAB.SDK/examples/BudgetList.cs deleted file mode 100644 index d0d2206..0000000 --- a/YNAB.SDK/examples/BudgetList.cs +++ /dev/null @@ -1,35 +0,0 @@ -using YNAB.SDK; -using System.Threading.Tasks; -using System; - -namespace YNAB.SDK.Examples { - public class BudgetList { - private API _ynabAPI; - public BudgetList(string accessToken) - { - _ynabAPI = new API(accessToken); - } - - public async Task FetchBudgets() - { - try { - var budgetResponse = await _ynabAPI.Budgets.GetBudgetsAsync(); - var budgets = budgetResponse.Data.Budgets; - Console.WriteLine($"This user has {budgets.Count} budgets."); - Console.WriteLine(@" -=========== -BUDGET LIST -=========== -"); - budgets.ForEach(budget => { - Console.WriteLine( - $"[id: {budget.Id}, name: {budget.Name}, lastModifiedOn: {budget.LastModifiedOn}]" - ); - }); - } catch (Exception exception) { - Console.WriteLine(exception.Message); - throw; - } - } - } -} diff --git a/YNAB.SDK/examples/BudgetListExample.cs b/YNAB.SDK/examples/BudgetListExample.cs new file mode 100644 index 0000000..5d724f1 --- /dev/null +++ b/YNAB.SDK/examples/BudgetListExample.cs @@ -0,0 +1,61 @@ +using YNAB.SDK; +using System.Threading.Tasks; +using System; + +namespace YNAB.SDK.Examples { + public class BudgetListExample { + private readonly API _ynabAPI; + public BudgetListExample(string accessToken) + { + _ynabAPI = new API(accessToken); + } + + public async Task ExecuteAsync() + { + try { + var budgetResponse = await _ynabAPI.Budgets.GetBudgetsAsync(); + var budgets = budgetResponse.Data.Budgets; + Console.WriteLine($"This user has {budgets.Count} budgets."); + Console.WriteLine(@" +=========== +BUDGET LIST +=========== +"); + budgets.ForEach(budget => { + Console.WriteLine( + $"[id: {budget.Id}, name: {budget.Name}, lastModifiedOn: {budget.LastModifiedOn}]" + ); + }); + } + catch (Exception exception) + { + Console.WriteLine(exception.Message); + throw; + } + } + + public void Execute() + { + try { + var budgetResponse = _ynabAPI.Budgets.GetBudgets(); + var budgets = budgetResponse.Data.Budgets; + Console.WriteLine($"This user has {budgets.Count} budgets."); + Console.WriteLine(@" +=========== +BUDGET LIST +=========== +"); + budgets.ForEach(budget => { + Console.WriteLine( + $"[id: {budget.Id}, name: {budget.Name}, lastModifiedOn: {budget.LastModifiedOn}]" + ); + }); + } + catch (Exception exception) + { + Console.WriteLine(exception.Message); + throw; + } + } + } +} diff --git a/YNAB.SDK/examples/BudgetMonth.cs b/YNAB.SDK/examples/BudgetMonth.cs deleted file mode 100644 index 54eeaae..0000000 --- a/YNAB.SDK/examples/BudgetMonth.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System.Threading.Tasks; -using System; - -namespace YNAB.SDK.Examples { - public class BudgetMonth { - private API _ynabAPI; - public BudgetMonth(string accessToken) - { - _ynabAPI = new API(accessToken); - } - - public async Task FetchBudgetMonth(Guid budgetId, string monthId) - { - try { - var monthResponse = await _ynabAPI.Months.GetBudgetMonthAsync(budgetId.ToString(), DateTime.Parse(monthId)); - var budgetMonth = monthResponse.Data.Month; - Console.WriteLine($@" -============ -BUDGET MONTH -============ - Month: ${budgetMonth.Month} - Note: ${budgetMonth.Note ?? ""} - Age Of Money: ${budgetMonth.AgeOfMoney} - Category Balances: -"); - budgetMonth.Categories.ForEach(category => { - var balance = string.Format("{0:0.00}", Utils.ConvertMilliUnitsToCurrencyAmount(category.Balance, 2)); - Console.WriteLine( - $" {category.Name} - ${balance}" - ); - /* - Month: 2017-11-01 - Note: Month Note here - Age Of Money: 103 - Category Balances: - Medical - $50.00 - Deferred Income SubCategory - $0.00 - Immediate Income SubCategory - $0.00 - Renter's/Home Insurance - $0.00 - ... - */ - }); - } catch (Exception exception) { - Console.WriteLine(exception.Message); - throw; - } - } - } -} - diff --git a/YNAB.SDK/examples/BudgetMonthExample.cs b/YNAB.SDK/examples/BudgetMonthExample.cs new file mode 100644 index 0000000..369e2bf --- /dev/null +++ b/YNAB.SDK/examples/BudgetMonthExample.cs @@ -0,0 +1,91 @@ +using System.Threading.Tasks; +using System; + +namespace YNAB.SDK.Examples { + public class BudgetMonthExample { + private readonly API _ynabAPI; + public BudgetMonthExample(string accessToken) + { + _ynabAPI = new API(accessToken); + } + + public async Task ExecuteAsync(Guid budgetId, string monthId) + { + try { + var monthResponse = await _ynabAPI.Months.GetBudgetMonthAsync(budgetId.ToString(), DateTime.Parse(monthId)); + var budgetMonth = monthResponse.Data.Month; + Console.WriteLine($@" +============ +BUDGET MONTH +============ + Month: ${budgetMonth.Month} + Note: ${budgetMonth.Note ?? ""} + Age Of Money: ${budgetMonth.AgeOfMoney} + Category Balances: +"); + budgetMonth.Categories.ForEach(category => { + var balance = string.Format("{0:0.00}", Utils.ConvertMilliUnitsToCurrencyAmount(category.Balance, 2)); + Console.WriteLine( + $" {category.Name} - ${balance}" + ); + /* + Month: 2017-11-01 + Note: Month Note here + Age Of Money: 103 + Category Balances: + Medical - $50.00 + Deferred Income SubCategory - $0.00 + Immediate Income SubCategory - $0.00 + Renter's/Home Insurance - $0.00 + ... + */ + }); + } + catch (Exception exception) + { + Console.WriteLine(exception.Message); + throw; + } + } + + public void Execute(Guid budgetId, string monthId) + { + try { + var monthResponse = _ynabAPI.Months.GetBudgetMonth(budgetId.ToString(), DateTime.Parse(monthId)); + var budgetMonth = monthResponse.Data.Month; + Console.WriteLine($@" +============ +BUDGET MONTH +============ + Month: ${budgetMonth.Month} + Note: ${budgetMonth.Note ?? ""} + Age Of Money: ${budgetMonth.AgeOfMoney} + Category Balances: +"); + budgetMonth.Categories.ForEach(category => { + var balance = string.Format("{0:0.00}", Utils.ConvertMilliUnitsToCurrencyAmount(category.Balance, 2)); + Console.WriteLine( + $" {category.Name} - ${balance}" + ); + /* + Month: 2017-11-01 + Note: Month Note here + Age Of Money: 103 + Category Balances: + Medical - $50.00 + Deferred Income SubCategory - $0.00 + Immediate Income SubCategory - $0.00 + Renter's/Home Insurance - $0.00 + ... + */ + }); + } + catch (Exception exception) + { + Console.WriteLine(exception.Message); + throw; + } + } + } +} + diff --git a/YNAB.SDK/examples/BulkTransactionCreate.cs b/YNAB.SDK/examples/BulkTransactionCreate.cs new file mode 100644 index 0000000..a9175c7 --- /dev/null +++ b/YNAB.SDK/examples/BulkTransactionCreate.cs @@ -0,0 +1,21 @@ +using System; +using System.Threading.Tasks; + +namespace YNAB.SDK.Examples +{ + public class BulkTransactionCreate + { + private readonly API _ynabApi; + public BulkTransactionCreate(string accessToken) + { + _ynabApi = new API(accessToken); + } + + private async Task ExecuteAsync() { + throw new NotImplementedException(); + } + private void Execute() { + throw new NotImplementedException(); + } + } +} diff --git a/YNAB.SDK/examples/CategoryBalanceExample.cs b/YNAB.SDK/examples/CategoryBalanceExample.cs new file mode 100644 index 0000000..3ab5971 --- /dev/null +++ b/YNAB.SDK/examples/CategoryBalanceExample.cs @@ -0,0 +1,55 @@ +using System.Threading.Tasks; +using System; + +namespace YNAB.SDK.Examples { + public class CategoryBalanceExample { + private readonly API _ynabAPI; + public CategoryBalanceExample(string accessToken) + { + _ynabAPI = new API(accessToken); + } + + public async Task ExecuteAsync(Guid budgetId, Guid categoryId) + { + try { + var monthResponse = await _ynabAPI.Categories.GetCategoryByIdAsync(budgetId.ToString(), categoryId.ToString()); + var category = monthResponse.Data.Category; + Console.WriteLine($@" +============ +CATEGORY +============ + Name: ${category.Name} + Budgeted: ${category.Budgeted} + Balance: ${category.Balance} +"); + } + catch (Exception exception) + { + Console.WriteLine(exception.Message); + throw; + } + } + + public void Execute(Guid budgetId, Guid categoryId) + { + try { + var monthResponse = _ynabAPI.Categories.GetCategoryById(budgetId.ToString(), categoryId.ToString()); + var category = monthResponse.Data.Category; + Console.WriteLine($@" +============ +CATEGORY +============ + Name: ${category.Name} + Budgeted: ${category.Budgeted} + Balance: ${category.Balance} +"); + } + catch (Exception exception) + { + Console.WriteLine(exception.Message); + throw; + } + } + } +} + diff --git a/YNAB.SDK/examples/CreateMultipleTransactions.cs b/YNAB.SDK/examples/CreateMultipleTransactions.cs new file mode 100644 index 0000000..8dd4317 --- /dev/null +++ b/YNAB.SDK/examples/CreateMultipleTransactions.cs @@ -0,0 +1,21 @@ +using System; +using System.Threading.Tasks; + +namespace YNAB.SDK.Examples +{ + public class CreateMultipleTransactions + { + private readonly API _ynabApi; + public CreateMultipleTransactions(string accessToken) + { + _ynabApi = new API(accessToken); + } + + private async Task ExecuteAsync() { + throw new NotImplementedException(); + } + private void Execute() { + throw new NotImplementedException(); + } + } +} diff --git a/YNAB.SDK/examples/CreateTransaction.cs b/YNAB.SDK/examples/CreateTransaction.cs new file mode 100644 index 0000000..4abb400 --- /dev/null +++ b/YNAB.SDK/examples/CreateTransaction.cs @@ -0,0 +1,21 @@ +using System; +using System.Threading.Tasks; + +namespace YNAB.SDK.Examples +{ + public class CreateTransaction + { + private readonly API _ynabApi; + public CreateTransaction(string accessToken) + { + _ynabApi = new API(accessToken); + } + + private async Task ExecuteAsync() { + throw new NotImplementedException(); + } + private void Execute() { + throw new NotImplementedException(); + } + } +} diff --git a/YNAB.SDK/examples/DeltaRequestExample.cs b/YNAB.SDK/examples/DeltaRequestExample.cs new file mode 100644 index 0000000..573a5df --- /dev/null +++ b/YNAB.SDK/examples/DeltaRequestExample.cs @@ -0,0 +1,134 @@ +using System; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Newtonsoft.Json; + +namespace YNAB.SDK.Examples +{ + public class DeltaRequestExample + { + private readonly API _ynabApi; + private int lastKnownServerKnowledge; + const int POLL_WAIT_TIME_IN_MS = 5000; + + public DeltaRequestExample(string accessToken) + { + _ynabApi = new API(accessToken); + } + + private async Task QueueUpPollAsync(string budgetToFetchId) + { + Console.WriteLine($"Current server knowledge is {lastKnownServerKnowledge}"); + Console.WriteLine($"Will poll for changes in {POLL_WAIT_TIME_IN_MS}ms..."); + await Task.Delay(POLL_WAIT_TIME_IN_MS); + Console.WriteLine("Polling for changes now..."); + var budgetChangesResponse = await _ynabApi.Budgets.GetBudgetByIdAsync(budgetToFetchId, lastKnownServerKnowledge); + Console.WriteLine($"Current server knowledge is now: {budgetChangesResponse.Data.ServerKnowledge}"); + if(budgetChangesResponse.Data.ServerKnowledge > lastKnownServerKnowledge) + { + Console.WriteLine("There have been changes to the following entities: "); + Console.WriteLine(JsonConvert.SerializeObject(budgetChangesResponse.Data.Budget)); + } + else + { + Console.WriteLine("No changes found"); + } + } + private void QueueUpPoll(string budgetToFetchId) + { + Console.WriteLine($"Current server knowledge is {lastKnownServerKnowledge}"); + Console.WriteLine($"Will poll for changes in {POLL_WAIT_TIME_IN_MS}ms..."); + Thread.Sleep(POLL_WAIT_TIME_IN_MS); + Console.WriteLine("Polling for changes now..."); + var budgetChangesResponse = _ynabApi.Budgets.GetBudgetById(budgetToFetchId, lastKnownServerKnowledge); + Console.WriteLine($"Current server knowledge is now: {budgetChangesResponse.Data.ServerKnowledge}"); + if(budgetChangesResponse.Data.ServerKnowledge > lastKnownServerKnowledge) + { + Console.WriteLine("There have been changes to the following entities: "); + Console.WriteLine(JsonConvert.SerializeObject(budgetChangesResponse.Data.Budget)); + } + else + { + Console.WriteLine("No changes found"); + } + } + public async Task ExecuteAsync() + { + try + { + Console.WriteLine("Fetching budgets..."); + var response = await _ynabApi.Budgets.GetBudgetsAsync(); + var allBudgets = response.Data.Budgets; + var budgetName = "TestBudget"; + var budgetToFetch = allBudgets.First(budget => budget.Name == budgetName); + Console.WriteLine($"Fetching contents of budget: {budgetToFetch.Name} - {budgetToFetch.Id}"); + var budgetContents = await _ynabApi.Budgets.GetBudgetByIdAsync(budgetToFetch.Id.ToString()); + var categories = budgetContents.Data.Budget.Categories; + Console.WriteLine("Here is the budget data for the current month: "); + var currentMonthISO = Utils.GetCurrentMonthInISOFormat(); + var monthDetailForCurrentMonth = budgetContents.Data.Budget.Months.Find(m => m.Month.ToString("yyyy-MM-dd") == currentMonthISO); + if (monthDetailForCurrentMonth != null) + { + Console.WriteLine(JsonConvert.SerializeObject(monthDetailForCurrentMonth)); + } + else + { + throw new Exception($"Could not find month detail for the current month: {currentMonthISO}"); + } + + var lastServerKnowledge = budgetContents.Data.ServerKnowledge; + + var iterationCount = 0; + do { + await QueueUpPollAsync(budgetToFetch.Id.ToString()); + } while(iterationCount++ < 2); + + } + catch (Exception exception) + { + Console.WriteLine(exception.Message); + throw; + } + } + + public void Execute() + { + try + { + Console.WriteLine("Fetching budgets..."); + var response = _ynabApi.Budgets.GetBudgets(); + var allBudgets = response.Data.Budgets; + var budgetName = "TestBudget"; + var budgetToFetch = allBudgets.First(budget => budget.Name == budgetName); + Console.WriteLine($"Fetching contents of budget: {budgetToFetch.Name} - {budgetToFetch.Id}"); + var budgetContents = _ynabApi.Budgets.GetBudgetById(budgetToFetch.Id.ToString()); + var categories = budgetContents.Data.Budget.Categories; + Console.WriteLine("Here is the budget data for the current month: "); + var currentMonthISO = Utils.GetCurrentMonthInISOFormat(); + var monthDetailForCurrentMonth = budgetContents.Data.Budget.Months.Find(m => m.Month.ToString("yyyy-MM-dd") == currentMonthISO); + if (monthDetailForCurrentMonth != null) + { + Console.WriteLine(JsonConvert.SerializeObject(monthDetailForCurrentMonth)); + } + else + { + throw new Exception($"Could not find month detail for the current month: {currentMonthISO}"); + } + + var lastServerKnowledge = budgetContents.Data.ServerKnowledge; + + var iterationCount = 0; + do { + QueueUpPoll(budgetToFetch.Id.ToString()); + } while(iterationCount++ < 2); + + } + catch (Exception exception) + { + Console.WriteLine(exception.Message); + throw; + } + } + } +} diff --git a/YNAB.SDK/examples/UpdateCategoryBudgeted.cs b/YNAB.SDK/examples/UpdateCategoryBudgeted.cs new file mode 100644 index 0000000..15a8dd2 --- /dev/null +++ b/YNAB.SDK/examples/UpdateCategoryBudgeted.cs @@ -0,0 +1,21 @@ +using System; +using System.Threading.Tasks; + +namespace YNAB.SDK.Examples +{ + public class UpdateCategoryBudgeted + { + private readonly API _ynabApi; + public UpdateCategoryBudgeted(string accessToken) + { + _ynabApi = new API(accessToken); + } + + private async Task ExecuteAsync() { + throw new NotImplementedException(); + } + private void Execute() { + throw new NotImplementedException(); + } + } +} diff --git a/YNAB.SDK/examples/UpdateTransaction.cs b/YNAB.SDK/examples/UpdateTransaction.cs new file mode 100644 index 0000000..42c29d9 --- /dev/null +++ b/YNAB.SDK/examples/UpdateTransaction.cs @@ -0,0 +1,22 @@ +using System; +using System.Threading.Tasks; + +namespace YNAB.SDK.Examples +{ + public class UpdateTransaction + { + private readonly API _ynabApi; + public UpdateTransaction(string accessToken) + { + _ynabApi = new API(accessToken); + } + + private async Task ExecuteAsync() { + throw new NotImplementedException(); + } + private void Execute() { + throw new NotImplementedException(); + } + } +} +