mirror of
https://github.com/eosswedenorg/antelope-api-healthcheck
synced 2026-06-18 05:00:03 +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])
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue