diff --git a/client/autopeering.go b/client/autopeering.go new file mode 100644 index 0000000000000000000000000000000000000000..32796b9bd30822fcbcf1deca747b47f43f98045c --- /dev/null +++ b/client/autopeering.go @@ -0,0 +1,27 @@ +package client + +import ( + "fmt" + "net/http" + + webapi_autopeering "github.com/iotaledger/goshimmer/plugins/webapi/autopeering" +) + +const ( + routeGetNeighbors = "getNeighbors" +) + +// GetNeighbors gets the chosen/accepted neighbors. +// If knownPeers is set, also all known peers to the node are returned additionally. +func (api *GoShimmerAPI) GetNeighbors(knownPeers bool) (*webapi_autopeering.Response, error) { + res := &webapi_autopeering.Response{} + if err := api.do(http.MethodGet, func() string { + if !knownPeers { + return routeGetNeighbors + } + return fmt.Sprintf("%s?known=1", routeGetNeighbors) + }(), nil, res); err != nil { + return nil, err + } + return res, nil +} diff --git a/client/data.go b/client/data.go new file mode 100644 index 0000000000000000000000000000000000000000..e18d5ed7f2f481c07f5264fef9e1b18b928a4d6d --- /dev/null +++ b/client/data.go @@ -0,0 +1,23 @@ +package client + +import ( + "net/http" + + webapi_data "github.com/iotaledger/goshimmer/plugins/webapi/data" +) + +const ( + routeData = "data" +) + +// Data sends the given data (payload) by creating a message in the backend. +func (api *GoShimmerAPI) Data(data []byte) (string, error) { + + res := &webapi_data.Response{} + if err := api.do(http.MethodPost, routeData, + &webapi_data.Request{Data: data}, res); err != nil { + return "", err + } + + return res.Id, nil +} diff --git a/client/lib.go b/client/lib.go index 5ebbb5f475d05461b7d47ad7484e94600592c34d..1201aa1cf25c4771563b8b081794369bdff010c7 100644 --- a/client/lib.go +++ b/client/lib.go @@ -9,12 +9,6 @@ import ( "io" "io/ioutil" "net/http" - - webapi_broadcastData "github.com/iotaledger/goshimmer/plugins/webapi/broadcastData" - webapi_findMessageById "github.com/iotaledger/goshimmer/plugins/webapi/findMessageById" - webapi_getNeighbors "github.com/iotaledger/goshimmer/plugins/webapi/getNeighbors" - webapi_spammer "github.com/iotaledger/goshimmer/plugins/webapi/spammer" - webapi_auth "github.com/iotaledger/goshimmer/plugins/webauth" ) var ( @@ -27,13 +21,6 @@ var ( ) const ( - routeBroadcastData = "broadcastData" - routeGetMessageById = "getMessageById" - routeGetNeighbors = "getNeighbors" - routeSpammer = "spammer" - routeLogin = "login" - //routeGetTransactionsToApprove = "getTransactionsToApprove" - contentTypeJSON = "application/json" ) @@ -134,81 +121,3 @@ func (api *GoShimmerAPI) do(method string, route string, reqObj interface{}, res } return nil } - -// Login authorizes this API instance against the web API. -// You must call this function before any before any other call, if the web-auth plugin is enabled. -func (api *GoShimmerAPI) Login(username string, password string) error { - res := &webapi_auth.Response{} - if err := api.do(http.MethodPost, routeLogin, - &webapi_auth.Request{Username: username, Password: password}, res); err != nil { - return err - } - api.jwt = res.Token - return nil -} - -// BroadcastData sends the given data (payload) by creating a message in the backend. -func (api *GoShimmerAPI) BroadcastData(data []byte) (string, error) { - - res := &webapi_broadcastData.Response{} - if err := api.do(http.MethodPost, routeBroadcastData, - &webapi_broadcastData.Request{Data: data}, res); err != nil { - return "", err - } - - return res.Id, nil -} - -func (api *GoShimmerAPI) FindMessageById(base58EncodedIds []string) (*webapi_findMessageById.Response, error) { - res := &webapi_findMessageById.Response{} - - err := api.do( - http.MethodPost, - routeGetMessageById, - &webapi_findMessageById.Request{Ids: base58EncodedIds}, - res, - ) - if err != nil { - return nil, err - } - - return res, nil -} - -// GetNeighbors gets the chosen/accepted neighbors. -// If knownPeers is set, also all known peers to the node are returned additionally. -func (api *GoShimmerAPI) GetNeighbors(knownPeers bool) (*webapi_getNeighbors.Response, error) { - res := &webapi_getNeighbors.Response{} - if err := api.do(http.MethodGet, func() string { - if !knownPeers { - return routeGetNeighbors - } - return fmt.Sprintf("%s?known=1", routeGetNeighbors) - }(), nil, res); err != nil { - return nil, err - } - return res, nil -} - -// // GetTips executes the tip-selection on the node to retrieve tips to approve. -// func (api *GoShimmerAPI) GetTransactionsToApprove() (*webapi_gtta.Response, error) { -// res := &webapi_gtta.Response{} -// if err := api.do(http.MethodGet, routeGetTransactionsToApprove, nil, res); err != nil { -// return nil, err -// } -// return res, nil -// } - -// ToggleSpammer toggles the node internal spammer. -func (api *GoShimmerAPI) ToggleSpammer(enable bool) (*webapi_spammer.Response, error) { - res := &webapi_spammer.Response{} - if err := api.do(http.MethodGet, func() string { - if enable { - return fmt.Sprintf("%s?cmd=start", routeSpammer) - } - return fmt.Sprintf("%s?cmd=stop", routeSpammer) - }(), nil, res); err != nil { - return nil, err - } - return res, nil -} diff --git a/client/login.go b/client/login.go new file mode 100644 index 0000000000000000000000000000000000000000..5183aa2153407335d716384c3b614f1e28159f3f --- /dev/null +++ b/client/login.go @@ -0,0 +1,23 @@ +package client + +import ( + "net/http" + + webapi_auth "github.com/iotaledger/goshimmer/plugins/webauth" +) + +const ( + routeLogin = "login" +) + +// Login authorizes this API instance against the web API. +// You must call this function before any before any other call, if the web-auth plugin is enabled. +func (api *GoShimmerAPI) Login(username string, password string) error { + res := &webapi_auth.Response{} + if err := api.do(http.MethodPost, routeLogin, + &webapi_auth.Request{Username: username, Password: password}, res); err != nil { + return err + } + api.jwt = res.Token + return nil +} diff --git a/client/message.go b/client/message.go new file mode 100644 index 0000000000000000000000000000000000000000..181b2632c4ed441ecc173eaad953f8b47f254d0f --- /dev/null +++ b/client/message.go @@ -0,0 +1,27 @@ +package client + +import ( + "net/http" + + webapi_message "github.com/iotaledger/goshimmer/plugins/webapi/message" +) + +const ( + routeFindById = "findById" +) + +func (api *GoShimmerAPI) FindMessageById(base58EncodedIds []string) (*webapi_message.Response, error) { + res := &webapi_message.Response{} + + err := api.do( + http.MethodPost, + routeFindById, + &webapi_message.Request{Ids: base58EncodedIds}, + res, + ) + if err != nil { + return nil, err + } + + return res, nil +} diff --git a/client/spammer.go b/client/spammer.go new file mode 100644 index 0000000000000000000000000000000000000000..0e747a69380b15061664e17843f8bfa57086fc4b --- /dev/null +++ b/client/spammer.go @@ -0,0 +1,26 @@ +package client + +import ( + "fmt" + "net/http" + + webapi_spammer "github.com/iotaledger/goshimmer/plugins/webapi/spammer" +) + +const ( + routeSpammer = "spammer" +) + +// ToggleSpammer toggles the node internal spammer. +func (api *GoShimmerAPI) ToggleSpammer(enable bool) (*webapi_spammer.Response, error) { + res := &webapi_spammer.Response{} + if err := api.do(http.MethodGet, func() string { + if enable { + return fmt.Sprintf("%s?cmd=start", routeSpammer) + } + return fmt.Sprintf("%s?cmd=stop", routeSpammer) + }(), nil, res); err != nil { + return nil, err + } + return res, nil +} diff --git a/pluginmgr/webapi/plugins.go b/pluginmgr/webapi/plugins.go index b67a824604077993f06603aa7e8713fe7dbf4994..6299901d87e3547c2acd8be3306522e91ad87ec6 100644 --- a/pluginmgr/webapi/plugins.go +++ b/pluginmgr/webapi/plugins.go @@ -2,10 +2,10 @@ package webapi import ( "github.com/iotaledger/goshimmer/plugins/webapi" - "github.com/iotaledger/goshimmer/plugins/webapi/broadcastData" + "github.com/iotaledger/goshimmer/plugins/webapi/autopeering" + "github.com/iotaledger/goshimmer/plugins/webapi/data" "github.com/iotaledger/goshimmer/plugins/webapi/drng" - "github.com/iotaledger/goshimmer/plugins/webapi/findMessageById" - "github.com/iotaledger/goshimmer/plugins/webapi/getNeighbors" + "github.com/iotaledger/goshimmer/plugins/webapi/message" "github.com/iotaledger/goshimmer/plugins/webapi/spammer" "github.com/iotaledger/goshimmer/plugins/webauth" "github.com/iotaledger/hive.go/node" @@ -14,10 +14,9 @@ import ( var PLUGINS = node.Plugins( webapi.PLUGIN, webauth.PLUGIN, - //gtta.PLUGIN, spammer.PLUGIN, - broadcastData.PLUGIN, + data.PLUGIN, drng.PLUGIN, - findMessageById.PLUGIN, - getNeighbors.PLUGIN, + message.PLUGIN, + autopeering.PLUGIN, ) diff --git a/plugins/webapi/getNeighbors/plugin.go b/plugins/webapi/autopeering/plugin.go similarity index 95% rename from plugins/webapi/getNeighbors/plugin.go rename to plugins/webapi/autopeering/plugin.go index acedd3f3f92bf5a705511f02099fbc51283b0c4d..5ee22878c7d43236ec71626fb6358111e63a85da 100644 --- a/plugins/webapi/getNeighbors/plugin.go +++ b/plugins/webapi/autopeering/plugin.go @@ -1,4 +1,4 @@ -package getNeighbors +package autopeering import ( "encoding/base64" @@ -14,10 +14,10 @@ import ( "github.com/labstack/echo" ) -var PLUGIN = node.NewPlugin("WebAPI getNeighbors Endpoint", node.Enabled, configure) +var PLUGIN = node.NewPlugin("WebAPI autopeering Endpoint", node.Enabled, configure) func configure(plugin *node.Plugin) { - webapi.Server.GET("getNeighbors", getNeighbors) + webapi.Server.GET("autopeering/neighbors", getNeighbors) } // getNeighbors returns the chosen and accepted neighbors of the node diff --git a/plugins/webapi/broadcastData/plugin.go b/plugins/webapi/data/plugin.go similarity index 84% rename from plugins/webapi/broadcastData/plugin.go rename to plugins/webapi/data/plugin.go index 4b71c2fa2688c53180579c2f09e6260927a51d91..643cacaf78333288b3693d080cb47e729fbb971a 100644 --- a/plugins/webapi/broadcastData/plugin.go +++ b/plugins/webapi/data/plugin.go @@ -1,4 +1,4 @@ -package broadcastData +package data import ( "net/http" @@ -11,12 +11,12 @@ import ( "github.com/labstack/echo" ) -var PLUGIN = node.NewPlugin("WebAPI broadcastData Endpoint", node.Enabled, configure) +var PLUGIN = node.NewPlugin("WebAPI data Endpoint", node.Enabled, configure) var log *logger.Logger func configure(plugin *node.Plugin) { - log = logger.NewLogger("API-broadcastData") - webapi.Server.POST("broadcastData", broadcastData) + log = logger.NewLogger("API-data") + webapi.Server.POST("data", broadcastData) } // broadcastData creates a message of the given payload and diff --git a/plugins/webapi/gtta/plugin.go b/plugins/webapi/gtta/plugin.go deleted file mode 100644 index 6ecd48a8f756956a22dbdb6028917c7271b34e5d..0000000000000000000000000000000000000000 --- a/plugins/webapi/gtta/plugin.go +++ /dev/null @@ -1,30 +0,0 @@ -package gtta - -import ( - "net/http" - - "github.com/iotaledger/hive.go/node" - "github.com/labstack/echo" - - "github.com/iotaledger/goshimmer/packages/binary/messagelayer/message" - "github.com/iotaledger/goshimmer/plugins/messagelayer" - "github.com/iotaledger/goshimmer/plugins/webapi" -) - -var PLUGIN = node.NewPlugin("WebAPI GTTA Endpoint", node.Disabled, func(plugin *node.Plugin) { - webapi.Server.GET("getTransactionsToApprove", Handler) -}) - -func Handler(c echo.Context) error { - trunkTransactionId, branchTransactionId := messagelayer.TipSelector.GetTips() - - return c.JSON(http.StatusOK, Response{ - TrunkTransaction: trunkTransactionId, - BranchTransaction: branchTransactionId, - }) -} - -type Response struct { - BranchTransaction message.Id `json:"branchTransaction"` - TrunkTransaction message.Id `json:"trunkTransaction"` -} diff --git a/plugins/webapi/findMessageById/plugin.go b/plugins/webapi/message/plugin.go similarity index 91% rename from plugins/webapi/findMessageById/plugin.go rename to plugins/webapi/message/plugin.go index 806aa04eb113f72dca6daca286b8e95d153f0089..509a1dde5ce542856de701f55ffd8c398e606c81 100644 --- a/plugins/webapi/findMessageById/plugin.go +++ b/plugins/webapi/message/plugin.go @@ -1,4 +1,4 @@ -package findMessageById +package message import ( "net/http" @@ -12,12 +12,12 @@ import ( "github.com/iotaledger/hive.go/node" ) -var PLUGIN = node.NewPlugin("WebAPI findMessageById Endpoint", node.Enabled, configure) +var PLUGIN = node.NewPlugin("WebAPI message Endpoint", node.Enabled, configure) var log *logger.Logger func configure(plugin *node.Plugin) { - log = logger.NewLogger("API-findMessageById") - webapi.Server.POST("findMessageById", findMessageById) + log = logger.NewLogger("API-message") + webapi.Server.POST("message/findById", findMessageById) } // findMessageById returns the array of messages for the diff --git a/tools/relay-checker/main.go b/tools/relay-checker/main.go index df7e812626a6cce48707ed455feca16d36e009a7..2c8b0f75af980dc00ea4451236393f72faef9551 100644 --- a/tools/relay-checker/main.go +++ b/tools/relay-checker/main.go @@ -10,7 +10,7 @@ import ( ) func testBroadcastData(api *client.GoShimmerAPI) (string, error) { - msgId, err := api.BroadcastData([]byte(msgData)) + msgId, err := api.Data([]byte(msgData)) if err != nil { return "", fmt.Errorf("broadcast failed: %w", err) }