mirror of
https://github.com/eosswedenorg/antelope-api-healthcheck
synced 2026-06-16 04:44:55 +02:00
Adding src/parse_request.go
This commit is contained in:
parent
5d27bf0ad2
commit
da8a53aa3c
2 changed files with 165 additions and 0 deletions
66
src/parse_request.go
Normal file
66
src/parse_request.go
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"github.com/eosswedenorg/eosio-api-healthcheck/src/api"
|
||||
)
|
||||
|
||||
type arguments struct {
|
||||
url string
|
||||
host string
|
||||
num_blocks int
|
||||
}
|
||||
|
||||
func ParseArguments(args []string) arguments {
|
||||
|
||||
a := arguments{
|
||||
num_blocks: 10,
|
||||
}
|
||||
|
||||
// 1. url (scheme + ip/domain + port)
|
||||
a.url = args[0]
|
||||
|
||||
// 2. num blocks
|
||||
if len(args) > 1 {
|
||||
num, err := strconv.ParseInt(args[1], 10, 32)
|
||||
if err == nil {
|
||||
a.num_blocks = int(num)
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Host
|
||||
if len(args) > 2 {
|
||||
a.host = args[2]
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
func ParseRequest(request string) (api.ApiInterface, error) {
|
||||
|
||||
// Parse arguments.
|
||||
// -------------------
|
||||
p := strings.Split(strings.TrimSpace(request), "|")
|
||||
|
||||
if len(p) < 2 {
|
||||
return nil, fmt.Errorf("Invalid number of parameters in agent request")
|
||||
}
|
||||
|
||||
a := ParseArguments(p[1:])
|
||||
|
||||
switch p[0] {
|
||||
case "v1":
|
||||
return api.NewEosioV1(a.url, a.host, float64(a.num_blocks / 2)), nil
|
||||
case "v2":
|
||||
return api.NewEosioV2(a.url, a.host, int64(a.num_blocks)), nil
|
||||
case "contract":
|
||||
return api.NewEosioContract(a.url, float64(a.num_blocks / 2)), nil
|
||||
case "test":
|
||||
return api.NewTestApi(a.url), nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("Invalid API '%s'", p[0])
|
||||
}
|
||||
99
src/parse_request_test.go
Normal file
99
src/parse_request_test.go
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
|
||||
package main
|
||||
|
||||
import (
|
||||
// "fmt"
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/eosswedenorg/eosio-api-healthcheck/src/api"
|
||||
)
|
||||
|
||||
func TestParseWithInvalidParams(t *testing.T) {
|
||||
|
||||
api, err := ParseRequest("v1")
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, err.Error(), "Invalid number of parameters in agent request")
|
||||
assert.Nil(t, api)
|
||||
}
|
||||
|
||||
// EosioV1
|
||||
// --------------------------------
|
||||
|
||||
func TestParseEosioV1(t *testing.T) {
|
||||
|
||||
expected := api.NewEosioV1("http://api.example.com", "", 5)
|
||||
|
||||
api, err := ParseRequest("v1|http://api.example.com")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, api)
|
||||
}
|
||||
|
||||
func TestParseEosioV1WithBlockNumber(t *testing.T) {
|
||||
|
||||
expected := api.NewEosioV1("http://api.example.com", "", 1000)
|
||||
|
||||
api, err := ParseRequest("v1|http://api.example.com|2000")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, api)
|
||||
}
|
||||
|
||||
|
||||
func TestParseEosioV1Full(t *testing.T) {
|
||||
|
||||
expected := api.NewEosioV1("http://api.example.com", "http://host.example.com", 500)
|
||||
|
||||
api, err := ParseRequest("v1|http://api.example.com|1000|http://host.example.com")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, api)
|
||||
}
|
||||
|
||||
// EosioV2
|
||||
// --------------------------------
|
||||
|
||||
func TestParseEosioV2(t *testing.T) {
|
||||
|
||||
expected := api.NewEosioV2("http://api.v2.example.com", "", 10)
|
||||
|
||||
api, err := ParseRequest("v2|http://api.v2.example.com")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, api)
|
||||
}
|
||||
|
||||
func TestParseEosioV2WithOffset(t *testing.T) {
|
||||
|
||||
expected := api.NewEosioV2("http://api.v2.example.com", "", 1000)
|
||||
|
||||
api, err := ParseRequest("v2|http://api.v2.example.com|1000")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, api)
|
||||
}
|
||||
|
||||
func TestParseEosioV2Full(t *testing.T) {
|
||||
|
||||
expected := api.NewEosioV2("http://api.v2.example.com", "http://host.example.com", 1000)
|
||||
|
||||
api, err := ParseRequest("v2|http://api.v2.example.com|1000|http://host.example.com")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, api)
|
||||
}
|
||||
|
||||
// EosioContract
|
||||
// --------------------------------
|
||||
|
||||
func TestParseEosioContract(t *testing.T) {
|
||||
|
||||
expected := api.NewEosioContract("http://api.contract.example.com", 5)
|
||||
|
||||
api, err := ParseRequest("contract|http://api.contract.example.com")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, api)
|
||||
}
|
||||
|
||||
func TestParseEosioContractWithBlockTime(t *testing.T) {
|
||||
|
||||
expected := api.NewEosioContract("http://api.contract.example.com", 256)
|
||||
|
||||
api, err := ParseRequest("contract|http://api.contract.example.com|512")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, api)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue