| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487 |
- package gateway
- import (
- "git.sxidc.com/go-framework/baize/framework/core/api"
- "git.sxidc.com/go-tools/utils/http_client"
- )
- type UrlTransferFunc func(c *api.Context, url string, historyRequest []BuilderRequest, resultMap map[string]any) (string, error)
- type HeadersFormFunc func(c *api.Context, historyRequest []BuilderRequest, resultMap map[string]any) (map[string]string, error)
- type BodyFormFunc func(c *api.Context, historyRequest []BuilderRequest, resultMap map[string]any) (any, error)
- type QueryParamsFormFunc func(c *api.Context, historyRequest []BuilderRequest, resultMap map[string]any) (map[string]string, error)
- type PathParamsFormFunc func(c *api.Context, historyRequest []BuilderRequest, resultMap map[string]any) (map[string]string, error)
- func FormJsonBodyWithTenantIDAndUserIDFunc(tenantIDFieldName string, userIDFieldName string) BodyFormFunc {
- return func(c *api.Context, historyRequest []BuilderRequest, resultMap map[string]any) (any, error) {
- err := AddJsonBodyTenantIDAndUserID(c, tenantIDFieldName, userIDFieldName)
- if err != nil {
- return nil, err
- }
- jsonBody, err := c.GetJsonBody()
- if err != nil {
- return nil, err
- }
- return jsonBody.Map(), nil
- }
- }
- func FormQueryParamsWithTenantIDAndUserIDFunc(tenantIDFieldName string, userIDFieldName string) QueryParamsFormFunc {
- return func(c *api.Context, historyRequest []BuilderRequest, resultMap map[string]any) (map[string]string, error) {
- err := AddQueryParamsTenantIDAndUserID(c, tenantIDFieldName, userIDFieldName)
- if err != nil {
- return nil, err
- }
- return c.GetQueryParams().Map(), nil
- }
- }
- type BuilderRequest interface {
- Request(c *api.Context, request *http_client.Request, historyRequest []BuilderRequest, resultMap map[string]any) (BuilderRequest, error)
- Response() *http_client.Response
- }
- type PostRequestOption func(options *PostRequestOptions)
- type PostRequestOptions struct {
- urlTransferFunc UrlTransferFunc
- headersFormFunc HeadersFormFunc
- bodyFormFunc BodyFormFunc
- }
- func PostRequestWithUrlTransferFunc(urlTransferFunc UrlTransferFunc) PostRequestOption {
- return func(options *PostRequestOptions) {
- options.urlTransferFunc = urlTransferFunc
- }
- }
- func PostRequestWithHeadersForm(headersFormFunc HeadersFormFunc) PostRequestOption {
- return func(options *PostRequestOptions) {
- options.headersFormFunc = headersFormFunc
- }
- }
- func PostRequestWithBodyForm(bodyFormFunc BodyFormFunc) PostRequestOption {
- return func(options *PostRequestOptions) {
- options.bodyFormFunc = bodyFormFunc
- }
- }
- type PostRequest struct {
- url string
- response *http_client.Response
- options *PostRequestOptions
- }
- func NewPostRequest(url string, opts ...PostRequestOption) *PostRequest {
- options := new(PostRequestOptions)
- for _, opt := range opts {
- opt(options)
- }
- if options.urlTransferFunc == nil {
- options.urlTransferFunc = defaultUrlTransferFunc
- }
- if options.headersFormFunc == nil {
- options.headersFormFunc = defaultHeadersFormFunc
- }
- if options.bodyFormFunc == nil {
- options.bodyFormFunc = defaultBodyFormFunc
- }
- return &PostRequest{
- url: url,
- options: options,
- }
- }
- func (req *PostRequest) Request(c *api.Context, request *http_client.Request, historyRequest []BuilderRequest, resultMap map[string]any) (BuilderRequest, error) {
- url, err := req.options.urlTransferFunc(c, req.url, historyRequest, resultMap)
- if err != nil {
- return nil, err
- }
- headers, err := req.options.headersFormFunc(c, historyRequest, resultMap)
- if err != nil {
- return nil, err
- }
- body, err := req.options.bodyFormFunc(c, historyRequest, resultMap)
- if err != nil {
- return nil, err
- }
- response, err := request.Post(url, body, http_client.WithRequestHeaders(headers))
- if err != nil {
- return nil, err
- }
- return &PostRequest{
- url: url,
- response: response,
- options: &PostRequestOptions{
- urlTransferFunc: req.options.urlTransferFunc,
- headersFormFunc: req.options.headersFormFunc,
- bodyFormFunc: req.options.bodyFormFunc,
- },
- }, nil
- }
- func (req *PostRequest) Response() *http_client.Response {
- return req.response
- }
- type DeleteRequestOption func(options *DeleteRequestOptions)
- type DeleteRequestOptions struct {
- urlTransferFunc UrlTransferFunc
- headersFormFunc HeadersFormFunc
- pathParamsFormFunc PathParamsFormFunc
- queryParamsFormFunc QueryParamsFormFunc
- }
- func DeleteRequestWithUrlTransferFunc(urlTransferFunc UrlTransferFunc) DeleteRequestOption {
- return func(options *DeleteRequestOptions) {
- options.urlTransferFunc = urlTransferFunc
- }
- }
- func DeleteRequestWithHeadersForm(headersFormFunc HeadersFormFunc) DeleteRequestOption {
- return func(options *DeleteRequestOptions) {
- options.headersFormFunc = headersFormFunc
- }
- }
- func DeleteRequestWithPathParamsForm(pathParamsFormFunc PathParamsFormFunc) DeleteRequestOption {
- return func(options *DeleteRequestOptions) {
- options.pathParamsFormFunc = pathParamsFormFunc
- }
- }
- func DeleteRequestWithQueryParamsForm(queryParamsFormFunc QueryParamsFormFunc) DeleteRequestOption {
- return func(options *DeleteRequestOptions) {
- options.queryParamsFormFunc = queryParamsFormFunc
- }
- }
- type DeleteRequest struct {
- url string
- response *http_client.Response
- options *DeleteRequestOptions
- }
- func NewDeleteRequest(url string, opts ...DeleteRequestOption) *DeleteRequest {
- options := new(DeleteRequestOptions)
- for _, opt := range opts {
- opt(options)
- }
- if options.urlTransferFunc == nil {
- options.urlTransferFunc = defaultUrlTransferFunc
- }
- if options.headersFormFunc == nil {
- options.headersFormFunc = defaultHeadersFormFunc
- }
- if options.pathParamsFormFunc == nil {
- options.pathParamsFormFunc = defaultPathParamsFormFunc
- }
- if options.queryParamsFormFunc == nil {
- options.queryParamsFormFunc = defaultQueryParamsFormFunc
- }
- return &DeleteRequest{
- url: url,
- options: options,
- }
- }
- func (req *DeleteRequest) Request(c *api.Context, request *http_client.Request, historyRequest []BuilderRequest, resultMap map[string]any) (BuilderRequest, error) {
- url, err := req.options.urlTransferFunc(c, req.url, historyRequest, resultMap)
- if err != nil {
- return nil, err
- }
- headers, err := req.options.headersFormFunc(c, historyRequest, resultMap)
- if err != nil {
- return nil, err
- }
- pathParams, err := req.options.pathParamsFormFunc(c, historyRequest, resultMap)
- if err != nil {
- return nil, err
- }
- queryParams, err := req.options.queryParamsFormFunc(c, historyRequest, resultMap)
- if err != nil {
- return nil, err
- }
- response, err := request.Delete(url,
- http_client.WithRequestHeaders(headers),
- http_client.WithRequestPathParams(pathParams),
- http_client.WithRequestQueryParams(queryParams))
- if err != nil {
- return nil, err
- }
- return &DeleteRequest{
- url: url,
- response: response,
- options: &DeleteRequestOptions{
- urlTransferFunc: req.options.urlTransferFunc,
- headersFormFunc: req.options.headersFormFunc,
- pathParamsFormFunc: req.options.pathParamsFormFunc,
- queryParamsFormFunc: req.options.queryParamsFormFunc,
- },
- }, nil
- }
- func (req *DeleteRequest) Response() *http_client.Response {
- return req.response
- }
- type PutRequestOption func(options *PutRequestOptions)
- type PutRequestOptions struct {
- urlTransferFunc UrlTransferFunc
- headersFormFunc HeadersFormFunc
- bodyFormFunc BodyFormFunc
- }
- func PutRequestWithUrlTransferFunc(urlTransferFunc UrlTransferFunc) PutRequestOption {
- return func(options *PutRequestOptions) {
- options.urlTransferFunc = urlTransferFunc
- }
- }
- func PutRequestWithHeadersForm(headersFormFunc HeadersFormFunc) PutRequestOption {
- return func(options *PutRequestOptions) {
- options.headersFormFunc = headersFormFunc
- }
- }
- func PutRequestWithBodyForm(bodyFormFunc BodyFormFunc) PutRequestOption {
- return func(options *PutRequestOptions) {
- options.bodyFormFunc = bodyFormFunc
- }
- }
- type PutRequest struct {
- url string
- response *http_client.Response
- options *PutRequestOptions
- }
- func NewPutRequest(url string, opts ...PutRequestOption) *PutRequest {
- options := new(PutRequestOptions)
- for _, opt := range opts {
- opt(options)
- }
- if options.urlTransferFunc == nil {
- options.urlTransferFunc = defaultUrlTransferFunc
- }
- if options.headersFormFunc == nil {
- options.headersFormFunc = defaultHeadersFormFunc
- }
- if options.bodyFormFunc == nil {
- options.bodyFormFunc = defaultBodyFormFunc
- }
- return &PutRequest{
- url: url,
- options: options,
- }
- }
- func (req *PutRequest) Request(c *api.Context, request *http_client.Request, historyRequest []BuilderRequest, resultMap map[string]any) (BuilderRequest, error) {
- url, err := req.options.urlTransferFunc(c, req.url, historyRequest, resultMap)
- if err != nil {
- return nil, err
- }
- headers, err := req.options.headersFormFunc(c, historyRequest, resultMap)
- if err != nil {
- return nil, err
- }
- body, err := req.options.bodyFormFunc(c, historyRequest, resultMap)
- if err != nil {
- return nil, err
- }
- response, err := request.Put(url, body, http_client.WithRequestHeaders(headers))
- if err != nil {
- return nil, err
- }
- return &PutRequest{
- url: url,
- response: response,
- options: &PutRequestOptions{
- urlTransferFunc: req.options.urlTransferFunc,
- headersFormFunc: req.options.headersFormFunc,
- bodyFormFunc: req.options.bodyFormFunc,
- },
- }, nil
- }
- func (req *PutRequest) Response() *http_client.Response {
- return req.response
- }
- type GetRequestOption func(options *GetRequestOptions)
- type GetRequestOptions struct {
- urlTransferFunc UrlTransferFunc
- headersFormFunc HeadersFormFunc
- pathParamsFormFunc PathParamsFormFunc
- queryParamsFormFunc QueryParamsFormFunc
- }
- func GetRequestWithUrlTransferFunc(urlTransferFunc UrlTransferFunc) GetRequestOption {
- return func(options *GetRequestOptions) {
- options.urlTransferFunc = urlTransferFunc
- }
- }
- func GetRequestWithHeadersForm(headersFormFunc HeadersFormFunc) GetRequestOption {
- return func(options *GetRequestOptions) {
- options.headersFormFunc = headersFormFunc
- }
- }
- func GetRequestWithPathParamsForm(pathParamsFormFunc PathParamsFormFunc) GetRequestOption {
- return func(options *GetRequestOptions) {
- options.pathParamsFormFunc = pathParamsFormFunc
- }
- }
- func GetRequestWithQueryParamsForm(queryParamsFormFunc QueryParamsFormFunc) GetRequestOption {
- return func(options *GetRequestOptions) {
- options.queryParamsFormFunc = queryParamsFormFunc
- }
- }
- type GetRequest struct {
- url string
- headers map[string]string
- pathParams map[string]string
- queryParams map[string]string
- response *http_client.Response
- options *GetRequestOptions
- }
- func NewGetRequest(url string, opts ...GetRequestOption) *GetRequest {
- options := new(GetRequestOptions)
- for _, opt := range opts {
- opt(options)
- }
- if options.urlTransferFunc == nil {
- options.urlTransferFunc = defaultUrlTransferFunc
- }
- if options.headersFormFunc == nil {
- options.headersFormFunc = defaultHeadersFormFunc
- }
- if options.pathParamsFormFunc == nil {
- options.pathParamsFormFunc = defaultPathParamsFormFunc
- }
- if options.queryParamsFormFunc == nil {
- options.queryParamsFormFunc = defaultQueryParamsFormFunc
- }
- return &GetRequest{
- url: url,
- options: options,
- }
- }
- func (req *GetRequest) Request(c *api.Context, request *http_client.Request, historyRequest []BuilderRequest, resultMap map[string]any) (BuilderRequest, error) {
- url, err := req.options.urlTransferFunc(c, req.url, historyRequest, resultMap)
- if err != nil {
- return nil, err
- }
- headers, err := req.options.headersFormFunc(c, historyRequest, resultMap)
- if err != nil {
- return nil, err
- }
- pathParams, err := req.options.pathParamsFormFunc(c, historyRequest, resultMap)
- if err != nil {
- return nil, err
- }
- queryParams, err := req.options.queryParamsFormFunc(c, historyRequest, resultMap)
- if err != nil {
- return nil, err
- }
- response, err := request.Get(url,
- http_client.WithRequestHeaders(headers),
- http_client.WithRequestPathParams(pathParams),
- http_client.WithRequestQueryParams(queryParams))
- if err != nil {
- return nil, err
- }
- return &GetRequest{
- url: url,
- headers: headers,
- pathParams: pathParams,
- queryParams: queryParams,
- response: response,
- options: &GetRequestOptions{
- urlTransferFunc: req.options.urlTransferFunc,
- headersFormFunc: req.options.headersFormFunc,
- pathParamsFormFunc: req.options.pathParamsFormFunc,
- queryParamsFormFunc: req.options.queryParamsFormFunc,
- },
- }, nil
- }
- func (req *GetRequest) Response() *http_client.Response {
- return req.response
- }
- func defaultUrlTransferFunc(_ *api.Context, url string, _ []BuilderRequest, _ map[string]any) (string, error) {
- return url, nil
- }
- func defaultHeadersFormFunc(c *api.Context, _ []BuilderRequest, _ map[string]any) (map[string]string, error) {
- return c.GetHeaders(), nil
- }
- func defaultBodyFormFunc(c *api.Context, _ []BuilderRequest, _ map[string]any) (any, error) {
- cachedBody, err := c.GetBytesBody()
- if err != nil {
- return nil, err
- }
- return cachedBody.Bytes(), nil
- }
- func defaultQueryParamsFormFunc(c *api.Context, _ []BuilderRequest, _ map[string]any) (map[string]string, error) {
- return c.GetQueryParams().Map(), nil
- }
- func defaultPathParamsFormFunc(c *api.Context, _ []BuilderRequest, _ map[string]any) (map[string]string, error) {
- return c.GetPathParams().Map(), nil
- }
|