feat_: locally handled methods package base structure (#5418)

This commit is contained in:
Godfrain Jacques 2024-07-12 11:12:14 -07:00 committed by GitHub
parent 33c2f231b0
commit 194d9444a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 71 additions and 13 deletions

View File

@ -1,15 +1,42 @@
package connector
func NewAPI(s *Service) *API {
return &API{
s: s,
}
}
import (
"encoding/json"
"fmt"
)
type API struct {
s *Service
r *CommandRegistry
}
func NewAPI(s *Service) *API {
r := NewCommandRegistry()
return &API{
s: s,
r: r,
}
}
type RPCRequest struct {
JSONRPC string `json:"jsonrpc"`
ID int `json:"id"`
Method string `json:"method"`
Params []interface{} `json:"params"`
}
func (api *API) CallRPC(inputJSON string) (string, error) {
var request RPCRequest
err := json.Unmarshal([]byte(inputJSON), &request)
if err != nil {
return "", fmt.Errorf("error unmarshalling JSON: %v", err)
}
if command, exists := api.r.GetCommand(request.Method); exists {
return command.Execute(inputJSON)
}
return api.s.rpcClient.CallRaw(inputJSON), nil
}

View File

@ -40,9 +40,7 @@ func setupTestAPI(t *testing.T) (*API, func()) {
service := NewService(rpcClient, nil)
return &API{
s: service,
}, cancel
return NewAPI(service), cancel
}
func TestCallRPC(t *testing.T) {
@ -51,21 +49,25 @@ func TestCallRPC(t *testing.T) {
tests := []struct {
request string
expectError bool
expectedContains string
notContains bool
}{
{
request: "{\"method\": \"eth_blockNumber\", \"params\": []}",
expectError: false,
expectedContains: "does not exist/is not available",
notContains: true,
},
{
request: "{\"method\": \"eth_blockNumbers\", \"params\": []}",
expectError: false,
expectedContains: "does not exist/is not available",
notContains: false,
},
{
request: "",
expectError: true,
expectedContains: "does not exist/is not available",
notContains: true,
},
@ -74,12 +76,16 @@ func TestCallRPC(t *testing.T) {
for _, tt := range tests {
t.Run(tt.request, func(t *testing.T) {
response, err := api.CallRPC(tt.request)
require.NoError(t, err)
require.NotEmpty(t, response)
if tt.notContains {
require.NotContains(t, response, tt.expectedContains)
if tt.expectError {
require.Error(t, err)
} else {
require.Contains(t, response, tt.expectedContains)
require.NoError(t, err)
require.NotEmpty(t, response)
if tt.notContains {
require.NotContains(t, response, tt.expectedContains)
} else {
require.Contains(t, response, tt.expectedContains)
}
}
})
}

View File

@ -0,0 +1,20 @@
package connector
type CommandRegistry struct {
commands map[string]RPCCommand
}
func NewCommandRegistry() *CommandRegistry {
return &CommandRegistry{
commands: make(map[string]RPCCommand),
}
}
func (r *CommandRegistry) Register(method string, command RPCCommand) {
r.commands[method] = command
}
func (r *CommandRegistry) GetCommand(method string) (RPCCommand, bool) {
command, exists := r.commands[method]
return command, exists
}

View File

@ -0,0 +1,5 @@
package connector
type RPCCommand interface {
Execute(inputJSON string) (string, error)
}