mirror of
https://github.com/jlengrand/dialogflow-fun.git
synced 2026-03-10 08:11:22 +00:00
Add support for transactions viewing
This commit is contained in:
@@ -45,6 +45,11 @@
|
||||
<artifactId>javax.json</artifactId>
|
||||
<version>1.1.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
package fr.lengrand.dialogflowfunapi;
|
||||
|
||||
import fr.lengrand.dialogflowfunapi.openbankproject.auth.Auth;
|
||||
import fr.lengrand.dialogflowfunapi.openbankproject.transactions.TransactionsHandler;
|
||||
import fr.lengrand.dialogflowfunapi.openbankproject.transactions.data.Transactions;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.IOException;
|
||||
|
||||
@SpringBootApplication
|
||||
@@ -16,10 +19,22 @@ public class DialogflowFunApiApplication {
|
||||
@Autowired
|
||||
private Auth auth;
|
||||
|
||||
@Autowired
|
||||
private TransactionsHandler transactionsHandler;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DialogflowFunApiApplication.class, args);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
try {
|
||||
auth.authenticate();
|
||||
} catch (IOException | InterruptedException e) {
|
||||
System.out.println("Error while authenticating!"); // TODO : Use a logger
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public String hello() {
|
||||
return "hello world!";
|
||||
@@ -28,7 +43,12 @@ public class DialogflowFunApiApplication {
|
||||
@GetMapping("/auth")
|
||||
public String auth() throws IOException, InterruptedException {
|
||||
auth.authenticate(); // TODO: Should be done on start!
|
||||
return auth.getAuthToken().isPresent()? auth.getAuthToken().get().getToken() : "No token!";
|
||||
return auth.getToken().isPresent()? auth.getToken().get() : "No token!";
|
||||
}
|
||||
|
||||
@GetMapping("/transactions")
|
||||
public Transactions transactions() throws IOException, InterruptedException {
|
||||
return transactionsHandler.getTransactions();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package fr.lengrand.dialogflowfunapi.openbankproject.auth;
|
||||
|
||||
import fr.lengrand.dialogflowfunapi.openbankproject.auth.data.AuthToken;
|
||||
import fr.lengrand.dialogflowfunapi.openbankproject.auth.data.OpenBankCredentials;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -23,14 +25,9 @@ public class Auth {
|
||||
|
||||
public JSONBodyHandler<AuthToken> jsonBodyHandler = JSONBodyHandler.getHandler(AuthToken.class);
|
||||
|
||||
private Optional<AuthToken> authToken = Optional.empty();
|
||||
private Optional<String> token = Optional.empty();
|
||||
|
||||
public void authenticate() throws IOException, InterruptedException {
|
||||
System.out.println("Username is " + credentials.getUsername());
|
||||
System.out.println("Password is " + credentials.getPassword());
|
||||
System.out.println("Key is " + credentials.getKey());
|
||||
|
||||
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.headers("Content-Type", "application/json",
|
||||
@@ -40,16 +37,16 @@ public class Auth {
|
||||
.build();
|
||||
|
||||
HttpResponse<AuthToken> response = client.send(request, jsonBodyHandler); //TODO : Handle failures.
|
||||
System.out.println(response.body().getToken());
|
||||
authToken = Optional.of(response.body());
|
||||
System.out.println("Authenticated");
|
||||
token = Optional.of(response.body().getToken());
|
||||
}
|
||||
|
||||
public boolean isAuthenticated() {
|
||||
return authToken.isPresent();
|
||||
return token.isPresent() && token.get() != null;
|
||||
}
|
||||
|
||||
public Optional<AuthToken> getAuthToken() {
|
||||
return authToken;
|
||||
public Optional<String> getToken() {
|
||||
return token;
|
||||
}
|
||||
|
||||
private String createDirectLoginHeader(){
|
||||
|
||||
@@ -10,6 +10,7 @@ import java.net.http.HttpResponse.ResponseInfo;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class JSONBodyHandler<T> implements HttpResponse.BodyHandler<T> {
|
||||
// TODO : Use Jackson
|
||||
private final Jsonb jsonBinder;
|
||||
private final Class<T> type;
|
||||
private HttpResponse.BodySubscriber<byte[]> byteArraySubscriber;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package fr.lengrand.dialogflowfunapi.openbankproject.auth;
|
||||
package fr.lengrand.dialogflowfunapi.openbankproject.auth.data;
|
||||
|
||||
public class AuthToken {
|
||||
private String token;
|
||||
@@ -1,4 +1,4 @@
|
||||
package fr.lengrand.dialogflowfunapi.openbankproject.auth;
|
||||
package fr.lengrand.dialogflowfunapi.openbankproject.auth.data;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
@@ -0,0 +1,46 @@
|
||||
package fr.lengrand.dialogflowfunapi.openbankproject.transactions;
|
||||
|
||||
import fr.lengrand.dialogflowfunapi.openbankproject.auth.Auth;
|
||||
import fr.lengrand.dialogflowfunapi.openbankproject.auth.JSONBodyHandler;
|
||||
import fr.lengrand.dialogflowfunapi.openbankproject.auth.data.AuthToken;
|
||||
import fr.lengrand.dialogflowfunapi.openbankproject.transactions.data.Transactions;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
|
||||
@Service
|
||||
public class TransactionsHandler {
|
||||
|
||||
@Autowired
|
||||
private Auth auth;
|
||||
|
||||
public JSONBodyHandler<Transactions> jsonBodyHandler = JSONBodyHandler.getHandler(Transactions.class);
|
||||
|
||||
public Transactions getTransactions() throws IOException, InterruptedException {
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.headers("Content-Type", "application/json",
|
||||
"Authorization", createAuthHeader())
|
||||
.uri(URI.create(createUrl("at02-1465--01", "john_doe"))) // TODO : Convert to DialogFlow names
|
||||
.build();
|
||||
|
||||
HttpResponse<Transactions> response = client.send(request, jsonBodyHandler); //TODO : Handle failures.
|
||||
return response.body();
|
||||
}
|
||||
|
||||
private String createUrl(String bank, String user){
|
||||
return "https://psd2-api.openbankproject.com/obp/v4.0.0/my/banks/"+ bank + "/accounts/" + user + "/transactions";
|
||||
}
|
||||
|
||||
private String createAuthHeader(){
|
||||
return "DirectLogin token=" + this.auth.getToken().get();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package fr.lengrand.dialogflowfunapi.openbankproject.transactions.data;
|
||||
|
||||
public class Amount {
|
||||
|
||||
public String currency;
|
||||
public float amount;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package fr.lengrand.dialogflowfunapi.openbankproject.transactions.data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Details {
|
||||
public String description;
|
||||
public Date posted;
|
||||
public Date completed;
|
||||
public Amount value;
|
||||
public Amount new_balance;
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package fr.lengrand.dialogflowfunapi.openbankproject.transactions.data;
|
||||
|
||||
public class Transaction {
|
||||
|
||||
public String id;
|
||||
public Details details;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package fr.lengrand.dialogflowfunapi.openbankproject.transactions.data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Transactions {
|
||||
|
||||
public List<Transaction> transactions;
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
obp.username=${OPENBANKPROJECT_USERNAME}
|
||||
obp.password=${OPENBANKPROJECT_PASSWORD}
|
||||
obp.key=${OPENBANKPROJECT_CONSUMERKEY}
|
||||
obp.key=${OPENBANKPROJECT_CONSUMERKEY}
|
||||
spring.jackson.serialization.indent_output=true
|
||||
|
||||
Reference in New Issue
Block a user