基于resty,参考 feign 实现的一个通过注释自动生成golang http client实现的工具; 开发者只需要定义接口,增加必要的注释,restyless就可以生成请求的模版代码;
- 目前注释中,只解析host,url; 且函数注释优先级高于接口注释;
- 参数类型为types.QueryParam 的参数将放到 resty QueryParam中
- 参数类型为types.PathParam 的参数将放到 resty PathParam中
- 参数类型为types.HeaderParam 的参数将放到 resty Header中
- 请求方法不是GET 且 参数类型为types.BodyParam 或者 不为 int,float,string,bool 的参数 将放到 resty body中
- 函数名称是
Get
开头的,将调用 restyGet
- 函数名是
Post
开头的,将调用Post
- 函数名
Put
开头的,调用Put
- 函数名
Del
开头的,将调用Delete
- 安装
go get github.com/wanglihui/restyless
- 定义http client interface
// UserProvider
// host=http://www.baidu.com
// params named header will put into http.header, params named query will put into querystring
// body will put into body
//
//go:generate go run github.com/wanglihui/restyless
type UserProvider interface {
//GetUser
//host=http://www.baidu.com,url=/id
GetUser(ctx context.Context, userID types.QueryParam) (User, error)
//PostUser
//host=http://www.dixincaigang.cn,url=/user/
PostUser(ctx context.Context, user User) (User, error)
//DeleteUser
//url=/user/{userID}
DeleteUser(ctx context.Context, userID types.PathParam) error
//PutUser
//url=/user/{userID}
PutUser(ctx context.Context, userID types.PathParam, user User) (User, error)
//PostUser2
//url=/user2
PostUser2(ctx context.Context, uid types.HeaderParam, token types.HeaderParam, user User) (User, error)
}
- 执行 go generate ./... 生成实现
// Code generated by file example.go and line 23 DO NOT EDIT.
// For more detail see https://github.com/wanglihui/restyless
package example
import (
"context"
_ "fmt"
"github.com/wanglihui/restyless/types"
"github.com/go-resty/resty/v2"
"gitlab.yc345.tv/teacher/golang-base/httperror"
)
func NewUserProviderImpl(r *resty.Client) UserProvider {
return &UserProviderImpl{
r : r,
}
}
type UserProviderImpl struct {
r *resty.Client
}
func (it *UserProviderImpl) GetUser (ctx context.Context,userID types.QueryParam) (User,error) {
var e httperror.HTTPError
r := it.r.R().SetError(e)
r=r.SetQueryParam("userID", string(userID))
var ret User
r = r.SetResult(&ret)
_, err := r.Get("http://www.baidu.com/id")
return ret, err
}
func (it *UserProviderImpl) PostUser (ctx context.Context,user User) (User,error) {
var e httperror.HTTPError
r := it.r.R().SetError(e)
r=r.SetBody(user)
var ret User
r = r.SetResult(&ret)
_, err := r.Post("http://www.dixincaigang.cn/user/")
return ret, err
}
func (it *UserProviderImpl) DeleteUser (ctx context.Context,userID types.PathParam) (error) {
var e httperror.HTTPError
r := it.r.R().SetError(e)
r=r.SetPathParam("userID", string(userID))
r=r.SetBody(ctx)
_, err := r.Delete("http://www.baidu.com/user/{userID}")
return err
}
func (it *UserProviderImpl) PutUser (ctx context.Context,userID types.PathParam,user User) (User,error) {
var e httperror.HTTPError
r := it.r.R().SetError(e)
r=r.SetPathParam("userID", string(userID))
r=r.SetBody(user)
var ret User
r = r.SetResult(&ret)
_, err := r.Put("http://www.baidu.com/user/{userID}")
return ret, err
}
func (it *UserProviderImpl) PostUser2 (ctx context.Context,uid types.HeaderParam,token types.HeaderParam,user User) (User,error) {
var e httperror.HTTPError
r := it.r.R().SetError(e)
r = r.SetHeader("uid", string(uid))
r = r.SetHeader("token", string(token))
r=r.SetBody(user)
var ret User
r = r.SetResult(&ret)
_, err := r.Post("http://www.baidu.com/user2")
return ret, err
}