From 91241bd2a566c5dce001fe9cb11a162e9ee698eb Mon Sep 17 00:00:00 2001
From: capossele <angelocapossele@gmail.com>
Date: Tue, 7 Apr 2020 15:32:46 +0100
Subject: [PATCH] :recycle: refactors web api and client

---
 client/autopeering.go                         | 27 ++++++
 client/data.go                                | 23 +++++
 client/lib.go                                 | 91 -------------------
 client/login.go                               | 23 +++++
 client/message.go                             | 27 ++++++
 client/spammer.go                             | 26 ++++++
 pluginmgr/webapi/plugins.go                   | 13 ++-
 .../{getNeighbors => autopeering}/plugin.go   |  6 +-
 .../webapi/{broadcastData => data}/plugin.go  |  8 +-
 plugins/webapi/gtta/plugin.go                 | 30 ------
 .../{findMessageById => message}/plugin.go    |  8 +-
 tools/relay-checker/main.go                   |  2 +-
 12 files changed, 144 insertions(+), 140 deletions(-)
 create mode 100644 client/autopeering.go
 create mode 100644 client/data.go
 create mode 100644 client/login.go
 create mode 100644 client/message.go
 create mode 100644 client/spammer.go
 rename plugins/webapi/{getNeighbors => autopeering}/plugin.go (95%)
 rename plugins/webapi/{broadcastData => data}/plugin.go (84%)
 delete mode 100644 plugins/webapi/gtta/plugin.go
 rename plugins/webapi/{findMessageById => message}/plugin.go (91%)

diff --git a/client/autopeering.go b/client/autopeering.go
new file mode 100644
index 00000000..32796b9b
--- /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 00000000..e18d5ed7
--- /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 5ebbb5f4..1201aa1c 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 00000000..5183aa21
--- /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 00000000..181b2632
--- /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 00000000..0e747a69
--- /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 b67a8246..6299901d 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 acedd3f3..5ee22878 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 4b71c2fa..643cacaf 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 6ecd48a8..00000000
--- 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 806aa04e..509a1dde 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 df7e8126..2c8b0f75 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)
 	}
-- 
GitLab