From c08db89ef112e17d290560fb651a92813cfc9974 Mon Sep 17 00:00:00 2001 From: JeremyTubongbanua Date: Wed, 12 Oct 2022 14:43:50 -0400 Subject: [PATCH] feat: `notify:list` ResponseTransformer --- .../atsign/common/ResponseTransformers.java | 49 ++++++++++++++++++- .../response_models/NotifyListResponse.java | 43 ++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 at_client/src/main/java/org/atsign/common/response_models/NotifyListResponse.java diff --git a/at_client/src/main/java/org/atsign/common/ResponseTransformers.java b/at_client/src/main/java/org/atsign/common/ResponseTransformers.java index dd37da49..a004cf7a 100644 --- a/at_client/src/main/java/org/atsign/common/ResponseTransformers.java +++ b/at_client/src/main/java/org/atsign/common/ResponseTransformers.java @@ -1,10 +1,12 @@ package org.atsign.common; +import java.util.ArrayList; import java.util.List; import java.util.Map; import org.atsign.client.api.Secondary.Response; import org.atsign.common.response_models.LlookupAllResponse; +import org.atsign.common.response_models.NotifyListResponse; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -71,6 +73,52 @@ public Map transform(Response value) { } } + public static class NotifyListResponseTransformer implements ResponseTransformer { + /* + * [ + * { + * "id": "8797feda-d2d6-432a-9b9d-cc23184f60eb", + * "from": "@fascinatingsnow", + * "to": "@smoothalligator", + * "key": "@smoothalligator:test@fascinatingsnow", + * "value": null, + * "operation": "update", + * "epochMillis": 1665514962777, + * "messageType": "MessageType.key", + * "isEncrypted": false + * } + * ] + */ + @Override + public NotifyListResponse transform(Response value) { + NotifyListResponse model = new NotifyListResponse(); + model.notifications = new ArrayList(); + + if(value.data == null || value.data.isEmpty() || value.data.equalsIgnoreCase("null")) { + return model; + } + + // value.data == "[{}, {}, {}]" + String s = value.data.substring(1, value.data.length() - 1); // s == "{}, {}, {}" + String[] notificationStrs = s.split(",\\s*\\{"); // notificationStrs == ["{}", "{}", "{}"] + for(int i = 1; i < notificationStrs.length; i++) { + notificationStrs[i] = "{" + notificationStrs[i]; + } + + ObjectMapper mapper = new ObjectMapper(); + for(String notificationStr : notificationStrs) { + try { + NotifyListResponse.Notification notification = mapper.readValue(notificationStr, NotifyListResponse.Notification.class); + model.notifications.add(notification); + } catch (Exception e) { + e.printStackTrace(); + } + } + + return model; + } + } + public static class NotifyResponseTransformer implements ResponseTransformer { @Override public String transform(Response value) { @@ -78,7 +126,6 @@ public String transform(Response value) { } } - public static class NotificationStatusResponseTransformer implements ResponseTransformer { @Override public NotificationStatus transform(Response value) { diff --git a/at_client/src/main/java/org/atsign/common/response_models/NotifyListResponse.java b/at_client/src/main/java/org/atsign/common/response_models/NotifyListResponse.java new file mode 100644 index 00000000..aa44179f --- /dev/null +++ b/at_client/src/main/java/org/atsign/common/response_models/NotifyListResponse.java @@ -0,0 +1,43 @@ +package org.atsign.common.response_models; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class NotifyListResponse { + + @JsonProperty + public List notifications; + + public static class Notification { + @JsonProperty + public String id; + + @JsonProperty + public String from; + + @JsonProperty + public String to; + + @JsonProperty + public String key; + + @JsonProperty + public String value; + + @JsonProperty + public String operation; + + @JsonProperty + public Long epochMillis; + + @JsonProperty + public String messageType; + + @JsonProperty + public Boolean isEncrypted; + + } + + +}