-
Notifications
You must be signed in to change notification settings - Fork 463
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: venus-shared: api version / namespaces & helper func for rpc en…
…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
Showing
18 changed files
with
258 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package v0 | ||
package gateway | ||
|
||
type IGateway interface { | ||
IProofEvent | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package v0 | ||
package gateway | ||
|
||
import ( | ||
"context" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package v0 | ||
package gateway | ||
|
||
import ( | ||
"context" | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package v0 | ||
package gateway | ||
|
||
import ( | ||
"context" | ||
|
Oops, something went wrong.