123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648 |
- package pass_through
- import (
- "git.sxidc.com/go-framework/baize/framework/gateway"
- "git.sxidc.com/go-tools/utils/http_client"
- "git.sxidc.com/go-tools/utils/strutils"
- "net/http"
- )
- // PostRoute POST直传API
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func PostRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodPost, opts...)
- }
- // DeleteRoute DELETE直传API
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func DeleteRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodDelete, opts...)
- }
- // PutRoute PUT直传API
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func PutRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodPut, opts...)
- }
- // GetRoute GET直传API
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func GetRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodGet, opts...)
- }
- // PostRouteWithTenantIDAndUserID POST直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和userId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func PostRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodPost, append(opts,
- WithTenantIDParamsName("tenantId"),
- WithUserIDParamsName("userId"))...)
- }
- // PostRouteWithTenantIDAndCreateUserID POST直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和createUserId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func PostRouteWithTenantIDAndCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodPost, append(opts,
- WithTenantIDParamsName("tenantId"),
- WithUserIDParamsName("createUserId"))...)
- }
- // DeleteRouteWithTenantIDAndUserID DELETE直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和userId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func DeleteRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodDelete, append(opts,
- WithTenantIDParamsName("tenantId"),
- WithUserIDParamsName("userId"))...)
- }
- // DeleteRouteWithTenantIDAndDeleteUserID DELETE直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和deleteUserId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func DeleteRouteWithTenantIDAndDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodDelete, append(opts,
- WithTenantIDParamsName("tenantId"),
- WithUserIDParamsName("deleteUserId"))...)
- }
- // PutRouteWithTenantIDAndUserID PUT直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和userId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func PutRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodPut, append(opts,
- WithTenantIDParamsName("tenantId"),
- WithUserIDParamsName("userId"))...)
- }
- // PutRouteWithTenantIDAndUpdateUserID PUT直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和updateUserId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func PutRouteWithTenantIDAndUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodPut, append(opts,
- WithTenantIDParamsName("tenantId"),
- WithUserIDParamsName("updateUserId"))...)
- }
- // PostRouteWithTenantIDAndUserInfo POST直传API,请求Body是JsonBody,且会添加租户ID,用户ID以及操作者用户名字段,字段名分别为tenantId,userId和operatorUserName
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func PostRouteWithTenantIDAndUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodPost, append(opts,
- WithTenantIDParamsName("tenantId"),
- WithUserIDParamsName("userId"),
- WithUserNameParamsName("operatorUserName"))...)
- }
- // PostRouteWithTenantIDAndCreateUserInfo POST直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为为tenantId,userId和operatorUserName
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func PostRouteWithTenantIDAndCreateUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodPost, append(opts,
- WithTenantIDParamsName("tenantId"),
- WithUserIDParamsName("createUserId"),
- WithUserNameParamsName("operatorUserName"))...)
- }
- // DeleteRouteWithTenantIDAndUserInfo DELETE直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为为tenantId,userId和operatorUserName
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func DeleteRouteWithTenantIDAndUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodDelete, append(opts,
- WithTenantIDParamsName("tenantId"),
- WithUserIDParamsName("userId"),
- WithUserNameParamsName("operatorUserName"))...)
- }
- // DeleteRouteWithTenantIDAndDeleteUserInfo DELETE直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为为tenantId,userId和operatorUserName
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func DeleteRouteWithTenantIDAndDeleteUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodDelete, append(opts,
- WithTenantIDParamsName("tenantId"),
- WithUserIDParamsName("deleteUserId"),
- WithUserNameParamsName("operatorUserName"))...)
- }
- // PutRouteWithTenantIDAndUserInfo PUT直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为为tenantId,userId和operatorUserName
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func PutRouteWithTenantIDAndUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodPut, append(opts,
- WithTenantIDParamsName("tenantId"),
- WithUserIDParamsName("userId"),
- WithUserNameParamsName("operatorUserName"))...)
- }
- // PutRouteWithTenantIDAndUpdateUserInfo PUT直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为为tenantId,userId和operatorUserName
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func PutRouteWithTenantIDAndUpdateUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodPut, append(opts,
- WithTenantIDParamsName("tenantId"),
- WithUserIDParamsName("updateUserId"),
- WithUserNameParamsName("operatorUserName"))...)
- }
- // GetRouteWithTenantIDAndUserID GET直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和userId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func GetRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodGet, append(opts,
- WithTenantIDParamsName("tenantId"),
- WithUserIDParamsName("userId"))...)
- }
- // GetRouteWithTenantIDAndCreateUserID GET直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和createUserId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func GetRouteWithTenantIDAndCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodGet, append(opts,
- WithTenantIDParamsName("tenantId"),
- WithUserIDParamsName("createUserId"))...)
- }
- // GetRouteWithTenantIDAndDeleteUserID GET直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和deleteUserId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func GetRouteWithTenantIDAndDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodGet, append(opts,
- WithTenantIDParamsName("tenantId"),
- WithUserIDParamsName("deleteUserId"))...)
- }
- // GetRouteWithTenantIDAndUpdateUserID GET直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和updateUserId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func GetRouteWithTenantIDAndUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodGet, append(opts,
- WithTenantIDParamsName("tenantId"),
- WithUserIDParamsName("updateUserId"))...)
- }
- // PostRouteWithTenantID POST直传API,请求Body是JsonBody,且会添加租户ID字段,字段名分别为tenantId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func PostRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodPost, append(opts,
- WithTenantIDParamsName("tenantId"))...)
- }
- // DeleteRouteWithTenantID DELETE直传API,会在查询参数添加租户ID字段,字段名分别为tenantId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func DeleteRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodDelete, append(opts,
- WithTenantIDParamsName("tenantId"))...)
- }
- // PutRouteWithTenantID PUT直传API,请求Body是JsonBody,且会添加租户ID字段,字段名分别为tenantId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func PutRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodPut, append(opts,
- WithTenantIDParamsName("tenantId"))...)
- }
- // GetRouteWithTenantID GET直传API,会在查询参数添加租户ID字段,字段名分别为tenantId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func GetRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodGet, append(opts,
- WithTenantIDParamsName("tenantId"))...)
- }
- // PostRouteWithUserID POST直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为userId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func PostRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodPost, append(opts,
- WithUserIDParamsName("userId"))...)
- }
- // PostRouteWithCreateUserID POST直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为createUserId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func PostRouteWithCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodPost, append(opts,
- WithUserIDParamsName("createUserId"))...)
- }
- // DeleteRouteWithUserID DELETE直传API,会在查询参数添加用户ID字段,字段名分别为userId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func DeleteRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodDelete, append(opts,
- WithUserIDParamsName("userId"))...)
- }
- // DeleteRouteWithDeleteUserID DELETE直传API,会在查询参数添加用户ID字段,字段名分别为deleteUserId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func DeleteRouteWithDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodDelete, append(opts,
- WithUserIDParamsName("deleteUserId"))...)
- }
- // PutRouteWithUserID PUT直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为createUserId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func PutRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodPut, append(opts,
- WithUserIDParamsName("userId"))...)
- }
- // PutRouteWithUpdateUserID PUT直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为updateUserId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func PutRouteWithUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodPut, append(opts,
- WithUserIDParamsName("updateUserId"))...)
- }
- // PostRouteWithUserInfo POST直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为userId,还会添加操作者用户名,字段名为operatorUserName
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func PostRouteWithUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodPost, append(opts,
- WithUserIDParamsName("userId"),
- WithUserNameParamsName("operatorUserName"))...)
- }
- // PostRouteWithCreateUserInfo POST直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为createUserId,还会添加操作者用户名,字段名为operatorUserName
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func PostRouteWithCreateUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodPost, append(opts,
- WithUserIDParamsName("createUserId"),
- WithUserNameParamsName("operatorUserName"))...)
- }
- // DeleteRouteWithUserInfo DELETE直传API,会在查询参数添加用户ID字段,字段名分别为userId,还会添加操作者用户名,字段名为operatorUserName
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func DeleteRouteWithUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodDelete, append(opts,
- WithUserIDParamsName("userId"),
- WithUserNameParamsName("operatorUserName"))...)
- }
- // DeleteRouteWithDeleteUserInfo DELETE直传API,会在查询参数添加用户ID字段,字段名分别为deleteUserId,还会添加操作者用户名,字段名为operatorUserName
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func DeleteRouteWithDeleteUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodDelete, append(opts,
- WithUserIDParamsName("deleteUserId"),
- WithUserNameParamsName("operatorUserName"))...)
- }
- // PutRouteWithUserInfo PUT直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为createUserId,还会添加操作者用户名,字段名为operatorUserName
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func PutRouteWithUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodPut, append(opts,
- WithUserIDParamsName("userId"),
- WithUserNameParamsName("operatorUserName"))...)
- }
- // PutRouteWithUpdateUserInfo PUT直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为updateUserId,还会添加操作者用户名,字段名为operatorUserName
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func PutRouteWithUpdateUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodPut, append(opts,
- WithUserIDParamsName("updateUserId"),
- WithUserNameParamsName("operatorUserName"))...)
- }
- // GetRouteWithUserID GET直传API,会在查询参数添加用户ID字段,字段名分别为userId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func GetRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodGet, append(opts,
- WithUserIDParamsName("userId"))...)
- }
- // GetRouteWithCreateUserID GET直传API,会在查询参数添加用户ID字段,字段名分别为createUserId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func GetRouteWithCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodGet, append(opts,
- WithUserIDParamsName("createUserId"))...)
- }
- // GetRouteWithDeleteUserID GET直传API,会在查询参数添加用户ID字段,字段名分别为deleteUserId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func GetRouteWithDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodGet, append(opts,
- WithUserIDParamsName("deleteUserId"))...)
- }
- // GetRouteWithUpdateUserID GET直传API,会在查询参数添加用户ID字段,字段名分别为updateUserId
- // 参数:
- // - builder: 该网关API构建器
- // - params: 网关直通参数
- // - opts: 网关直通选项
- // 返回值: 无
- func GetRouteWithUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
- params.passThrough(builder, http.MethodGet, append(opts,
- WithUserIDParamsName("updateUserId"))...)
- }
- // Simple 参数
- type Simple struct {
- // RelativePath 网关开放API的RelativePath
- RelativePath string
- // 服务的URL
- ServiceUrl string
- }
- func (params *Simple) passThrough(builder *gateway.Builder, httpMethod string, opts ...Option) {
- options := new(Options)
- for _, opt := range opts {
- opt(options)
- }
- builder.AddRoute(httpMethod, params.RelativePath,
- func(requestBuilder *gateway.RequestBuilder) {
- if options.responseSuccessCallback != nil {
- requestBuilder.ResponseSuccessCallback(options.responseSuccessCallback)
- }
- if options.responseErrorCallback != nil {
- requestBuilder.ResponseErrorCallback(options.responseErrorCallback)
- }
- if strutils.IsStringNotEmpty(options.tenantIDParamsName) ||
- strutils.IsStringNotEmpty(options.userIDParamsName) {
- if httpMethod == http.MethodPost || httpMethod == http.MethodPut {
- err := gateway.AddJsonBodyTenantIDAndUserInfo(requestBuilder, options.tenantIDParamsName, options.userIDParamsName, options.userNameParamsName)
- if err != nil {
- requestBuilder.ResponseError(err)
- return
- }
- }
- if httpMethod == http.MethodDelete || httpMethod == http.MethodGet {
- err := gateway.AddQueryParamsTenantIDAndUserInfo(requestBuilder, options.tenantIDParamsName, options.userIDParamsName, options.userNameParamsName)
- if err != nil {
- requestBuilder.ResponseError(err)
- return
- }
- }
- }
- if options.beforeRequestCallback != nil {
- err := options.beforeRequestCallback(requestBuilder)
- if err != nil {
- requestBuilder.ResponseError(err)
- return
- }
- }
- requestOptions := make([]gateway.RequestOption, 0)
- if options.afterRequestCallback != nil {
- requestOptions = append(requestOptions, gateway.WithRequestResponseCallback(
- func(requestBuilder *gateway.RequestBuilder, response *http_client.Response) error {
- err := options.afterRequestCallback(requestBuilder)
- if err != nil {
- return err
- }
- return nil
- }))
- }
- switch httpMethod {
- case http.MethodPost:
- requestBuilder.Post(&gateway.PostRequest{
- Url: params.ServiceUrl,
- }, requestOptions...)
- case http.MethodDelete:
- requestBuilder.Delete(&gateway.DeleteRequest{
- Url: params.ServiceUrl,
- }, requestOptions...)
- case http.MethodPut:
- requestBuilder.Put(&gateway.PutRequest{
- Url: params.ServiceUrl,
- }, requestOptions...)
- case http.MethodGet:
- requestBuilder.Get(&gateway.GetRequest{
- Url: params.ServiceUrl,
- }, requestOptions...)
- default:
- panic("不支持的请求类型")
- }
- requestBuilder.Request()
- }, options.middlewares...)
- }
- type RequestBuilderCallback func(requestBuilder *gateway.RequestBuilder) error
- type Option func(options *Options)
- type Options struct {
- // tenantIDParamsName 租户ID请求参数名称
- tenantIDParamsName string
- // userIDParamsName 用户ID请求参数名称
- userIDParamsName string
- // userNameParamsName 用户名请求参数名称
- userNameParamsName string
- // beforeRequestCallback 请求前回调
- beforeRequestCallback RequestBuilderCallback
- // afterRequestCallback 请求后回调
- afterRequestCallback RequestBuilderCallback
- // responseSuccessCallback 成功响应回调
- responseSuccessCallback gateway.ResponseSuccessCallback
- // responseErrorCallback 失败响应回调
- responseErrorCallback gateway.ResponseErrorCallback
- // 中间件
- middlewares []gateway.Handler
- }
- // WithTenantIDParamsName 设置请求参数中的租户ID参数的名称
- func WithTenantIDParamsName(tenantIDParamsName string) Option {
- return func(options *Options) {
- options.tenantIDParamsName = tenantIDParamsName
- }
- }
- // WithUserIDParamsName 设置请求参数中的用户ID参数的名称
- func WithUserIDParamsName(userIDParamsName string) Option {
- return func(options *Options) {
- options.userIDParamsName = userIDParamsName
- }
- }
- // WithUserNameParamsName 设置请求参数中的用户名参数的名称
- func WithUserNameParamsName(userNameParamsName string) Option {
- return func(options *Options) {
- options.userNameParamsName = userNameParamsName
- }
- }
- // WithBeforeRequestCallback 设置请求前回调
- func WithBeforeRequestCallback(callback RequestBuilderCallback) Option {
- return func(options *Options) {
- options.beforeRequestCallback = callback
- }
- }
- // WithAfterRequestCallback 设置请求后回调
- func WithAfterRequestCallback(callback RequestBuilderCallback) Option {
- return func(options *Options) {
- options.afterRequestCallback = callback
- }
- }
- // WithResponseSuccessCallback 设置成功响应回调,默认回调会将服务响应作为网关API的响应返回
- func WithResponseSuccessCallback(callback gateway.ResponseSuccessCallback) Option {
- return func(options *Options) {
- options.responseSuccessCallback = callback
- }
- }
- // WithResponseErrorCallback 设置失败响应回调,默认回调会按照是否存在错误返回MsgResponse
- func WithResponseErrorCallback(callback gateway.ResponseErrorCallback) Option {
- return func(options *Options) {
- options.responseErrorCallback = callback
- }
- }
- // WithMiddlewares 设置中间件
- func WithMiddlewares(middlewares ...gateway.Handler) Option {
- return func(options *Options) {
- options.middlewares = middlewares
- }
- }
|