Skip to content
Snippets Groups Projects
Commit 91241bd2 authored by capossele's avatar capossele
Browse files

:recycle: refactors web api and client

parent c5d3a43e
Branches
Tags
No related merge requests found
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
}
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
}
...@@ -9,12 +9,6 @@ import ( ...@@ -9,12 +9,6 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "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 ( var (
...@@ -27,13 +21,6 @@ var ( ...@@ -27,13 +21,6 @@ var (
) )
const ( const (
routeBroadcastData = "broadcastData"
routeGetMessageById = "getMessageById"
routeGetNeighbors = "getNeighbors"
routeSpammer = "spammer"
routeLogin = "login"
//routeGetTransactionsToApprove = "getTransactionsToApprove"
contentTypeJSON = "application/json" contentTypeJSON = "application/json"
) )
...@@ -134,81 +121,3 @@ func (api *GoShimmerAPI) do(method string, route string, reqObj interface{}, res ...@@ -134,81 +121,3 @@ func (api *GoShimmerAPI) do(method string, route string, reqObj interface{}, res
} }
return nil 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
}
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
}
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
}
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
}
...@@ -2,10 +2,10 @@ package webapi ...@@ -2,10 +2,10 @@ package webapi
import ( import (
"github.com/iotaledger/goshimmer/plugins/webapi" "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/drng"
"github.com/iotaledger/goshimmer/plugins/webapi/findMessageById" "github.com/iotaledger/goshimmer/plugins/webapi/message"
"github.com/iotaledger/goshimmer/plugins/webapi/getNeighbors"
"github.com/iotaledger/goshimmer/plugins/webapi/spammer" "github.com/iotaledger/goshimmer/plugins/webapi/spammer"
"github.com/iotaledger/goshimmer/plugins/webauth" "github.com/iotaledger/goshimmer/plugins/webauth"
"github.com/iotaledger/hive.go/node" "github.com/iotaledger/hive.go/node"
...@@ -14,10 +14,9 @@ import ( ...@@ -14,10 +14,9 @@ import (
var PLUGINS = node.Plugins( var PLUGINS = node.Plugins(
webapi.PLUGIN, webapi.PLUGIN,
webauth.PLUGIN, webauth.PLUGIN,
//gtta.PLUGIN,
spammer.PLUGIN, spammer.PLUGIN,
broadcastData.PLUGIN, data.PLUGIN,
drng.PLUGIN, drng.PLUGIN,
findMessageById.PLUGIN, message.PLUGIN,
getNeighbors.PLUGIN, autopeering.PLUGIN,
) )
package getNeighbors package autopeering
import ( import (
"encoding/base64" "encoding/base64"
...@@ -14,10 +14,10 @@ import ( ...@@ -14,10 +14,10 @@ import (
"github.com/labstack/echo" "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) { 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 // getNeighbors returns the chosen and accepted neighbors of the node
......
package broadcastData package data
import ( import (
"net/http" "net/http"
...@@ -11,12 +11,12 @@ import ( ...@@ -11,12 +11,12 @@ import (
"github.com/labstack/echo" "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 var log *logger.Logger
func configure(plugin *node.Plugin) { func configure(plugin *node.Plugin) {
log = logger.NewLogger("API-broadcastData") log = logger.NewLogger("API-data")
webapi.Server.POST("broadcastData", broadcastData) webapi.Server.POST("data", broadcastData)
} }
// broadcastData creates a message of the given payload and // broadcastData creates a message of the given payload and
......
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"`
}
package findMessageById package message
import ( import (
"net/http" "net/http"
...@@ -12,12 +12,12 @@ import ( ...@@ -12,12 +12,12 @@ import (
"github.com/iotaledger/hive.go/node" "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 var log *logger.Logger
func configure(plugin *node.Plugin) { func configure(plugin *node.Plugin) {
log = logger.NewLogger("API-findMessageById") log = logger.NewLogger("API-message")
webapi.Server.POST("findMessageById", findMessageById) webapi.Server.POST("message/findById", findMessageById)
} }
// findMessageById returns the array of messages for the // findMessageById returns the array of messages for the
......
...@@ -10,7 +10,7 @@ import ( ...@@ -10,7 +10,7 @@ import (
) )
func testBroadcastData(api *client.GoShimmerAPI) (string, error) { func testBroadcastData(api *client.GoShimmerAPI) (string, error) {
msgId, err := api.BroadcastData([]byte(msgData)) msgId, err := api.Data([]byte(msgData))
if err != nil { if err != nil {
return "", fmt.Errorf("broadcast failed: %w", err) return "", fmt.Errorf("broadcast failed: %w", err)
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment