code.dwrz.net

Go monorepo.
Log | Files | Refs

middleware.go (1718B)


      1 package query
      2 
      3 import (
      4 	"context"
      5 	"fmt"
      6 	"io/ioutil"
      7 
      8 	"github.com/aws/smithy-go/middleware"
      9 	smithyhttp "github.com/aws/smithy-go/transport/http"
     10 )
     11 
     12 // AddAsGetRequestMiddleware adds a middleware to the Serialize stack after the
     13 // operation serializer that will convert the query request body to a GET
     14 // operation with the query message in the HTTP request querystring.
     15 func AddAsGetRequestMiddleware(stack *middleware.Stack) error {
     16 	return stack.Serialize.Insert(&asGetRequest{}, "OperationSerializer", middleware.After)
     17 }
     18 
     19 type asGetRequest struct{}
     20 
     21 func (*asGetRequest) ID() string { return "Query:AsGetRequest" }
     22 
     23 func (m *asGetRequest) HandleSerialize(
     24 	ctx context.Context, input middleware.SerializeInput, next middleware.SerializeHandler,
     25 ) (
     26 	out middleware.SerializeOutput, metadata middleware.Metadata, err error,
     27 ) {
     28 	req, ok := input.Request.(*smithyhttp.Request)
     29 	if !ok {
     30 		return out, metadata, fmt.Errorf("expect smithy HTTP Request, got %T", input.Request)
     31 	}
     32 
     33 	req.Method = "GET"
     34 
     35 	// If the stream is not set, nothing else to do.
     36 	stream := req.GetStream()
     37 	if stream == nil {
     38 		return next.HandleSerialize(ctx, input)
     39 	}
     40 
     41 	// Clear the stream since there will not be any body.
     42 	req.Header.Del("Content-Type")
     43 	req, err = req.SetStream(nil)
     44 	if err != nil {
     45 		return out, metadata, fmt.Errorf("unable update request body %w", err)
     46 	}
     47 	input.Request = req
     48 
     49 	// Update request query with the body's query string value.
     50 	delim := ""
     51 	if len(req.URL.RawQuery) != 0 {
     52 		delim = "&"
     53 	}
     54 
     55 	b, err := ioutil.ReadAll(stream)
     56 	if err != nil {
     57 		return out, metadata, fmt.Errorf("unable to get request body %w", err)
     58 	}
     59 	req.URL.RawQuery += delim + string(b)
     60 
     61 	return next.HandleSerialize(ctx, input)
     62 }