Skip to content

Commit

Permalink
feat: venus-shared: api version / namespaces & helper func for rpc en…
Browse files Browse the repository at this point in the history
…dpoint (#4782)

* feat: helper for formating endpoint with given addr & ver

* feat: add Version for api-gen

* feat: re-generate api clients with ver & nss

* refactor: rename v0 to gateway so that we could have a better api namespace
  • Loading branch information
dtynn authored Feb 18, 2022
1 parent a1df3e5 commit 500180b
Show file tree
Hide file tree
Showing 18 changed files with 258 additions and 29 deletions.
25 changes: 18 additions & 7 deletions venus-devtool/api-gen/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions venus-devtool/api-gen/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func init() {
IncludeAll: true,
},
RPCMeta: util.RPCMeta{
Version: 0,
MethodNamespace: "Message",
},
},
Expand All @@ -39,6 +40,9 @@ func init() {
ImportPath: "github.com/filecoin-project/venus/venus-shared/api/wallet",
IncludeAll: true,
},
RPCMeta: util.RPCMeta{
Version: 0,
},
},
util.APIMeta{
Type: reflect.TypeOf((*gatewayv1.IGateway)(nil)).Elem(),
Expand All @@ -47,6 +51,7 @@ func init() {
IncludeAll: true,
},
RPCMeta: util.RPCMeta{
Version: 1,
MethodNamespace: "Gateway",
},
},
Expand All @@ -57,6 +62,7 @@ func init() {
IncludeAll: true,
},
RPCMeta: util.RPCMeta{
Version: 0,
MethodNamespace: "Gateway",
},
},
Expand All @@ -67,6 +73,7 @@ func init() {
IncludeAll: true,
},
RPCMeta: util.RPCMeta{
Version: 0,
MethodNamespace: "VENUS_MARKET",
},
},
Expand All @@ -77,6 +84,7 @@ func init() {
IncludeAll: true,
},
RPCMeta: util.RPCMeta{
Version: 0,
MethodNamespace: "VENUS_MARKET_CLIENT",
},
},
Expand Down
7 changes: 7 additions & 0 deletions venus-devtool/util/api_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ var ChainAPIPairs = []struct {
ImportPath: "github.com/filecoin-project/venus/venus-shared/api/chain/v0",
IncludeAll: true,
},
RPCMeta: RPCMeta{
Version: 0,
},
},
},
{
Expand All @@ -48,13 +51,17 @@ var ChainAPIPairs = []struct {
ImportPath: "github.com/filecoin-project/venus/venus-shared/api/chain/v1",
IncludeAll: true,
},
RPCMeta: RPCMeta{
Version: 1,
},
},
},
}

var LatestChainAPIPair = ChainAPIPairs[len(ChainAPIPairs)-1]

type RPCMeta struct {
Version uint32
Namespace string
MethodNamespace string
}
Expand Down
14 changes: 12 additions & 2 deletions venus-shared/api/chain/v0/client_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions venus-shared/api/chain/v1/client_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions venus-shared/api/endpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package api

import (
"fmt"
"net/url"
)

func Endpoint(raw string, ver uint32) (string, error) {
u, err := url.Parse(raw)
if err != nil {
return "", fmt.Errorf("invalid url: %w", err)
}

if u.Scheme == "" {
return "", fmt.Errorf("scheme is required")
}

if u.Host == "" {
return "", fmt.Errorf("host is required")
}

// raw url contains more than just scheme://host(:prot)
if u.Path != "" && u.Path != "/" {
return raw, nil
}

return fmt.Sprintf("%s://%s/rpc/v%d", u.Scheme, u.Host, ver), nil
}
95 changes: 95 additions & 0 deletions venus-shared/api/endpoint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package api

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestParseHost(t *testing.T) {

cases := []struct {
raw string
ver uint32
expected string
}{
// valid
{
raw: "http://api.venus.io:1234",
ver: 1,
expected: "http://api.venus.io:1234/rpc/v1",
},
{
raw: "http://api.venus.io:1234/",
ver: 1,
expected: "http://api.venus.io:1234/rpc/v1",
},
{
raw: "http://api.venus.io:1234/rpc",
ver: 1,
expected: "http://api.venus.io:1234/rpc",
},

{
raw: "http://api.venus.io:1234/rpc/v0",
ver: 1,
expected: "http://api.venus.io:1234/rpc/v0",
},

// invalid: no scheme
{
raw: "://api.venus.io:1234",
ver: 1,
expected: "",
},
{
raw: "://api.venus.io:1234/",
ver: 1,
expected: "",
},
{
raw: "://api.venus.io:1234/rpc",
ver: 1,
expected: "",
},
{
raw: "://api.venus.io:1234/rpc/v0",
ver: 1,
expected: "",
},

// invalid: no scheme 2
{
raw: "api.venus.io:1234",
ver: 1,
expected: "",
},
{
raw: "api.venus.io:1234/",
ver: 1,
expected: "",
},
{
raw: "api.venus.io:1234/rpc",
ver: 1,
expected: "",
},
{
raw: "api.venus.io:1234/rpc/v0",
ver: 1,
expected: "",
},
}

for i := range cases {
c := cases[i]
got, err := Endpoint(c.raw, c.ver)
if c.expected == "" {
require.Errorf(t, err, "%s is expected to be invalid, got %s", c.raw, got)
continue
}

require.NoErrorf(t, err, "%s is expected to be valid", c.raw)
require.Equal(t, c.expected, got, "converted endpoint")
}
}
2 changes: 1 addition & 1 deletion venus-shared/api/gateway/v0/api.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v0
package gateway

type IGateway interface {
IProofEvent
Expand Down
16 changes: 13 additions & 3 deletions venus-shared/api/gateway/v0/client_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion venus-shared/api/gateway/v0/market_event.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v0
package gateway

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion venus-shared/api/gateway/v0/proof_event.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v0
package gateway

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion venus-shared/api/gateway/v0/proxy_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion venus-shared/api/gateway/v0/wallet_event.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v0
package gateway

import (
"context"
Expand Down
Loading

0 comments on commit 500180b

Please sign in to comment.