From 3d018884767450ec1e6351befd27bf8418f7ad8d Mon Sep 17 00:00:00 2001
From: Levente Pap <levente.pap@iota.org>
Date: Fri, 19 Jun 2020 12:43:26 +0200
Subject: [PATCH] Send payload api (#501)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* ✨ Add sendPayload API

* 🚧 WIP

* 🚨 Fix webapi linter warnings

* Fix after cherry-pick

* Fix linter

Co-authored-by: capossele <angelocapossele@gmail.com>
---
 client/message.go                     | 14 ++++++++-
 plugins/webapi/message/plugin.go      |  1 +
 plugins/webapi/message/sendPayload.go | 44 +++++++++++++++++++++++++++
 3 files changed, 58 insertions(+), 1 deletion(-)
 create mode 100644 plugins/webapi/message/sendPayload.go

diff --git a/client/message.go b/client/message.go
index 267dbb34..a920c336 100644
--- a/client/message.go
+++ b/client/message.go
@@ -7,7 +7,8 @@ import (
 )
 
 const (
-	routeFindByID = "message/findById"
+	routeFindByID    = "message/findById"
+	routeSendPayload = "message/sendPayload"
 )
 
 // FindMessageByID finds messages by the given base58 encoded IDs. The messages are returned in the same order as
@@ -26,3 +27,14 @@ func (api *GoShimmerAPI) FindMessageByID(base58EncodedIDs []string) (*webapi_mes
 
 	return res, nil
 }
+
+// SendPayload send a message with the given payload.
+func (api *GoShimmerAPI) SendPayload(payload []byte) (string, error) {
+	res := &webapi_message.MsgResponse{}
+	if err := api.do(http.MethodPost, routeSendPayload,
+		&webapi_message.MsgRequest{Payload: payload}, res); err != nil {
+		return "", err
+	}
+
+	return res.ID, nil
+}
diff --git a/plugins/webapi/message/plugin.go b/plugins/webapi/message/plugin.go
index 1ebbf8b7..bdba4f37 100644
--- a/plugins/webapi/message/plugin.go
+++ b/plugins/webapi/message/plugin.go
@@ -23,6 +23,7 @@ var (
 func configure(plugin *node.Plugin) {
 	log = logger.NewLogger(PluginName)
 	webapi.Server.POST("message/findById", findMessageByID)
+	webapi.Server.POST("message/sendPayload", sendPayload)
 }
 
 // findMessageByID returns the array of messages for the
diff --git a/plugins/webapi/message/sendPayload.go b/plugins/webapi/message/sendPayload.go
new file mode 100644
index 00000000..159ffe16
--- /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 MsgRequest
+	if err := c.Bind(&request); err != nil {
+		log.Info(err.Error())
+		return c.JSON(http.StatusBadRequest, MsgResponse{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, MsgResponse{Error: "not a valid payload"})
+	}
+
+	msg, err := issuer.IssuePayload(parsedPayload)
+	if err != nil {
+		return c.JSON(http.StatusBadRequest, MsgResponse{Error: err.Error()})
+	}
+
+	return c.JSON(http.StatusOK, MsgResponse{ID: msg.Id().String()})
+}
+
+// MsgResponse contains the ID of the message sent.
+type MsgResponse struct {
+	ID    string `json:"id,omitempty"`
+	Error string `json:"error,omitempty"`
+}
+
+// MsgRequest contains the message to send.
+type MsgRequest struct {
+	Payload []byte `json:"payload"`
+}
-- 
GitLab