AAAAAAAAAAAAAAAAAAAh
This commit is contained in:
@@ -24,11 +24,13 @@ class MailController:
|
||||
with ApiClient(self.config) as api:
|
||||
req = request.get_json()
|
||||
content = req["content"]
|
||||
payload = json.dumps(content)
|
||||
print(payload)
|
||||
|
||||
api.default_headers["Authorization"] = f"Bearer {self._auth_data.get_token()}"
|
||||
mail_api = MailApi(api)
|
||||
|
||||
mail_api.send(StrictStr(f'{{"content": {content}}}'))
|
||||
mail_api.send(StrictStr(payload))
|
||||
return jsonify({"success": True, "message": None}), 200
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
@@ -25,9 +25,8 @@ public class EmailController {
|
||||
}
|
||||
|
||||
@PostMapping("/mail/send")
|
||||
public ResponseEntity<String> send(@RequestBody String payload){
|
||||
public ResponseEntity<String> send(@RequestBody Map<String, Object> payload){
|
||||
try {
|
||||
Logger.displayInfo(payload);
|
||||
service.send("laurent0206.cr@gmail.com", "Hello from board mate !", payload);
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -14,6 +14,8 @@ import com.sendgrid.helpers.mail.objects.Email;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class SendgridService {
|
||||
|
||||
@@ -28,9 +30,9 @@ public class SendgridService {
|
||||
this.senderEmail = email;
|
||||
}
|
||||
|
||||
public void send(String dst, String subject, String text) throws ServiceException {
|
||||
public void send(String dst, String subject, Map<String, Object> payload) throws ServiceException {
|
||||
try {
|
||||
Content content = new Content("text/plain", formatContent(text));
|
||||
Content content = new Content("text/plain", formatContent(payload));
|
||||
Mail mail = new Mail(this.senderEmail, subject, new Email(dst), content);
|
||||
|
||||
Request request = new Request();
|
||||
@@ -46,44 +48,28 @@ public class SendgridService {
|
||||
}
|
||||
}
|
||||
|
||||
private String formatContent(String content) throws JsonProcessingException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode root = mapper.readTree(content);
|
||||
|
||||
JsonNode node = root.has("content") ? root.path("content") : root;
|
||||
|
||||
if (node.isTextual()) {
|
||||
return node.asText();
|
||||
}
|
||||
private String formatContent(Map<String, Object> payload) throws JsonProcessingException {
|
||||
|
||||
StringBuilder mailBody = new StringBuilder();
|
||||
|
||||
mailBody.append("White: ")
|
||||
.append(node.path("white_name").asText("N/A"))
|
||||
.append(payload.get("white_name"))
|
||||
.append("\n");
|
||||
|
||||
mailBody.append("Black: ")
|
||||
.append(node.path("black_name").asText("N/A"))
|
||||
.append(payload.get("black_name"))
|
||||
.append("\n");
|
||||
|
||||
mailBody.append("Time Control: ")
|
||||
.append(node.path("time_control").asInt(0))
|
||||
.append(payload.get("time_control"))
|
||||
.append(" + ")
|
||||
.append(node.path("increment").asInt(0))
|
||||
.append(payload.get("increment"))
|
||||
.append("\n");
|
||||
|
||||
mailBody.append("Timestamp: ")
|
||||
.append(node.path("timestamp").asLong(0))
|
||||
.append(payload.get("timestamp"))
|
||||
.append("\n\n");
|
||||
|
||||
mailBody.append("Moves:\n");
|
||||
int i = 1;
|
||||
for (JsonNode move : node.path("moves")) {
|
||||
mailBody.append(i++).append(". ")
|
||||
.append(move.asText())
|
||||
.append("\n");
|
||||
}
|
||||
|
||||
return mailBody.toString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user