mirror of
https://github.com/jlengrand/dialogflow-fun.git
synced 2026-03-10 08:11:22 +00:00
Caching somewhere no good (#23)
* Minor changes. Remove TODOs * Fix order bug in getting last transaction * Caching somewhere. Trying deprecated client * Fixed caching bug * Update date presentation format in details
This commit is contained in:
committed by
GitHub
parent
98b3a39649
commit
96cce7036c
@@ -23,5 +23,6 @@ A simple project using dialog flow
|
||||
- [Full library reference](https://googleapis.dev/java/google-cloud-clients/latest/index.html?com/google/cloud/dialogflow/v2/package-summary.html)
|
||||
- [Maven dependency](https://search.maven.org/artifact/com.google.cloud/google-cloud-dialogflow/0.114.0-alpha/jar)
|
||||
- [Authentication](https://cloud.google.com/docs/authentication/getting-started)
|
||||
|
||||
-[Current state of the slides](https://docs.google.com/presentation/d/1MOqI4hZbGXSNjEiItIqCiXfIT8c9jCV77UMBWtftKbM/edit?usp=sharing)
|
||||
- [Transaction Types](https://github.com/OpenBankProject/OBP-API/wiki/Transaction-Requests)
|
||||
|
||||
- [Current state of the slides](https://docs.google.com/presentation/d/1MOqI4hZbGXSNjEiItIqCiXfIT8c9jCV77UMBWtftKbM/edit?usp=sharing)
|
||||
|
||||
@@ -31,11 +31,7 @@ public class DialogflowFunApiApplication {
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
try {
|
||||
auth.authenticate();
|
||||
} catch (IOException | InterruptedException e) {
|
||||
System.out.println("Error while authenticating!"); // TODO : Use a logger
|
||||
}
|
||||
auth.safeAuthenticate();
|
||||
}
|
||||
|
||||
@PostMapping(value = "/fulfillment")
|
||||
|
||||
@@ -12,6 +12,8 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Comparator;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@@ -21,13 +23,6 @@ public class DialogFlowService {
|
||||
|
||||
// TODO : Add strong authentication
|
||||
public DialogFlowResponse createPaymentRequest(DialogFlowWebHookRequest request) throws IOException, InterruptedException {
|
||||
System.out.println("/////////");
|
||||
System.out.println("Payment request info : ");
|
||||
System.out.println(request.getQueryResult().getParameters().getContact());
|
||||
System.out.println(request.getQueryResult().getParameters().getUnitCurrency().getAmount());
|
||||
System.out.println(request.getQueryResult().getParameters().getUnitCurrency().getCurrency());
|
||||
System.out.println("/////////");
|
||||
|
||||
Optional<BankAccount> userAccount = UserAccountLookup.getBankAccountFromContact(request.getQueryResult().getParameters().getContact());
|
||||
|
||||
if (userAccount.isEmpty())
|
||||
@@ -37,10 +32,11 @@ public class DialogFlowService {
|
||||
, new PaymentRequestDetails(
|
||||
userAccount.get().toAccount(),
|
||||
request.getQueryResult().getParameters().getUnitCurrency(),
|
||||
request.getQueryResult().getParameters().getContact() + " at " + LocalDateTime.now()));
|
||||
request.getQueryResult().getParameters().getContact() + " at " + getCurrentTime())
|
||||
);
|
||||
|
||||
return paymentRequest.getStatus().equalsIgnoreCase("completed") ?
|
||||
new DialogFlowResponse("Created a payment for a value of " + paymentRequest.getDetails().getValue().getAmount() + paymentRequest.getDetails().getValue().getCurrency() + " to " + request.getQueryResult().getParameters().getContact()) // TODO: Add reverse lookup
|
||||
new DialogFlowResponse("Created a payment for a value of " + request.getQueryResult().getParameters().getUnitCurrency().getAmount() + request.getQueryResult().getParameters().getUnitCurrency().getCurrency() + " to " + request.getQueryResult().getParameters().getContact())
|
||||
: new DialogFlowResponse("Sorry, the creation of the payment failed. Please try again later!");
|
||||
}
|
||||
|
||||
@@ -52,12 +48,21 @@ public class DialogFlowService {
|
||||
}
|
||||
|
||||
private String createTransactionDialogResponse(Transaction transaction){
|
||||
return "Your last transaction was for " + transaction.getDetails().getDescription() + " with an amount of " + (-transaction.getDetails().getValue().getAmount()) + transaction.getDetails().getValue().getCurrency() + ". Your new balance is " + transaction.getDetails().getNewBalance().getAmount() + transaction.getDetails().getNewBalance().getCurrency();
|
||||
return "Your last transaction was for " + transaction.getDetails().getDescription() + " with an amount of " + (transaction.getDetails().getValue().getAmount()) + transaction.getDetails().getValue().getCurrency() + ". Your new balance is " + transaction.getDetails().getNewBalance().getAmount() + transaction.getDetails().getNewBalance().getCurrency();
|
||||
|
||||
}
|
||||
|
||||
private Optional<Transaction> getLastTransaction() throws IOException, InterruptedException {
|
||||
var transactionsObject = openBankClient.getTransactions(UserAccountLookup.getCurrentUserAccount());
|
||||
return transactionsObject.getTransactions().isEmpty() ? Optional.empty() : Optional.of(transactionsObject.getTransactions().get(0));
|
||||
|
||||
if (transactionsObject.getTransactions().isEmpty()) return Optional.empty();
|
||||
|
||||
return transactionsObject.getTransactions().stream()
|
||||
.sorted(Comparator.comparing(t -> t.getDetails().getCompleted(), Comparator.reverseOrder()) )
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
private String getCurrentTime() {
|
||||
return LocalDateTime.now().format(DateTimeFormatter.ofPattern("MM-dd HH:mm"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,9 @@ public class UserAccountLookup {
|
||||
|
||||
public static Optional<BankAccount> getBankAccountFromContact(String contact){
|
||||
BankAccount bankAccount = accountsLookup.get(contact.toLowerCase().trim());
|
||||
|
||||
if(bankAccount != null) System.out.println("Found " + bankAccount.getBankId() + " " + bankAccount.getUserId());
|
||||
|
||||
return bankAccount == null? Optional.empty() : Optional.of(bankAccount);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,47 +10,48 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
public class OpenBankClient {
|
||||
|
||||
private static final String TRANSACTION_TYPE = "SANDBOX_TAN";
|
||||
|
||||
@Autowired
|
||||
protected OpenBankHandler openBankHandler;
|
||||
|
||||
public JSONBodyHandler<Balance> balanceJSONHandler = JSONBodyHandler.getHandler(Balance.class);
|
||||
public JSONBodyHandler<TransactionsObject> transactionsJSONHandler = JSONBodyHandler.getHandler(TransactionsObject.class);
|
||||
public JSONBodyHandler<PaymentRequest> paymentRequestJSONHandler = JSONBodyHandler.getHandler(PaymentRequest.class);
|
||||
|
||||
|
||||
public Balance getBalance(BankAccount account) throws IOException, InterruptedException {
|
||||
return openBankHandler.get(
|
||||
balanceJSONHandler,
|
||||
JSONBodyHandler.getHandler(Balance.class),
|
||||
createBalanceRelativeUrl(account));
|
||||
}
|
||||
|
||||
public TransactionsObject getTransactions(BankAccount account) throws IOException, InterruptedException {
|
||||
return openBankHandler.get(
|
||||
transactionsJSONHandler,
|
||||
createTransactionsUrl(account) // TODO : Convert to DialogFlow names
|
||||
JSONBodyHandler.getHandler(TransactionsObject.class),
|
||||
createTransactionsUrl(account)
|
||||
);
|
||||
}
|
||||
|
||||
public PaymentRequest createPaymentRequest(BankAccount account, PaymentRequestDetails details) throws IOException, InterruptedException {
|
||||
return openBankHandler.post(
|
||||
paymentRequestJSONHandler,
|
||||
JSONBodyHandler.getHandler(PaymentRequest.class),
|
||||
createPaymentRequestUrl(account),
|
||||
PaymentRequestDetails.toJSON(details)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
private String createPaymentRequestUrl(BankAccount account){
|
||||
return "/banks/" + account.getBankId() + "/accounts/" + account.getUserId() + "/owner/transaction-request-types/SANDBOX_TAN/transaction-requests";
|
||||
return "/banks/" + account.getBankId() + "/accounts/" + account.getUserId() + "/owner/transaction-request-types/" + TRANSACTION_TYPE + "/transaction-requests";
|
||||
}
|
||||
|
||||
private String createTransactionsUrl(BankAccount account){
|
||||
return "/my/banks/"+ account.getBankId() + "/accounts/" + account.getUserId() + "/transactions";
|
||||
return "/my/banks/"+ account.getBankId() + "/accounts/" + account.getUserId() + "/transactions?" + generateRandomString();
|
||||
}
|
||||
|
||||
private String createBalanceRelativeUrl(BankAccount account){ return "/banks/" + account.getBankId() + "/balances"; }
|
||||
|
||||
private static String generateRandomString(){
|
||||
return UUID.randomUUID().toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package fr.lengrand.dialogflowfunapi.openbankproject;
|
||||
|
||||
import fr.lengrand.dialogflowfunapi.openbankproject.auth.Auth;
|
||||
import fr.lengrand.dialogflowfunapi.openbankproject.auth.JSONBodyHandler;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -19,8 +20,6 @@ public class OpenBankHandler {
|
||||
|
||||
public static final String BASE_URL = "https://psd2-api.openbankproject.com/obp/v4.0.0/";
|
||||
|
||||
private HttpClient client = HttpClient.newHttpClient();
|
||||
|
||||
public <T> T post(JSONBodyHandler<T> jsonBodyHandler, String relativeUrl, String body) throws IOException, InterruptedException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.headers("Content-Type", "application/json",
|
||||
@@ -29,7 +28,7 @@ public class OpenBankHandler {
|
||||
.POST(HttpRequest.BodyPublishers.ofString(body))
|
||||
.build();
|
||||
|
||||
HttpResponse<T> response = client.send(request, jsonBodyHandler);
|
||||
HttpResponse<T> response = HttpClient.newHttpClient().send(request, jsonBodyHandler);
|
||||
return response.body();
|
||||
}
|
||||
|
||||
@@ -40,7 +39,7 @@ public class OpenBankHandler {
|
||||
.uri(URI.create(BASE_URL + relativeUrl))
|
||||
.build();
|
||||
|
||||
HttpResponse<T> response = client.send(request, jsonBodyHandler);
|
||||
HttpResponse<T> response = HttpClient.newHttpClient().send(request, jsonBodyHandler);
|
||||
return response.body();
|
||||
}
|
||||
|
||||
|
||||
@@ -41,6 +41,14 @@ public class Auth {
|
||||
token = Optional.of(response.body().getToken());
|
||||
}
|
||||
|
||||
public void safeAuthenticate(){
|
||||
try {
|
||||
this.authenticate();
|
||||
} catch (InterruptedException | IOException e) {
|
||||
System.out.println("Error while authenticating");
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isAuthenticated() {
|
||||
return token.isPresent() && token.get() != null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user