mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-26 09:42:22 +00:00
oracle/neofs: allow to use filters on NeoFS responses
This commit is contained in:
parent
508a1ca9ee
commit
540c6c13a9
2 changed files with 15 additions and 12 deletions
|
@ -6,6 +6,8 @@ import (
|
|||
"unicode/utf8"
|
||||
|
||||
"github.com/PaesslerAG/jsonpath"
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
||||
)
|
||||
|
||||
func filter(value []byte, path string) ([]byte, error) {
|
||||
|
@ -22,3 +24,14 @@ func filter(value []byte, path string) ([]byte, error) {
|
|||
}
|
||||
return json.Marshal([]interface{}{result})
|
||||
}
|
||||
|
||||
func filterRequest(result []byte, req *state.OracleRequest) (transaction.OracleResponseCode, []byte) {
|
||||
if req.Filter != nil {
|
||||
var err error
|
||||
result, err = filter(result, *req.Filter)
|
||||
if err != nil {
|
||||
return transaction.Error, nil
|
||||
}
|
||||
}
|
||||
return transaction.Success, result
|
||||
}
|
||||
|
|
|
@ -117,16 +117,7 @@ func (o *Oracle) processRequest(priv *keys.PrivateKey, req request) error {
|
|||
}
|
||||
break
|
||||
}
|
||||
if req.Req.Filter != nil {
|
||||
res, err := filter(result, *req.Req.Filter)
|
||||
if err != nil {
|
||||
resp.Code = transaction.Error
|
||||
break
|
||||
}
|
||||
result = res
|
||||
}
|
||||
resp.Code = transaction.Success
|
||||
resp.Result = result
|
||||
resp.Code, resp.Result = filterRequest(result, req.Req)
|
||||
case r.StatusCode == http.StatusForbidden:
|
||||
resp.Code = transaction.Forbidden
|
||||
case r.StatusCode == http.StatusNotFound:
|
||||
|
@ -144,8 +135,7 @@ func (o *Oracle) processRequest(priv *keys.PrivateKey, req request) error {
|
|||
if err != nil {
|
||||
resp.Code = transaction.Error
|
||||
} else {
|
||||
resp.Code = transaction.Success
|
||||
resp.Result = res
|
||||
resp.Code, resp.Result = filterRequest(res, req.Req)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue