package gateway import ( "git.sxidc.com/go-framework/baize/framework/core/api" "git.sxidc.com/go-tools/utils/http_client" ) // Request 请求接口 type Request interface { // Request 发送请求 // 参数: // - c: 上下文 // - request: HTTP客户端请求,用来发送HTTP请求 // 返回值: // - 带有响应的请求接口 // - 错误 Request(c *api.Context, request *http_client.Request) (Request, error) // Response 获取响应 // 参数: 无 // 返回值: // - HTTP客户端响应 Response() *http_client.Response } // PostRequest POST请求 type PostRequest struct { // Url 要发送请求的完整URL Url string // Body请求体,如果为nil,则默认读取RequestBuilder中上下文中的缓存Body Body any headers map[string]string response *http_client.Response } func (req *PostRequest) Request(c *api.Context, request *http_client.Request) (Request, error) { preparedRequest, err := req.prepare(c) if err != nil { return nil, err } response, err := request.Post(preparedRequest.Url, preparedRequest.Body, http_client.WithRequestHeaders(preparedRequest.headers)) if err != nil { return nil, err } preparedRequest.response = response return preparedRequest, nil } func (req *PostRequest) Response() *http_client.Response { return req.response } func (req *PostRequest) prepare(c *api.Context) (*PostRequest, error) { preparedRequest := &PostRequest{ Url: req.Url, Body: req.Body, } // 赋值默认body if preparedRequest.Body == nil { cacheBody, err := c.GetBytesBody() if err != nil { return nil, err } preparedRequest.Body = cacheBody.Bytes() } // 赋值默认headers preparedRequest.headers = c.GetHeader().Map() return preparedRequest, nil } // DeleteRequest DELETE请求 type DeleteRequest struct { // Url 要发送请求的完整URL Url string // 查询参数,如果为nil,则默认读取RequestBuilder中上下文中的缓存查询参数 QueryParams map[string]string // 路径参数,如果为nil,则默认读取RequestBuilder中上下文中的缓存路径参数 PathParams map[string]string headers map[string]string response *http_client.Response } func (req *DeleteRequest) Request(c *api.Context, request *http_client.Request) (Request, error) { preparedRequest, err := req.prepare(c) if err != nil { return nil, err } response, err := request.Delete(preparedRequest.Url, http_client.WithRequestQueryParams(preparedRequest.QueryParams), http_client.WithRequestPathParams(preparedRequest.PathParams), http_client.WithRequestHeaders(preparedRequest.headers)) if err != nil { return nil, err } preparedRequest.response = response return preparedRequest, nil } func (req *DeleteRequest) Response() *http_client.Response { return req.response } func (req *DeleteRequest) prepare(c *api.Context) (*DeleteRequest, error) { preparedRequest := &DeleteRequest{ Url: req.Url, QueryParams: req.QueryParams, PathParams: req.PathParams, } // 赋值默认查询参数 if preparedRequest.QueryParams == nil || len(preparedRequest.QueryParams) == 0 { preparedRequest.QueryParams = c.GetQueryParams().Map() } // 赋值默认路径参数 if preparedRequest.PathParams == nil || len(preparedRequest.PathParams) == 0 { preparedRequest.PathParams = c.GetPathParams().Map() } // 赋值默认headers preparedRequest.headers = c.GetHeader().Map() return preparedRequest, nil } // PutRequest PUT请求 type PutRequest struct { // Url 要发送请求的完整URL Url string // Body请求体,如果为nil,则默认读取RequestBuilder中上下文中的缓存Body Body any headers map[string]string response *http_client.Response } func (req *PutRequest) Request(c *api.Context, request *http_client.Request) (Request, error) { preparedRequest, err := req.prepare(c) if err != nil { return nil, err } response, err := request.Put(preparedRequest.Url, preparedRequest.Body, http_client.WithRequestHeaders(preparedRequest.headers)) if err != nil { return nil, err } preparedRequest.response = response return preparedRequest, nil } func (req *PutRequest) Response() *http_client.Response { return req.response } func (req *PutRequest) prepare(c *api.Context) (*PutRequest, error) { preparedRequest := &PutRequest{ Url: req.Url, Body: req.Body, } // 赋值默认body if preparedRequest.Body == nil { cacheBody, err := c.GetBytesBody() if err != nil { return nil, err } preparedRequest.Body = cacheBody.Bytes() } // 赋值默认headers preparedRequest.headers = c.GetHeader().Map() return preparedRequest, nil } // GetRequest GET请求 type GetRequest struct { // Url 要发送请求的完整URL Url string // 查询参数,如果为nil,则默认读取RequestBuilder中上下文中的缓存查询参数 QueryParams map[string]string // 路径参数,如果为nil,则默认读取RequestBuilder中上下文中的缓存路径参数 PathParams map[string]string headers map[string]string response *http_client.Response } func (req *GetRequest) Request(c *api.Context, request *http_client.Request) (Request, error) { preparedRequest, err := req.prepare(c) if err != nil { return nil, err } response, err := request.Get(preparedRequest.Url, http_client.WithRequestQueryParams(preparedRequest.QueryParams), http_client.WithRequestPathParams(preparedRequest.PathParams), http_client.WithRequestHeaders(preparedRequest.headers)) if err != nil { return nil, err } preparedRequest.response = response return preparedRequest, nil } func (req *GetRequest) Response() *http_client.Response { return req.response } func (req *GetRequest) prepare(c *api.Context) (*GetRequest, error) { preparedRequest := &GetRequest{ Url: req.Url, QueryParams: req.QueryParams, PathParams: req.PathParams, } // 赋值默认查询参数 if preparedRequest.QueryParams == nil || len(preparedRequest.QueryParams) == 0 { preparedRequest.QueryParams = c.GetQueryParams().Map() } // 赋值默认路径参数 if preparedRequest.PathParams == nil || len(preparedRequest.PathParams) == 0 { preparedRequest.PathParams = c.GetPathParams().Map() } // 赋值默认headers preparedRequest.headers = c.GetHeader().Map() return preparedRequest, nil }