mirror of
https://github.com/eosswedenorg/antelope-api-healthcheck
synced 2026-07-04 12:03:43 +02:00
Merge branch 'api-refactor' into dev
This commit is contained in:
commit
cb8eec6ff6
10 changed files with 108 additions and 21 deletions
|
|
@ -25,6 +25,10 @@ func parseResponse(resp string) (agentcheck.Response, error) {
|
||||||
return agentcheck.NewStatusResponse(rtype), nil
|
return agentcheck.NewStatusResponse(rtype), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DebugApiFactory(args ApiArguments) ApiInterface {
|
||||||
|
return NewDebugApi(args.Url)
|
||||||
|
}
|
||||||
|
|
||||||
func NewDebugApi(response string) DebugApi {
|
func NewDebugApi(response string) DebugApi {
|
||||||
|
|
||||||
resp, _ := parseResponse(response)
|
resp, _ := parseResponse(response)
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,18 @@ import (
|
||||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestDebugApiFactory(t *testing.T) {
|
||||||
|
|
||||||
|
api := DebugApiFactory(ApiArguments{
|
||||||
|
Url: "up",
|
||||||
|
Host: "host",
|
||||||
|
NumBlocks: 40,
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.IsType(t, DebugApi{}, api)
|
||||||
|
assert.Equal(t, api.(DebugApi).response, agentcheck.NewStatusResponse(agentcheck.Up))
|
||||||
|
}
|
||||||
|
|
||||||
func TestNewDebugApi(t *testing.T) {
|
func TestNewDebugApi(t *testing.T) {
|
||||||
type args struct {
|
type args struct {
|
||||||
response string
|
response string
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,10 @@ type EosioContract struct {
|
||||||
block_time float64
|
block_time float64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func EosioContractFactory(args ApiArguments) ApiInterface {
|
||||||
|
return NewEosioContract(args.Url, float64(args.NumBlocks / 2))
|
||||||
|
}
|
||||||
|
|
||||||
func NewEosioContract(url string, block_time float64) EosioContract {
|
func NewEosioContract(url string, block_time float64) EosioContract {
|
||||||
return EosioContract{
|
return EosioContract{
|
||||||
client: contract_api.Client{
|
client: contract_api.Client{
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,21 @@ import (
|
||||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestEosioContractFactory(t *testing.T) {
|
||||||
|
|
||||||
|
api := EosioContractFactory(ApiArguments{
|
||||||
|
Url: "https://atomic.example.com",
|
||||||
|
NumBlocks: 120,
|
||||||
|
})
|
||||||
|
|
||||||
|
expected := NewEosioContract("https://atomic.example.com", 60)
|
||||||
|
|
||||||
|
assert.IsType(t, expected, api)
|
||||||
|
assert.Equal(t, expected.client.Url, api.(EosioContract).client.Url)
|
||||||
|
assert.Equal(t, expected.client.Host, api.(EosioContract).client.Host)
|
||||||
|
assert.Equal(t, expected.block_time, api.(EosioContract).block_time)
|
||||||
|
}
|
||||||
|
|
||||||
func TestEosioContractLogInfo(t *testing.T) {
|
func TestEosioContractLogInfo(t *testing.T) {
|
||||||
|
|
||||||
api := NewEosioContract("https://atomic.example.com", 120)
|
api := NewEosioContract("https://atomic.example.com", 120)
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,10 @@ type EosioV1 struct {
|
||||||
block_time float64
|
block_time float64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func EosioV1Factory(args ApiArguments) ApiInterface {
|
||||||
|
return NewEosioV1(args.Url, args.Host, float64(args.NumBlocks / 2))
|
||||||
|
}
|
||||||
|
|
||||||
func NewEosioV1(url string, host string, block_time float64) EosioV1 {
|
func NewEosioV1(url string, host string, block_time float64) EosioV1 {
|
||||||
|
|
||||||
api := EosioV1{
|
api := EosioV1{
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,22 @@ import (
|
||||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestEosioV1Factory(t *testing.T) {
|
||||||
|
|
||||||
|
api := EosioV1Factory(ApiArguments{
|
||||||
|
Url: "https://api.v1.example.com",
|
||||||
|
Host: "host.example.com",
|
||||||
|
NumBlocks: 120,
|
||||||
|
})
|
||||||
|
|
||||||
|
expected := NewEosioV1("https://api.v1.example.com", "host.example.com", 60)
|
||||||
|
|
||||||
|
assert.IsType(t, expected, api)
|
||||||
|
assert.Equal(t, expected.client.Url, api.(EosioV1).client.Url)
|
||||||
|
assert.Equal(t, expected.client.Host, api.(EosioV1).client.Host)
|
||||||
|
assert.Equal(t, expected.block_time, api.(EosioV1).block_time)
|
||||||
|
}
|
||||||
|
|
||||||
func TestEosioV1LogInfo(t *testing.T) {
|
func TestEosioV1LogInfo(t *testing.T) {
|
||||||
|
|
||||||
api := NewEosioV1("https://api.v1.example.com", "host.example.com", 120)
|
api := NewEosioV1("https://api.v1.example.com", "host.example.com", 120)
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,10 @@ type EosioV2 struct {
|
||||||
offset int64
|
offset int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func EosioV2Factory(args ApiArguments) ApiInterface {
|
||||||
|
return NewEosioV2(args.Url, args.Host, int64(args.NumBlocks))
|
||||||
|
}
|
||||||
|
|
||||||
func NewEosioV2(url string, host string, offset int64) EosioV2 {
|
func NewEosioV2(url string, host string, offset int64) EosioV2 {
|
||||||
|
|
||||||
api := EosioV2{
|
api := EosioV2{
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,22 @@ import (
|
||||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestEosioV2Factory(t *testing.T) {
|
||||||
|
|
||||||
|
api := EosioV2Factory(ApiArguments{
|
||||||
|
Url: "https://api.v2.example.com",
|
||||||
|
Host: "host.example.com",
|
||||||
|
NumBlocks: 120,
|
||||||
|
})
|
||||||
|
|
||||||
|
expected := NewEosioV2("https://api.v2.example.com", "host.example.com", 120)
|
||||||
|
|
||||||
|
assert.IsType(t, expected, api)
|
||||||
|
assert.Equal(t, expected.client.Url, api.(EosioV2).client.Url)
|
||||||
|
assert.Equal(t, expected.client.Host, api.(EosioV2).client.Host)
|
||||||
|
assert.Equal(t, expected.offset, api.(EosioV2).offset)
|
||||||
|
}
|
||||||
|
|
||||||
func TestEosioV2LogInfo(t *testing.T) {
|
func TestEosioV2LogInfo(t *testing.T) {
|
||||||
|
|
||||||
api := NewEosioV2("https://api.v2.example.com", "host.example.com", 120)
|
api := NewEosioV2("https://api.v2.example.com", "host.example.com", 120)
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,24 @@ import (
|
||||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generic struct that is passed to factory functions
|
||||||
|
* to configure the API request.
|
||||||
|
*/
|
||||||
|
type ApiArguments struct {
|
||||||
|
Url string
|
||||||
|
Host string
|
||||||
|
NumBlocks int
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory function
|
||||||
|
*
|
||||||
|
* Each API must implement this function and process `args`
|
||||||
|
* returing a instance of it's implementation of the ApiInterface
|
||||||
|
*/
|
||||||
|
type Factory func(args ApiArguments) ApiInterface
|
||||||
|
|
||||||
type ApiInterface interface {
|
type ApiInterface interface {
|
||||||
|
|
||||||
// Returns Logging information
|
// Returns Logging information
|
||||||
|
|
|
||||||
|
|
@ -8,32 +8,26 @@ import (
|
||||||
"github.com/eosswedenorg/eosio-api-healthcheck/src/api"
|
"github.com/eosswedenorg/eosio-api-healthcheck/src/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
type arguments struct {
|
func ParseArguments(args []string) api.ApiArguments {
|
||||||
url string
|
|
||||||
host string
|
|
||||||
num_blocks int
|
|
||||||
}
|
|
||||||
|
|
||||||
func ParseArguments(args []string) arguments {
|
a := api.ApiArguments{
|
||||||
|
NumBlocks: 10,
|
||||||
a := arguments{
|
|
||||||
num_blocks: 10,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. url (scheme + ip/domain + port)
|
// 1. url (scheme + ip/domain + port)
|
||||||
a.url = args[0]
|
a.Url = args[0]
|
||||||
|
|
||||||
// 2. num blocks
|
// 2. num blocks
|
||||||
if len(args) > 1 {
|
if len(args) > 1 {
|
||||||
num, err := strconv.ParseInt(args[1], 10, 32)
|
num, err := strconv.ParseInt(args[1], 10, 32)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
a.num_blocks = int(num)
|
a.NumBlocks = int(num)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Host
|
// 3. Host
|
||||||
if len(args) > 2 {
|
if len(args) > 2 {
|
||||||
a.host = args[2]
|
a.Host = args[2]
|
||||||
}
|
}
|
||||||
|
|
||||||
return a
|
return a
|
||||||
|
|
@ -41,6 +35,13 @@ func ParseArguments(args []string) arguments {
|
||||||
|
|
||||||
func ParseRequest(request string) (api.ApiInterface, error) {
|
func ParseRequest(request string) (api.ApiInterface, error) {
|
||||||
|
|
||||||
|
factories := map[string]api.Factory{
|
||||||
|
"v1": api.EosioV1Factory,
|
||||||
|
"v2": api.EosioV2Factory,
|
||||||
|
"contract": api.EosioContractFactory,
|
||||||
|
"debug": api.DebugApiFactory,
|
||||||
|
}
|
||||||
|
|
||||||
// Parse arguments.
|
// Parse arguments.
|
||||||
// -------------------
|
// -------------------
|
||||||
p := strings.Split(strings.TrimSpace(request), "|")
|
p := strings.Split(strings.TrimSpace(request), "|")
|
||||||
|
|
@ -51,15 +52,8 @@ func ParseRequest(request string) (api.ApiInterface, error) {
|
||||||
|
|
||||||
a := ParseArguments(p[1:])
|
a := ParseArguments(p[1:])
|
||||||
|
|
||||||
switch p[0] {
|
if factory, ok := factories[p[0]]; ok {
|
||||||
case "v1":
|
return factory(a), nil
|
||||||
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 "debug":
|
|
||||||
return api.NewDebugApi(a.url), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, fmt.Errorf("invalid API '%s'", p[0])
|
return nil, fmt.Errorf("invalid API '%s'", p[0])
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue