Skip to content
Snippets Groups Projects
Unverified Commit 3d018884 authored by Levente Pap's avatar Levente Pap Committed by GitHub
Browse files

Send payload api (#501)

* :sparkles: Add sendPayload API

* :construction: WIP

* :rotating_light:

 Fix webapi linter warnings

* Fix after cherry-pick

* Fix linter

Co-authored-by: default avatarcapossele <angelocapossele@gmail.com>
parent 0b68bca0
No related branches found
No related tags found
No related merge requests found
...@@ -7,7 +7,8 @@ import ( ...@@ -7,7 +7,8 @@ import (
) )
const ( 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 // 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 ...@@ -26,3 +27,14 @@ func (api *GoShimmerAPI) FindMessageByID(base58EncodedIDs []string) (*webapi_mes
return res, nil 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
}
...@@ -23,6 +23,7 @@ var ( ...@@ -23,6 +23,7 @@ var (
func configure(plugin *node.Plugin) { func configure(plugin *node.Plugin) {
log = logger.NewLogger(PluginName) log = logger.NewLogger(PluginName)
webapi.Server.POST("message/findById", findMessageByID) webapi.Server.POST("message/findById", findMessageByID)
webapi.Server.POST("message/sendPayload", sendPayload)
} }
// findMessageByID returns the array of messages for the // findMessageByID returns the array of messages for the
......
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"`
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment