123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- 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
- }
|