Skip to content

Commit

Permalink
Change apiUrl and adapt method for params
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomallada1 committed Oct 17, 2024
1 parent dfb1005 commit 053cea1
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 18 deletions.
42 changes: 29 additions & 13 deletions library/src/main/java/com/fidesmo/fdsm/FidesmoApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.Map;
import java.text.MessageFormat;

public class FidesmoApiClient {
public static final String APIv2 = "https://api.fidesmo.com/";
public static final String APIv3 = "https://api.fidesmo.com/v3";

public static final String APPS_URL = "apps%s";
public static final String APP_INFO_URL = "apps/%s";
Expand Down Expand Up @@ -95,7 +96,7 @@ public class FidesmoApiClient {

@Deprecated
public FidesmoApiClient() {
this(APIv2, null, null);
this(APIv3, null, null);
}

public FidesmoApiClient(String url, ClientAuthentication authentication, OutputStream apidump) {
Expand All @@ -111,7 +112,7 @@ public FidesmoApiClient(String url, ClientAuthentication authentication, OutputS
}

public FidesmoApiClient(ClientAuthentication authentication, OutputStream apidump) {
this(APIv2, authentication, apidump);
this(APIv3, authentication, apidump);
}

public CloseableHttpResponse get(URI uri) throws IOException {
Expand Down Expand Up @@ -193,7 +194,7 @@ public JsonNode rpc(URI uri, JsonNode request) throws IOException {

req.setHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.toString());
req.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
req.setHeader("Protocol-Requirements", "parametrisedTranslations");
req.setHeader(HttpHeaders.ACCEPT_LANGUAGE, Locale.getDefault().toLanguageTag());

try (CloseableHttpResponse response = transmit(req)) {
if (apidump != null) {
Expand Down Expand Up @@ -242,25 +243,40 @@ public static String getVersion() {

// Prefer English if system locale is not present
// to convert a possible multilanguage node to a string
public static String lamei18n(JsonNode n) {
public static String lamei18n(JsonNode n) {
// For missing values
if (n == null)
return "";
if (!n.isEmpty()) {
//Check if JSON comes in new Translation format, it can be multilanguage
JsonNode jsonNodeFormat = (n.has("text")) ? n.get("text") : n;
try {
Map<String, Object> langs = mapper.convertValue(jsonNodeFormat, new TypeReference<Map<String, Object>>() {
boolean isNewFormat = n.has("text");
if (isNewFormat) {
return FidesmoApiClient.buildMessageWithParams(n);
}
else {
Map<String, Object> langs = mapper.convertValue(n, new TypeReference<Map<String, Object>>() {
});
Map.Entry<String, Object> first = langs.entrySet().iterator().next();
return langs.getOrDefault(Locale.getDefault().getLanguage(), langs.getOrDefault("en", first.getValue())).toString();
}
catch (Exception e) {
return jsonNodeFormat.asText();
}

} else {
return n.asText();
}
}


public static String buildMessageWithParams(JsonNode n) {

String text = n.path("text").asText();

JsonNode paramsNode = n.path("params");

// Convert the params array to String[]
String[] params = new String[paramsNode.size()];
for (int i = 0; i < paramsNode.size(); i++) {
params[i] = paramsNode.get(i).asText();
}

MessageFormat mf = new MessageFormat(text.replace("'", "''"));
return mf.format(params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public DeliveryResult deliver(BIBO bibo, String appId, String serviceId) throws

JsonNode delivery = client.rpc(client.getURI(FidesmoApiClient.SERVICE_DELIVER_URL), deliveryRequest);
String sessionId = delivery.get("sessionId").asText();

logger.info("Delivering: {}", FidesmoApiClient.lamei18n(description.get("title")));
logger.info("Session ID: {}", sessionId);

Expand Down
68 changes: 68 additions & 0 deletions library/src/test/java/com/fidesmo/fdsm/TranslationsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.fidesmo.fdsm;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import org.testng.annotations.Test;
import com.fasterxml.jackson.databind.ObjectMapper;

import static org.testng.Assert.assertEquals;

public class TranslationsTest {

// @Test
public void returnMessagesInOldFormat() throws JsonProcessingException {
String jsonString = "{ \"fr\" : \"Connecter votre carte de paiement\",\n" +
" \"it\" : \"Associa carta di pagamento\",\n" +
" \"nl\" : \"Koppel uw betaalkaart\",\n" +
" \"de\" : \"Karte verbinden\",\n" +
" \"ru\" : \"Подключить платежную карту\",\n" +
" \"sv\" : \"Anslut betalkort\",\n" +
" \"en\" : \"Connect payment card\"\n}";

ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonString);

assertEquals(FidesmoApiClient.lamei18n(jsonNode), "Connect payment card");
}
@Test
public void returnMessagesInNewFormatWithParams() throws JsonProcessingException {
String jsonString = "{ \"id\": \"service.statuses.success\", \"text\": \"Congrats! Now you can pay with your {0}\", \"params\": [ \"Ring!\" ] }";

// Parse JSON to JsonNode
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonString);

assertEquals(FidesmoApiClient.lamei18n(jsonNode), "Congrats! Now you can pay with your Ring!");
}

@Test
public void returnMessagesInNewFormatWithMultipleParams() throws JsonProcessingException {
String jsonString = "{ \"id\": \"service.statuses.success\", \"text\": \"Congrats! Now you can pay with your {0} from {1}\", \"params\": [ \"Ring!\", \"your bank\" ] }";

// Parse JSON to JsonNode
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonString);

assertEquals(FidesmoApiClient.lamei18n(jsonNode), "Congrats! Now you can pay with your Ring! from your bank");
}

@Test
public void returnMessagesInNewFormatWithApostrophes() throws JsonProcessingException {
String jsonString = "{ \"id\": \"service.statuses.success\", \"text\": \"You're now able to pay with your {0}\", \"params\": [ \"Ring!\"] }";

ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonString);

assertEquals(FidesmoApiClient.lamei18n(jsonNode), "You're now able to pay with your Ring!");
}

@Test
public void returnMessagesInNewFormatWithoutParams() throws JsonProcessingException {
String jsonString = "{ \"id\": \"service.statuses.success\", \"text\": \"Your card has been removed\"}";

ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonString);

assertEquals(FidesmoApiClient.lamei18n(jsonNode), "Your card has been removed");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected static void inspectEnvironment(OptionSet args) {

// API URL
try {
String url = new URL(System.getenv().getOrDefault(ENV_FIDESMO_API_URL, FidesmoApiClient.APIv2)).toString();
String url = new URL(System.getenv().getOrDefault(ENV_FIDESMO_API_URL, FidesmoApiClient.APIv3)).toString();
// normalize URL
apiurl = url.endsWith("/") ? url : url + "/";
} catch (MalformedURLException e) {
Expand Down
4 changes: 1 addition & 3 deletions tool/src/main/java/com/fidesmo/fdsm/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,7 @@ public static void main(String[] argv) {
DeliveryUrl delivery = DeliveryUrl.parse(args.valueOf(OPT_RUN));

if (delivery.isWebSocket()) {
if (fidesmoMetadata.isPresent()) {
fidesmoMetadata.get().selectEmpty(bibo);
}
fidesmoMetadata.ifPresent(fidesmoCard -> fidesmoCard.selectEmpty(bibo));
boolean success = WsClient.execute(new URI(delivery.getService()), bibo, auth).join().isSuccess();
if (!success) {
fail("Fail to run a script");
Expand Down

0 comments on commit 053cea1

Please sign in to comment.