diff --git a/README.md b/README.md index 3a77cf89..eafbc18d 100644 --- a/README.md +++ b/README.md @@ -102,7 +102,7 @@ $ go get -u github.com/larksuite/oapi-sdk-go // request.SetNotDataField(), set whether the response does not have a `data` field, business interfaces all have `data `Field, so you don’t need to set // request.SetTenantKey("TenantKey"), as an `app store application`, it means using `tenant_access_token` to access the API, you need to set // request.SetUserAccessToken("UserAccessToken"), which means using` user_access_token` To access the API, you need to set - // req := request.NewRequest2(httpPath:string, httpMethod:string, accessTokenType:AccessTokenType, input:interface, output:interface, ... optFns:OptFn [))) + // req := request.NewRequestWithNative(httpPath:string, httpMethod:string, accessTokenType:AccessTokenType, input:interface, output:interface, ... optFns:OptFn [))) // Example: body := map[string]interface{}{ "open_id": "[open_id]", @@ -112,7 +112,7 @@ $ go get -u github.com/larksuite/oapi-sdk-go }, } ret := make(map[string]interface{}) - req := request.NewRequest2("message/v4/send", "POST", request.AccessTokenTypeTenant, body, &ret) + req := request.NewRequestWithNative("message/v4/send", "POST", request.AccessTokenTypeTenant, body, &ret) coreCtx := core.WarpContext(context.Background()) err := api.Send(coreCtx, conf, req) fmt.Println(coreCtx.GetRequestID()) @@ -163,7 +163,7 @@ $ go get -u github.com/larksuite/oapi-sdk-go // event: map[string]interface{} // return: // error: not nil, response status code 500 - event.SetTypeHandler2(conf, "app_status_change", func(coreCtx *core.Context, event map[string]interface{}) error { + event.SetTypeCallback(conf, "app_status_change", func(coreCtx *core.Context, event map[string]interface{}) error { fmt.Println(coreCtx.GetRequestID()) fmt.Println(tools.Prettify(event)) data := event["event"].(map[string]interface{}) diff --git a/README.zh.md b/README.zh.md index 16492792..491f1799 100644 --- a/README.zh.md +++ b/README.zh.md @@ -107,7 +107,7 @@ $ go get -u github.com/larksuite/oapi-sdk-go // request.SetNotDataField(),设置响应的是否 没有`data`字段,业务接口都是有`data`字段,所以不需要设置 // request.SetTenantKey("TenantKey"),以`应用商店应用`身份,表示使用`tenant_access_token`访问API,需要设置 // request.SetUserAccessToken("UserAccessToken"),表示使用`user_access_token`访问API,需要设置 - // req := request.NewRequest2(httpPath:string, httpMethod:string, accessTokenType:AccessTokenType, input:interface, output:interface, ... optFns:OptFn [))) + // req := request.NewRequestWithNative(httpPath:string, httpMethod:string, accessTokenType:AccessTokenType, input:interface, output:interface, ... optFns:OptFn [))) // Example: body := map[string]interface{}{ "open_id": "[open_id]", @@ -117,7 +117,7 @@ $ go get -u github.com/larksuite/oapi-sdk-go }, } ret := make(map[string]interface{}) - req := request.NewRequest2("message/v4/send", "POST", request.AccessTokenTypeTenant, body, &ret)coreCtx := core.WarpContext(context.Background()) + req := request.NewRequestWithNative("message/v4/send", "POST", request.AccessTokenTypeTenant, body, &ret)coreCtx := core.WarpContext(context.Background()) err := api.Send(coreCtx, conf, req) fmt.Println(coreCtx.GetRequestID()) fmt.Println(coreCtx.GetHTTPStatusCode()) @@ -168,7 +168,7 @@ $ go get -u github.com/larksuite/oapi-sdk-go // event: 事件数据 // return: // error: 不为nil,响应状态码 500 - event.SetTypeHandler2(conf, "app_status_change", func(coreCtx *core.Context, event map[string]interface{}) error { + event.SetTypeCallback(conf, "app_status_change", func(coreCtx *core.Context, event map[string]interface{}) error { fmt.Println(coreCtx.GetRequestID()) fmt.Println(tools.Prettify(event)) data := event["event"].(map[string]interface{}) diff --git a/api/core/request/request.go b/api/core/request/request.go index 62ea1703..a75f22f9 100644 --- a/api/core/request/request.go +++ b/api/core/request/request.go @@ -150,11 +150,17 @@ func NewRequestByAuth(httpPath, httpMethod string, input, output interface{}) *R } } +// Deprecated, please use `NewRequestWithNative` func NewRequest2(httpPath, httpMethod string, accessTokenType AccessTokenType, input interface{}, output interface{}, optFns ...OptFn) *Request { return NewRequest(httpPath, httpMethod, []AccessTokenType{accessTokenType}, input, output, optFns...) } +func NewRequestWithNative(httpPath, httpMethod string, accessTokenType AccessTokenType, + input interface{}, output interface{}, optFns ...OptFn) *Request { + return NewRequest(httpPath, httpMethod, []AccessTokenType{accessTokenType}, input, output, optFns...) +} + func NewRequest(httpPath, httpMethod string, accessTokenTypes []AccessTokenType, input interface{}, output interface{}, optFns ...OptFn) *Request { accessibleTokenTypeSet := make(map[AccessTokenType]struct{}) diff --git a/event/event.go b/event/event.go index 636f0cb5..24ca803b 100644 --- a/event/event.go +++ b/event/event.go @@ -16,12 +16,17 @@ func SetTypeHandler(conf *config.Config, eventType string, handler handlers.Hand handlers.SetTypeHandler(conf, eventType, handler) } -func SetTypeHandler2(conf *config.Config, eventType string, fn func(ctx *core.Context, event map[string]interface{}) error) { - SetTypeHandler(conf, eventType, &defaultHandler{fn: fn}) +// Deprecated, please use `SetTypeCallback` +func SetTypeHandler2(conf *config.Config, eventType string, callback func(ctx *core.Context, event map[string]interface{}) error) { + SetTypeHandler(conf, eventType, &defaultHandler{callback: callback}) +} + +func SetTypeCallback(conf *config.Config, eventType string, callback func(ctx *core.Context, event map[string]interface{}) error) { + SetTypeHandler(conf, eventType, &defaultHandler{callback: callback}) } type defaultHandler struct { - fn func(ctx *core.Context, event map[string]interface{}) error + callback func(ctx *core.Context, event map[string]interface{}) error } func (h *defaultHandler) GetEvent() interface{} { @@ -31,7 +36,7 @@ func (h *defaultHandler) GetEvent() interface{} { func (h *defaultHandler) Handle(ctx *core.Context, event interface{}) error { e := event.(*map[string]interface{}) - return h.fn(ctx, *e) + return h.callback(ctx, *e) } func Handle(conf *config.Config, request *coremodel.OapiRequest) *coremodel.OapiResponse { diff --git a/sample/api/api.go b/sample/api/api.go index 1a0a31ed..47718f4e 100644 --- a/sample/api/api.go +++ b/sample/api/api.go @@ -33,7 +33,7 @@ func testSendMessage() { }, } ret := make(map[string]interface{}) - req := request.NewRequest2("message/v4/send", "POST", + req := request.NewRequestWithNative("message/v4/send", "POST", request.AccessTokenTypeTenant, body, &ret, //应用市场应用 request.SetTenantKey("TenantKey"), ) @@ -75,7 +75,7 @@ func testUploadFile() { formData.AppendFile(request.NewFile().SetContentStream(file).SetFieldName("image")) */ ret := &UploadImage{} - err = api.Send(coreCtx, conf, request.NewRequest2("image/v4/put", "POST", + err = api.Send(coreCtx, conf, request.NewRequestWithNative("image/v4/put", "POST", request.AccessTokenTypeTenant, formData, ret)) fmt.Println(coreCtx.GetRequestID()) fmt.Println(coreCtx.GetHTTPStatusCode()) @@ -101,7 +101,7 @@ func testDownloadFile() { return } */ - req := request.NewRequest2("image/v4/get", "GET", + req := request.NewRequestWithNative("image/v4/get", "GET", request.AccessTokenTypeTenant, nil, ret, request.SetQueryParams(map[string]interface{}{"image_key": "[image key]"}), request.SetResponseStream()) err := api.Send(coreCtx, conf, req) diff --git a/sample/event/gin.go b/sample/event/gin.go index 80df6b3b..f1cef289 100644 --- a/sample/event/gin.go +++ b/sample/event/gin.go @@ -31,7 +31,7 @@ func main() { return nil }) */ - event.SetTypeHandler2(conf, "app_status_change", func(ctx *core.Context, event map[string]interface{}) error { + event.SetTypeCallback(conf, "app_status_change", func(ctx *core.Context, event map[string]interface{}) error { fmt.Println(ctx.GetRequestID()) fmt.Println(tools.Prettify(event)) data := event["event"].(map[string]interface{}) diff --git a/sample/event/gin2.go b/sample/event/gin2.go index 5da4b307..19cd0b8d 100644 --- a/sample/event/gin2.go +++ b/sample/event/gin2.go @@ -31,7 +31,7 @@ func main() { return nil }) */ - event.SetTypeHandler2(conf, "app_status_change", func(ctx *core.Context, event map[string]interface{}) error { + event.SetTypeCallback(conf, "app_status_change", func(ctx *core.Context, event map[string]interface{}) error { fmt.Println(ctx.GetRequestID()) fmt.Println(tools.Prettify(event)) data := event["event"].(map[string]interface{}) diff --git a/sample/event/http_server.go b/sample/event/http_server.go index 942abe9c..ffb69b50 100644 --- a/sample/event/http_server.go +++ b/sample/event/http_server.go @@ -30,7 +30,7 @@ func main() { return nil }) - event.SetTypeHandler2(conf, "user.created_v2", func(coreCtx *core.Context, event map[string]interface{}) error { + event.SetTypeCallback(conf, "user.created_v2", func(coreCtx *core.Context, event map[string]interface{}) error { fmt.Println(coreCtx.GetRequestID()) fmt.Println(tools.Prettify(event)) return nil