Skip to content

Commit

Permalink
Merge pull request #6 from larksuite/name_fix
Browse files Browse the repository at this point in the history
rename
  • Loading branch information
zhaoche27 committed Dec 8, 2020
2 parents 742c062 + 70a4cb4 commit 63b9a6d
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,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]",
Expand All @@ -113,7 +113,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())
Expand Down Expand Up @@ -164,7 +164,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{})
Expand Down
6 changes: 3 additions & 3 deletions README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,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]",
Expand All @@ -118,7 +118,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())
Expand Down Expand Up @@ -169,7 +169,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{})
Expand Down
6 changes: 6 additions & 0 deletions api/core/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down
13 changes: 9 additions & 4 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{} {
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions sample/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
)
Expand Down Expand Up @@ -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())
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion sample/event/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down
2 changes: 1 addition & 1 deletion sample/event/gin2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down
2 changes: 1 addition & 1 deletion sample/event/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 63b9a6d

Please sign in to comment.