inspired by python-requests
go get -u https://github.com/guestin/go-requests@latest
we have some bean definations...
// git tag
type GitTag struct {
Ref string `json:"ref" validate:"required"`
NodeId string `json:"node_id" validate:"required"`
TagUrl string `json:"url validate:"url"`
Object SHAObject `json:"object" validate:"required"`
}
// SHA object
type SHAObject struct {
Sha string `json:"sha" validate:"required"`
Type string `json:"type" validate:"required"`
Url string `json:"url" validate:"url"`
}
func TestGet(t *testing.T) {
data, err := Get(context.TODO(),
"https://api.github.com",
opt.BuildUrl(murl.WithPath("repos/guestin/mob/git/refs/tags")),
opt.ExpectStatusCode(http.StatusOK))
assert.NoError(t, err)
t.Log(data)
}
work with go-playground/validator.v10 pretty good
func TestValidateResponse(t *testing.T) {
validator, err := mvalidate.NewValidator("zh")
assert.NoError(t, err)
data, err := Get(context.TODO(),
"https://api.github.com",
opt.BuildUrl(murl.WithPath("repos/guestin/mob/git/refs/tags")),
opt.ValidateVar(validator, `required,dive,required`),
opt.ExpectStatusCode(http.StatusOK),
opt.BindJSON(&[]GitTag{}))
if err != nil {
t.Log(err)
t.FailNow()
return
}
versionData := data.(*[]GitTag)
t.Logf("%v\n", versionData)
}
// define scope: RemoteGitAPI
var RemoteGitAPI = NewScope(
opt.Url("https://api.github.com"))
func TestGetScope(t *testing.T) {
requestScope := RemoteGitAPI.With(
opt.HttpMethod(http.MethodGet),
opt.BuildUrl(murl.WithPath("repos/guestin/mob/git/refs/tags")),
opt.BindJSON(&[]GitTag{}))
data, err := requestScope.Execute()
assert.NoError(t, err)
t.Log(data)
}
go-requests
have a lot of options, just try it yourself! 😎
Released under the MIT License