| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- package gateway
- import (
- "git.sxidc.com/go-framework/baize/framework/core/api"
- "git.sxidc.com/go-tools/utils/http_client"
- "net/http"
- )
- type FormHeadersFunc func(c *api.Context) (map[string]string, error)
- type FormBodyFunc func(c *api.Context) (any, error)
- type FormQueryParamsFunc func(c *api.Context) (map[string]string, error)
- type FormPathParamsFunc func(c *api.Context) (map[string]string, error)
- func DefaultFormHeadersFunc(c *api.Context) (map[string]string, error) {
- return c.GetHeaders(), nil
- }
- func DefaultFormBodyFunc(c *api.Context) (any, error) {
- return c.ReadBody()
- }
- func DefaultFormQueryParamsFunc(c *api.Context) (map[string]string, error) {
- return c.GetAllQueryParams(), nil
- }
- func DefaultFormPathParamsFunc(c *api.Context) (map[string]string, error) {
- return c.GetAllPathParams(), nil
- }
- type BuilderRequest interface {
- HttpMethod() string
- RequestUrl() string
- Request(c *api.Context, request *http_client.Request) error
- Response() *http_client.Response
- }
- type PostRequest struct {
- Url string
- Headers FormHeadersFunc
- Body FormBodyFunc
- response *http_client.Response
- }
- func (req *PostRequest) HttpMethod() string {
- return http.MethodPost
- }
- func (req *PostRequest) RequestUrl() string {
- return req.Url
- }
- func (req *PostRequest) Request(c *api.Context, request *http_client.Request) error {
- if req.Headers == nil {
- req.Headers = DefaultFormHeadersFunc
- }
- if req.Body == nil {
- req.Body = DefaultFormBodyFunc
- }
- headers, err := req.Headers(c)
- if err != nil {
- return err
- }
- body, err := req.Body(c)
- if err != nil {
- return err
- }
- response, err := request.Post(req.Url, body,
- http_client.WithRequestHeaders(headers))
- if err != nil {
- return err
- }
- req.response = response
- return nil
- }
- func (req *PostRequest) Response() *http_client.Response {
- return req.response
- }
- type DeleteRequest struct {
- Url string
- Headers FormHeadersFunc
- PathParams FormPathParamsFunc
- QueryParams FormQueryParamsFunc
- response *http_client.Response
- }
- func (req *DeleteRequest) HttpMethod() string {
- return http.MethodDelete
- }
- func (req *DeleteRequest) RequestUrl() string {
- return req.Url
- }
- func (req *DeleteRequest) Request(c *api.Context, request *http_client.Request) error {
- if req.Headers == nil {
- req.Headers = DefaultFormHeadersFunc
- }
- if req.QueryParams == nil {
- req.QueryParams = DefaultFormQueryParamsFunc
- }
- if req.PathParams == nil {
- req.PathParams = DefaultFormPathParamsFunc
- }
- headers, err := req.Headers(c)
- if err != nil {
- return err
- }
- queryParams, err := req.QueryParams(c)
- if err != nil {
- return err
- }
- pathParams, err := req.PathParams(c)
- if err != nil {
- return err
- }
- response, err := request.Delete(req.Url,
- http_client.WithRequestHeaders(headers),
- http_client.WithRequestPathParams(pathParams),
- http_client.WithRequestQueryParams(queryParams))
- if err != nil {
- return err
- }
- req.response = response
- return nil
- }
- func (req *DeleteRequest) Response() *http_client.Response {
- return req.response
- }
- type PutRequest struct {
- Url string
- Headers FormHeadersFunc
- Body FormBodyFunc
- response *http_client.Response
- }
- func (req *PutRequest) HttpMethod() string {
- return http.MethodPut
- }
- func (req *PutRequest) RequestUrl() string {
- return req.Url
- }
- func (req *PutRequest) Request(c *api.Context, request *http_client.Request) error {
- if req.Headers == nil {
- req.Headers = DefaultFormHeadersFunc
- }
- if req.Body == nil {
- req.Body = DefaultFormBodyFunc
- }
- headers, err := req.Headers(c)
- if err != nil {
- return err
- }
- body, err := req.Body(c)
- if err != nil {
- return err
- }
- response, err := request.Put(req.Url, body,
- http_client.WithRequestHeaders(headers))
- if err != nil {
- return err
- }
- req.response = response
- return nil
- }
- func (req *PutRequest) Response() *http_client.Response {
- return req.response
- }
- type GetRequest struct {
- Url string
- Headers FormHeadersFunc
- PathParams FormPathParamsFunc
- QueryParams FormQueryParamsFunc
- response *http_client.Response
- }
- func (req *GetRequest) HttpMethod() string {
- return http.MethodGet
- }
- func (req *GetRequest) RequestUrl() string {
- return req.Url
- }
- func (req *GetRequest) Request(c *api.Context, request *http_client.Request) error {
- if req.Headers == nil {
- req.Headers = DefaultFormHeadersFunc
- }
- if req.QueryParams == nil {
- req.QueryParams = DefaultFormQueryParamsFunc
- }
- if req.PathParams == nil {
- req.PathParams = DefaultFormPathParamsFunc
- }
- headers, err := req.Headers(c)
- if err != nil {
- return err
- }
- queryParams, err := req.QueryParams(c)
- if err != nil {
- return err
- }
- pathParams, err := req.PathParams(c)
- if err != nil {
- return err
- }
- response, err := request.Get(req.Url,
- http_client.WithRequestHeaders(headers),
- http_client.WithRequestPathParams(pathParams),
- http_client.WithRequestQueryParams(queryParams))
- if err != nil {
- return err
- }
- req.response = response
- return nil
- }
- func (req *GetRequest) Response() *http_client.Response {
- return req.response
- }
|