From ef33293ec6a93883aa3d98484e8a8aaa99a07fd5 Mon Sep 17 00:00:00 2001
From: capossele <angelocapossele@gmail.com>
Date: Tue, 16 Jun 2020 21:20:33 +0100
Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20sendPayload=20API?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 plugins/webapi/message/sendPayload.go | 44 +++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
 create mode 100644 plugins/webapi/message/sendPayload.go

diff --git a/plugins/webapi/message/sendPayload.go b/plugins/webapi/message/sendPayload.go
new file mode 100644
index 00000000..1d88b622
--- /dev/null
+++ b/plugins/webapi/message/sendPayload.go
@@ -0,0 +1,44 @@
+package message
+
+import (
+	"net/http"
+
+	"github.com/iotaledger/goshimmer/packages/binary/messagelayer/payload"
+	"github.com/iotaledger/goshimmer/plugins/issuer"
+	"github.com/labstack/echo"
+)
+
+// sendPayload creates a message of the given payload and
+// broadcasts it to the node's neighbors. It returns the message ID if successful.
+func sendPayload(c echo.Context) error {
+	var request MessageRequest
+	if err := c.Bind(&request); err != nil {
+		log.Info(err.Error())
+		return c.JSON(http.StatusBadRequest, MessageResponse{Error: err.Error()})
+	}
+
+	//TODO: to check max payload size allowed, if exceeding return an error
+
+	parsedPayload, _, err := payload.FromBytes(request.Payload)
+	if err != nil {
+		return c.JSON(http.StatusBadRequest, MessageResponse{Error: "not a valid payload"})
+	}
+
+	msg, err := issuer.IssuePayload(parsedPayload)
+	if err != nil {
+		return c.JSON(http.StatusBadRequest, MessageResponse{Error: err.Error()})
+	}
+
+	return c.JSON(http.StatusOK, MessageResponse{ID: msg.Id().String()})
+}
+
+// MessageResponse contains the ID of the message sent.
+type MessageResponse struct {
+	ID    string `json:"id,omitempty"`
+	Error string `json:"error,omitempty"`
+}
+
+// MessageRequest contains the message to send.
+type MessageRequest struct {
+	Payload []byte `json:"payload"`
+}
-- 
GitLab