Skip to content
This repository has been archived by the owner on Mar 8, 2021. It is now read-only.

Commit

Permalink
Merge pull request #2 from rzari/feature/savings_goals
Browse files Browse the repository at this point in the history
Feature/savings goals
  • Loading branch information
rzari authored Apr 1, 2018
2 parents e240948 + 2e5cac0 commit 262317a
Show file tree
Hide file tree
Showing 60 changed files with 1,052 additions and 292 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Details of how to obtain a token can be found [here](https://developer.starlingb

### Initialise Client
```java
StarlingBank starling = new StarlingBank(StarlingBankEnvironment.PRODUCTION, "<my_personal_access_token>");
StarlingBank starling = new Starling(StarlingBankEnvironment.PRODUCTION, "<my_personal_access_token>");
```

### Fetch your account details
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.jarling</groupId>
<artifactId>jarling</artifactId>
<version>0.1.1-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
<url>https://github.com/rzari/jarling</url>

<properties>
Expand Down
137 changes: 135 additions & 2 deletions src/main/java/org/jarling/Starling.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
package org.jarling;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.jarling.api.*;
import org.jarling.exceptions.StarlingBankRequestException;
import org.jarling.http.HttpParameter;
import org.jarling.http.HttpResponse;
import org.jarling.models.*;
import org.jarling.services.*;
import org.jarling.models.accounts.Account;
import org.jarling.models.accounts.AccountBalance;
import org.jarling.models.common.Addresses;
import org.jarling.models.cards.Card;
import org.jarling.models.common.*;
import org.jarling.models.contacts.Contact;
import org.jarling.models.contacts.ContactAccount;
import org.jarling.models.contacts.ContactAccountType;
import org.jarling.models.customers.Customer;
import org.jarling.models.directDebits.DirectDebitMandate;
import org.jarling.models.directDebits.DirectDebitTransaction;
import org.jarling.models.merchants.Merchant;
import org.jarling.models.merchants.MerchantLocation;
import org.jarling.models.payments.Payment;
import org.jarling.models.budgeting.SavingsGoal;
import org.jarling.models.transactions.FasterPaymentsInTransaction;
import org.jarling.models.transactions.FasterPaymentsOutTransaction;
import org.jarling.models.transactions.MasterCardTransaction;
import org.jarling.models.transactions.Transaction;
import org.jarling.models.customers.WhoAmI;
import org.jarling.services.ApiService;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
*
Expand All @@ -23,6 +44,7 @@
public final class Starling extends StarlingBase implements StarlingBank{

private static ApiService apiService;
private static Gson gson = new Gson();

public Starling(StarlingBankEnvironment environment, String accessToken){
if (accessToken == null || accessToken.equals("")){
Expand Down Expand Up @@ -279,6 +301,111 @@ public WhoAmI getWhoAmI() throws StarlingBankRequestException {
return gson.fromJson(apiService.get("/me").asString(), WhoAmI.class);
}


@Override
public SavingsGoal getSavingsGoal(String savingsGoalUid) throws StarlingBankRequestException {
return gson.fromJson(apiService.get("/savings-goals/" + savingsGoalUid).asString(), SavingsGoal.class);
}


@Override
public List<SavingsGoal> listSavingsGoals() throws StarlingBankRequestException {
return fromJsonList(SavingsGoal[].class, apiService.get("/savings-goals").asString(), "savingsGoalList");
}

@Override
public void deleteSavingsGoal(String savingsGoalUid) throws StarlingBankRequestException {
apiService.delete("/savings-goals/" + savingsGoalUid);
}

@Override
public Photo getSavingsGoalPhoto(String savingsGoalUid) throws StarlingBankRequestException {
return gson.fromJson(apiService.get("/savings-goals/" + savingsGoalUid + "/photo").asString(), Photo.class);
}

@Override
public String createSavingsGoal(String name, String currency, BigDecimal targetAmount) throws StarlingBankRequestException {
return createSavingsGoal(name, currency, targetAmount, "");
}

@Override
public String createSavingsGoal(String name, String currency, BigDecimal targetAmount, String photo) throws StarlingBankRequestException {
JsonObject savingsGoalRequestJson = new JsonObject();
JsonObject targetJson = new JsonObject();

savingsGoalRequestJson.addProperty("name", name);
savingsGoalRequestJson.addProperty("currency", currency);
savingsGoalRequestJson.addProperty("photo", photo);
targetJson.addProperty("currency", currency);
targetJson.addProperty("minorUnits", targetAmount.multiply(BigDecimal.TEN.multiply(BigDecimal.TEN)).longValue());
savingsGoalRequestJson.add("target", targetJson);

HttpResponse savingsGoalResponse = apiService.put("/savings-goals/" + UUID.randomUUID().toString(), null, null, savingsGoalRequestJson.toString());

JsonObject response = savingsGoalResponse.asJsonObject();

if (response.get("success").getAsBoolean()){
return response.get("savingsGoalUid").getAsString();
}
return null;
}

@Override
public RecurringTransfer getSavingsGoalRecurringTransfer(String savingsGoalUid) throws StarlingBankRequestException {
return gson.fromJson(apiService.get("/savings-goals/" + savingsGoalUid + "/recurring-transfer").asString(), RecurringTransfer.class);
}

@Override
public String createSavingsGoalRecurringTransfer(String savingsGoalUid, RecurrenceRule recurrenceRule, CurrencyAndAmount currencyAndAmount) throws StarlingBankRequestException {
RecurringTransfer recurringTransfer = new RecurringTransfer(recurrenceRule, currencyAndAmount);
String jsonString = gson.toJson(recurringTransfer);
HttpResponse response = apiService.put("/savings-goals/" + savingsGoalUid + "/recurring-transfer", null, null, jsonString);

if (200 == response.getStatusCode()){
JsonObject json = response.asJsonObject();
if (json.get("success").getAsBoolean()){
return json.get("transferUid").getAsString();
}
}

return null;
}

@Override
public void deleteSavingsGoalRecurringTransfer(String savingsGoalUid) throws StarlingBankRequestException {
apiService.delete("/savings-goals/" + savingsGoalUid + "/recurring-transfer");
}

@Override
public String withdrawMoneyFromSavingsGoal(String savingsGoalUid, Amount amount) throws StarlingBankRequestException {
String jsonString = gson.toJson(amount);
HttpResponse response = apiService.put("/savings-goals/" + savingsGoalUid + "/withdraw-money/" + UUID.randomUUID().toString(), null, null, jsonString);

if (200 == response.getStatusCode()){
JsonObject json = response.asJsonObject();
if (json.get("success").getAsBoolean()){
return json.get("transferUid").getAsString();
}
}

return null;
}

@Override
public String addMoneyToSavingsGoal(String savingsGoalUid, Amount amount) throws StarlingBankRequestException {
String jsonString = gson.toJson(amount);
HttpResponse response = apiService.put("/savings-goals/" + savingsGoalUid + "/add-money/" + UUID.randomUUID().toString(), null, null, jsonString);

if (200 == response.getStatusCode()){
JsonObject json = response.asJsonObject();
if (json.get("success").getAsBoolean()){
return json.get("transferUid").getAsString();
}
}

return null;
}

@Override
public AccountResource account() {
return this;
Expand Down Expand Up @@ -319,6 +446,11 @@ public PaymentResource payment() {
return this;
}

@Override
public SavingsGoalResource savingsGoal() {
return this;
}

@Override
public TransactionResource transaction() {
return this;
Expand Down Expand Up @@ -348,4 +480,5 @@ public TransactionDirectDebitResource transactionDirectDebit() {
public WhoAmIResource whoAmI() {
return this;
}

}
3 changes: 3 additions & 0 deletions src/main/java/org/jarling/StarlingBank.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public interface StarlingBank extends AccountResource,
DirectDebitMandateResource,
MerchantResource,
PaymentResource,
SavingsGoalResource,
TransactionDirectDebitResource,
TransactionFasterPaymentInResource,
TransactionFasterPaymentOutResource,
Expand All @@ -37,6 +38,8 @@ public interface StarlingBank extends AccountResource,

PaymentResource payment();

SavingsGoalResource savingsGoal();

TransactionResource transaction();

TransactionMasterCardResource transactionMastercard();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jarling/api/AccountResource.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.jarling.api;

import org.jarling.exceptions.StarlingBankRequestException;
import org.jarling.models.Account;
import org.jarling.models.AccountBalance;
import org.jarling.models.accounts.Account;
import org.jarling.models.accounts.AccountBalance;

/**
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jarling/api/AddressResource.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.jarling.api;

import org.jarling.exceptions.StarlingBankRequestException;
import org.jarling.models.Addresses;
import org.jarling.models.common.Addresses;

/**
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jarling/api/CardResource.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.jarling.api;

import org.jarling.exceptions.StarlingBankRequestException;
import org.jarling.models.Card;
import org.jarling.models.cards.Card;

/**
*
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jarling/api/ContactResource.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.jarling.api;

import org.jarling.exceptions.StarlingBankRequestException;
import org.jarling.models.Contact;
import org.jarling.models.ContactAccount;
import org.jarling.models.contacts.Contact;
import org.jarling.models.contacts.ContactAccount;

import java.util.List;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jarling/api/CustomerResource.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.jarling.api;

import org.jarling.exceptions.StarlingBankRequestException;
import org.jarling.models.Customer;
import org.jarling.models.customers.Customer;

/**
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.jarling.api;

import org.jarling.exceptions.StarlingBankRequestException;
import org.jarling.models.DirectDebitMandate;
import org.jarling.models.directDebits.DirectDebitMandate;

import java.util.List;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jarling/api/MerchantResource.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.jarling.api;

import org.jarling.exceptions.StarlingBankRequestException;
import org.jarling.models.Merchant;
import org.jarling.models.MerchantLocation;
import org.jarling.models.merchants.Merchant;
import org.jarling.models.merchants.MerchantLocation;

/**
*
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jarling/api/PaymentResource.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.jarling.api;

import org.jarling.exceptions.StarlingBankRequestException;
import org.jarling.models.Payment;
import org.jarling.models.RecurrenceRule;
import org.jarling.models.common.RecurrenceRule;
import org.jarling.models.payments.Payment;

import java.math.BigDecimal;
import java.util.List;
Expand Down
Loading

0 comments on commit 262317a

Please sign in to comment.