Skip to content

Commit

Permalink
feat: notify:list ResponseTransformer
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyTubongbanua committed Oct 12, 2022
1 parent 984e2ee commit c08db89
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -71,14 +73,59 @@ public Map<String, Object> transform(Response value) {
}
}

public static class NotifyListResponseTransformer implements ResponseTransformer<Response, NotifyListResponse> {
/*
* [
* {
* "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<NotifyListResponse.Notification>();

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<Response, String> {
@Override
public String transform(Response value) {
return value.data;
}
}


public static class NotificationStatusResponseTransformer implements ResponseTransformer<Response, NotificationStatus> {
@Override
public NotificationStatus transform(Response value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Notification> 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;

}


}

0 comments on commit c08db89

Please sign in to comment.