Skip to content
Snippets Groups Projects
Unverified Commit d054e418 authored by Luca Moser's avatar Luca Moser Committed by GitHub
Browse files

Set new txs as modified, adjusts webapi endpoint methods (#120)

parent cc36b062
Branches
Tags
No related merge requests found
...@@ -40,13 +40,16 @@ const ( ...@@ -40,13 +40,16 @@ const (
contentTypeJSON = "application/json" contentTypeJSON = "application/json"
) )
func NewGoShimmerAPI(node string) *GoShimmerAPI { func NewGoShimmerAPI(node string, httpClient ...http.Client) *GoShimmerAPI {
if len(httpClient) > 0 {
return &GoShimmerAPI{node: node, httpClient: httpClient[0]}
}
return &GoShimmerAPI{node: node} return &GoShimmerAPI{node: node}
} }
type GoShimmerAPI struct { type GoShimmerAPI struct {
http.Client httpClient http.Client
node string node string
} }
type errorresponse struct { type errorresponse struct {
...@@ -60,7 +63,7 @@ func interpretBody(res *http.Response, decodeTo interface{}) error { ...@@ -60,7 +63,7 @@ func interpretBody(res *http.Response, decodeTo interface{}) error {
} }
defer res.Body.Close() defer res.Body.Close()
if res.StatusCode == http.StatusOK { if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusCreated {
return json.Unmarshal(resBody, decodeTo) return json.Unmarshal(resBody, decodeTo)
} }
...@@ -80,20 +83,17 @@ func interpretBody(res *http.Response, decodeTo interface{}) error { ...@@ -80,20 +83,17 @@ func interpretBody(res *http.Response, decodeTo interface{}) error {
return errors.Wrap(ErrUnknownError, errRes.Error) return errors.Wrap(ErrUnknownError, errRes.Error)
} }
func (api *GoShimmerAPI) BroadcastData(targetAddress trinary.Trytes, data trinary.Trytes) (trinary.Hash, error) { func (api *GoShimmerAPI) BroadcastData(targetAddress trinary.Trytes, data string) (trinary.Hash, error) {
if !guards.IsHash(targetAddress) { if !guards.IsHash(targetAddress) {
return "", errors.Wrapf(consts.ErrInvalidHash, "invalid address: %s", targetAddress) return "", errors.Wrapf(consts.ErrInvalidHash, "invalid address: %s", targetAddress)
} }
if !guards.IsTrytes(data) {
return "", errors.Wrapf(consts.ErrInvalidTrytes, "invalid trytes: %s", data)
}
reqBytes, err := json.Marshal(&webapi_broadcastData.Request{Address: targetAddress, Data: data}) reqBytes, err := json.Marshal(&webapi_broadcastData.Request{Address: targetAddress, Data: data})
if err != nil { if err != nil {
return "", err return "", err
} }
res, err := api.Post(fmt.Sprintf("%s/%s", api.node, routeBroadcastData), contentTypeJSON, bytes.NewReader(reqBytes)) res, err := api.httpClient.Post(fmt.Sprintf("%s/%s", api.node, routeBroadcastData), contentTypeJSON, bytes.NewReader(reqBytes))
if err != nil { if err != nil {
return "", err return "", err
} }
...@@ -118,7 +118,7 @@ func (api *GoShimmerAPI) GetTrytes(txHashes trinary.Hashes) ([]trinary.Trytes, e ...@@ -118,7 +118,7 @@ func (api *GoShimmerAPI) GetTrytes(txHashes trinary.Hashes) ([]trinary.Trytes, e
return nil, err return nil, err
} }
res, err := api.Post(fmt.Sprintf("%s/%s", api.node, routeGetTrytes), contentTypeJSON, bytes.NewReader(reqBytes)) res, err := api.httpClient.Post(fmt.Sprintf("%s/%s", api.node, routeGetTrytes), contentTypeJSON, bytes.NewReader(reqBytes))
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -143,7 +143,7 @@ func (api *GoShimmerAPI) GetTransactions(txHashes trinary.Hashes) ([]webapi_getT ...@@ -143,7 +143,7 @@ func (api *GoShimmerAPI) GetTransactions(txHashes trinary.Hashes) ([]webapi_getT
return nil, err return nil, err
} }
res, err := api.Post(fmt.Sprintf("%s/%s", api.node, routeGetTransactions), contentTypeJSON, bytes.NewReader(reqBytes)) res, err := api.httpClient.Post(fmt.Sprintf("%s/%s", api.node, routeGetTransactions), contentTypeJSON, bytes.NewReader(reqBytes))
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -168,7 +168,7 @@ func (api *GoShimmerAPI) FindTransactions(query *webapi_findTransactions.Request ...@@ -168,7 +168,7 @@ func (api *GoShimmerAPI) FindTransactions(query *webapi_findTransactions.Request
return nil, err return nil, err
} }
res, err := api.Post(fmt.Sprintf("%s/%s", api.node, routeFindTransactions), contentTypeJSON, bytes.NewReader(reqBytes)) res, err := api.httpClient.Post(fmt.Sprintf("%s/%s", api.node, routeFindTransactions), contentTypeJSON, bytes.NewReader(reqBytes))
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -181,8 +181,9 @@ func (api *GoShimmerAPI) FindTransactions(query *webapi_findTransactions.Request ...@@ -181,8 +181,9 @@ func (api *GoShimmerAPI) FindTransactions(query *webapi_findTransactions.Request
return resObj.Transactions, nil return resObj.Transactions, nil
} }
func (api *GoShimmerAPI) GetNeighbors() (*webapi_getNeighbors.Response, error) { func (api *GoShimmerAPI) GetNeighbors() (*webapi_getNeighbors.Response, error) {
res, err := api.Get(fmt.Sprintf("%s/%s", api.node, routeGetNeighbors)) res, err := api.httpClient.Get(fmt.Sprintf("%s/%s", api.node, routeGetNeighbors))
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -195,8 +196,9 @@ func (api *GoShimmerAPI) GetNeighbors() (*webapi_getNeighbors.Response, error) { ...@@ -195,8 +196,9 @@ func (api *GoShimmerAPI) GetNeighbors() (*webapi_getNeighbors.Response, error) {
return resObj, nil return resObj, nil
} }
func (api *GoShimmerAPI) GetTips() (*webapi_gtta.Response, error) { func (api *GoShimmerAPI) GetTips() (*webapi_gtta.Response, error) {
res, err := api.Get(fmt.Sprintf("%s/%s", api.node, routeGetTransactionsToApprove)) res, err := api.httpClient.Get(fmt.Sprintf("%s/%s", api.node, routeGetTransactionsToApprove))
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -210,7 +212,7 @@ func (api *GoShimmerAPI) GetTips() (*webapi_gtta.Response, error) { ...@@ -210,7 +212,7 @@ func (api *GoShimmerAPI) GetTips() (*webapi_gtta.Response, error) {
} }
func (api *GoShimmerAPI) ToggleSpammer(enable bool) (*webapi_spammer.Response, error) { func (api *GoShimmerAPI) ToggleSpammer(enable bool) (*webapi_spammer.Response, error) {
res, err := api.Get(fmt.Sprintf("%s/%s?cmd=%s", api.node, routeSpammer, func() string { res, err := api.httpClient.Get(fmt.Sprintf("%s/%s?cmd=%s", api.node, routeSpammer, func() string {
if enable { if enable {
return "start" return "start"
} }
......
...@@ -181,7 +181,9 @@ func processMetaTransaction(metaTransaction *meta_transaction.MetaTransaction) { ...@@ -181,7 +181,9 @@ func processMetaTransaction(metaTransaction *meta_transaction.MetaTransaction) {
if tx, err := GetTransaction(metaTransaction.GetHash(), func(transactionHash trinary.Trytes) *value_transaction.ValueTransaction { if tx, err := GetTransaction(metaTransaction.GetHash(), func(transactionHash trinary.Trytes) *value_transaction.ValueTransaction {
newTransaction = true newTransaction = true
return value_transaction.FromMetaTransaction(metaTransaction) tx := value_transaction.FromMetaTransaction(metaTransaction)
tx.SetModified(true)
return tx
}); err != nil { }); err != nil {
log.Errorf("Unable to load transaction %s: %s", metaTransaction.GetHash(), err.Error()) log.Errorf("Unable to load transaction %s: %s", metaTransaction.GetHash(), err.Error())
} else if newTransaction { } else if newTransaction {
......
...@@ -16,7 +16,7 @@ var log *logger.Logger ...@@ -16,7 +16,7 @@ var log *logger.Logger
func configure(plugin *node.Plugin) { func configure(plugin *node.Plugin) {
log = logger.NewLogger("API-findTransactions") log = logger.NewLogger("API-findTransactions")
webapi.Server.GET("findTransactions", findTransactions) webapi.Server.POST("findTransactions", findTransactions)
} }
// findTransactions returns the array of transaction hashes for the // findTransactions returns the array of transaction hashes for the
...@@ -24,7 +24,6 @@ func configure(plugin *node.Plugin) { ...@@ -24,7 +24,6 @@ func configure(plugin *node.Plugin) {
// If a node doesn't have any transaction hash for a given address in its ledger, // If a node doesn't have any transaction hash for a given address in its ledger,
// the value at the index of that address is empty. // the value at the index of that address is empty.
func findTransactions(c echo.Context) error { func findTransactions(c echo.Context) error {
var request Request var request Request
if err := c.Bind(&request); err != nil { if err := c.Bind(&request); err != nil {
......
...@@ -16,7 +16,7 @@ var log *logger.Logger ...@@ -16,7 +16,7 @@ var log *logger.Logger
func configure(plugin *node.Plugin) { func configure(plugin *node.Plugin) {
log = logger.NewLogger("API-getTransactions") log = logger.NewLogger("API-getTransactions")
webapi.Server.GET("getTransactions", getTransactions) webapi.Server.POST("getTransactions", getTransactions)
} }
// getTransactions returns the array of transactions for the // getTransactions returns the array of transactions for the
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment