-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoundUp.java
133 lines (114 loc) · 4.91 KB
/
RoundUp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.starlingbank;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
/**
* Calculates the total round-up value for a given week and given account
* @author Zoey Luu
*/
public class RoundUp {
private static final String authID = "Bearer "; // Enter "Bearer {yourAccessToken}" here
private static final String userID = ""; // Enter value here for User-Agent
public static void main(String[] args) throws IOException {
Account account = getAccountsInfo();
assert account != null;
BigInteger weeklyRoundUp = WeeklySavings.getWeeklyRoundUps(account.getTransactionFeed(authID, userID));
/*
String savingsAccInfo = createSavingsAccInfo(account.getAccountUid());
transferToSavings(weeklyRoundUp, savingsAccInfo, account.getAccountUid());
*/
}
/**
* Fetches account information from API then parses
* @return Account information
*/
private static Account getAccountsInfo(){
try {
URL url = new URL("https://api-sandbox.starlingbank.com/api/v2/accounts");
JSONObject obj = getJsonObject(url);
try {
JSONArray arr = obj.getJSONArray("accounts");
String accountUid = arr.getJSONObject(0).getString("accountUid");
String defaultCategory = arr.getJSONObject(0).getString("defaultCategory");
String currency = arr.getJSONObject(0).getString("currency");
String createdAt = arr.getJSONObject(0).getString("createdAt");
return new Account(accountUid, defaultCategory, currency, createdAt, authID, userID);
} catch (JSONException e){
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/*
private static String createSavingsAccInfo(String accountUid){
try {
URL url = new URL("https://api-sandbox.starlingbank.com/api/v2/account/" + accountUid + "/savings-goals");
JSONObject obj = putJsonObject(url);
try {
return obj.getString("savingsGoalUid");
} catch (JSONException e){
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static JSONObject putJsonObject(URL url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Authorization", authID);
connection.setRequestProperty("User-Agent", userID);
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
JSONObject data = new JSONObject().put("folder",
new JSONObject().put("name", "test creation folder"));
wr.write(data.toString());
return data;
}
*/
/**
* Runs a GET request to retrieve information
* @param url The url to retrieve information from
* @return Resource information
* @throws IOException
*/
private static JSONObject getJsonObject(URL url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Authorization", authID);
connection.setRequestProperty("User-Agent", userID);
connection.setRequestMethod("GET");
if (connection.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code: " + connection.getResponseCode());
}
Scanner scan = new Scanner(connection.getInputStream());
StringBuilder str = new StringBuilder();
while (scan.hasNext()) {
str.append(scan.nextLine());
}
scan.close();
return new JSONObject(str.toString());
}
/*
private static void transferToSavings(BigInteger amount, String savingAccUid, String accountUid) throws IOException {
URL url = new URL("https://api-sandbox.starlingbank.com/api/v2/account/a7a08af3-9239-44ee-ad13-1ad7b1906a60/savings-goals");
putJsonObject(url);
}
private static void updateTransactionFeed(String feedItemUID, String direction, String status, BigInteger amount){
}
*/
}