| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- package gateway
- import (
- "git.sxidc.com/go-framework/baize/framework/core/api"
- "git.sxidc.com/go-tools/utils/http_client"
- "net/http"
- )
- 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 map[string]string
- Body any
- 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.Body == nil {
- body, err := c.ReadBody()
- if err != nil {
- return err
- }
- req.Body = body
- }
- response, err := request.Post(req.Url, req.Body,
- http_client.WithRequestHeaders(req.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 map[string]string
- PathParams map[string]string
- QueryParams map[string]string
- 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.QueryParams == nil || len(req.QueryParams) == 0 {
- req.QueryParams = c.GetAllQueryParams()
- }
- if req.PathParams == nil || len(req.PathParams) == 0 {
- req.PathParams = c.GetAllPathParams()
- }
- response, err := request.Delete(req.Url,
- http_client.WithRequestHeaders(req.Headers),
- http_client.WithRequestPathParams(req.PathParams),
- http_client.WithRequestQueryParams(req.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 map[string]string
- Body []byte
- 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.Body == nil {
- body, err := c.ReadBody()
- if err != nil {
- return err
- }
- req.Body = body
- }
- response, err := request.Put(req.Url, req.Body,
- http_client.WithRequestHeaders(req.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 map[string]string
- PathParams map[string]string
- QueryParams map[string]string
- 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.QueryParams == nil || len(req.QueryParams) == 0 {
- req.QueryParams = c.GetAllQueryParams()
- }
- if req.PathParams == nil || len(req.PathParams) == 0 {
- req.PathParams = c.GetAllPathParams()
- }
- response, err := request.Get(req.Url,
- http_client.WithRequestHeaders(req.Headers),
- http_client.WithRequestPathParams(req.PathParams),
- http_client.WithRequestQueryParams(req.QueryParams))
- if err != nil {
- return err
- }
- req.response = response
- return nil
- }
- func (req *GetRequest) Response() *http_client.Response {
- return req.response
- }
|