yjp hace 1 año
padre
commit
831ff33c61
Se han modificado 2 ficheros con 127 adiciones y 40 borrados
  1. 12 2
      framework/core/api/context.go
  2. 115 38
      framework/gateway/builder_request.go

+ 12 - 2
framework/core/api/context.go

@@ -36,6 +36,16 @@ func (c *Context) GetFileHeaderBytes(fileHeader *multipart.FileHeader) (string,
 	return fileHeader.Filename, contentBytes, nil
 }
 
+func (c *Context) GetHeaders() map[string]string {
+	headers := make(map[string]string, 0)
+
+	for key, values := range c.Request.Header {
+		headers[key] = strings.Join(values, ",")
+	}
+
+	return headers
+}
+
 func (c *Context) ReadBody() ([]byte, error) {
 	if c.Request.Body == nil {
 		return make([]byte, 0), nil
@@ -62,8 +72,8 @@ func (c *Context) ReadBody() ([]byte, error) {
 func (c *Context) GetAllQueryParams() map[string]string {
 	queryParams := make(map[string]string, 0)
 
-	for key, value := range c.Request.URL.Query() {
-		queryParams[key] = strings.Join(value, ",")
+	for key, values := range c.Request.URL.Query() {
+		queryParams[key] = strings.Join(values, ",")
 	}
 
 	return queryParams

+ 115 - 38
framework/gateway/builder_request.go

@@ -6,6 +6,27 @@ import (
 	"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
@@ -15,8 +36,8 @@ type BuilderRequest interface {
 
 type PostRequest struct {
 	Url     string
-	Headers map[string]string
-	Body    any
+	Headers FormHeadersFunc
+	Body    FormBodyFunc
 
 	response *http_client.Response
 }
@@ -30,17 +51,26 @@ func (req *PostRequest) RequestUrl() string {
 }
 
 func (req *PostRequest) Request(c *api.Context, request *http_client.Request) error {
+	if req.Headers == nil {
+		req.Headers = DefaultFormHeadersFunc
+	}
+
 	if req.Body == nil {
-		body, err := c.ReadBody()
-		if err != nil {
-			return err
-		}
+		req.Body = DefaultFormBodyFunc
+	}
+
+	headers, err := req.Headers(c)
+	if err != nil {
+		return err
+	}
 
-		req.Body = body
+	body, err := req.Body(c)
+	if err != nil {
+		return err
 	}
 
-	response, err := request.Post(req.Url, req.Body,
-		http_client.WithRequestHeaders(req.Headers))
+	response, err := request.Post(req.Url, body,
+		http_client.WithRequestHeaders(headers))
 	if err != nil {
 		return err
 	}
@@ -56,9 +86,9 @@ func (req *PostRequest) Response() *http_client.Response {
 
 type DeleteRequest struct {
 	Url         string
-	Headers     map[string]string
-	PathParams  map[string]string
-	QueryParams map[string]string
+	Headers     FormHeadersFunc
+	PathParams  FormPathParamsFunc
+	QueryParams FormQueryParamsFunc
 
 	response *http_client.Response
 }
@@ -72,18 +102,37 @@ func (req *DeleteRequest) RequestUrl() string {
 }
 
 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.Headers == nil {
+		req.Headers = DefaultFormHeadersFunc
+	}
+
+	if req.QueryParams == nil {
+		req.QueryParams = DefaultFormQueryParamsFunc
+	}
+
+	if req.PathParams == nil {
+		req.PathParams = DefaultFormPathParamsFunc
 	}
 
-	if req.PathParams == nil || len(req.PathParams) == 0 {
-		req.PathParams = c.GetAllPathParams()
+	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(req.Headers),
-		http_client.WithRequestPathParams(req.PathParams),
-		http_client.WithRequestQueryParams(req.QueryParams))
+		http_client.WithRequestHeaders(headers),
+		http_client.WithRequestPathParams(pathParams),
+		http_client.WithRequestQueryParams(queryParams))
 	if err != nil {
 		return err
 	}
@@ -99,8 +148,8 @@ func (req *DeleteRequest) Response() *http_client.Response {
 
 type PutRequest struct {
 	Url     string
-	Headers map[string]string
-	Body    []byte
+	Headers FormHeadersFunc
+	Body    FormBodyFunc
 
 	response *http_client.Response
 }
@@ -114,17 +163,26 @@ func (req *PutRequest) RequestUrl() string {
 }
 
 func (req *PutRequest) Request(c *api.Context, request *http_client.Request) error {
+	if req.Headers == nil {
+		req.Headers = DefaultFormHeadersFunc
+	}
+
 	if req.Body == nil {
-		body, err := c.ReadBody()
-		if err != nil {
-			return err
-		}
+		req.Body = DefaultFormBodyFunc
+	}
 
-		req.Body = body
+	headers, err := req.Headers(c)
+	if err != nil {
+		return err
 	}
 
-	response, err := request.Put(req.Url, req.Body,
-		http_client.WithRequestHeaders(req.Headers))
+	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
 	}
@@ -140,9 +198,9 @@ func (req *PutRequest) Response() *http_client.Response {
 
 type GetRequest struct {
 	Url         string
-	Headers     map[string]string
-	PathParams  map[string]string
-	QueryParams map[string]string
+	Headers     FormHeadersFunc
+	PathParams  FormPathParamsFunc
+	QueryParams FormQueryParamsFunc
 
 	response *http_client.Response
 }
@@ -156,18 +214,37 @@ func (req *GetRequest) RequestUrl() string {
 }
 
 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.Headers == nil {
+		req.Headers = DefaultFormHeadersFunc
 	}
 
-	if req.PathParams == nil || len(req.PathParams) == 0 {
-		req.PathParams = c.GetAllPathParams()
+	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(req.Headers),
-		http_client.WithRequestPathParams(req.PathParams),
-		http_client.WithRequestQueryParams(req.QueryParams))
+		http_client.WithRequestHeaders(headers),
+		http_client.WithRequestPathParams(pathParams),
+		http_client.WithRequestQueryParams(queryParams))
 	if err != nil {
 		return err
 	}