...

Source file src/github.com/aws/smithy-go/transport/http/deserialize_example_test.go

Documentation: github.com/aws/smithy-go/transport/http

     1  package http
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"os"
     8  	"strconv"
     9  
    10  	"github.com/aws/smithy-go/middleware"
    11  )
    12  
    13  func ExampleResponse_deserializeMiddleware() {
    14  	// Create the stack and provide the function that will create a new Request
    15  	// when the SerializeStep is invoked.
    16  	stack := middleware.NewStack("deserialize example", NewStackRequest)
    17  
    18  	type Output struct {
    19  		FooName  string
    20  		BarCount int
    21  	}
    22  
    23  	// Add a Deserialize middleware that will extract the RawResponse and
    24  	// deserialize into the target output type.
    25  	stack.Deserialize.Add(middleware.DeserializeMiddlewareFunc("example deserialize",
    26  		func(ctx context.Context, in middleware.DeserializeInput, next middleware.DeserializeHandler) (
    27  			out middleware.DeserializeOutput, metadata middleware.Metadata, err error,
    28  		) {
    29  			out, metadata, err = next.HandleDeserialize(ctx, in)
    30  			if err != nil {
    31  				return middleware.DeserializeOutput{}, metadata, err
    32  			}
    33  
    34  			metadata.Set("example-meta", "meta-value")
    35  
    36  			rawResp := out.RawResponse.(*Response)
    37  			out.Result = &Output{
    38  				FooName: rawResp.Header.Get("foo-name"),
    39  				BarCount: func() int {
    40  					v, _ := strconv.Atoi(rawResp.Header.Get("bar-count"))
    41  					return v
    42  				}(),
    43  			}
    44  
    45  			return out, metadata, nil
    46  		}),
    47  		middleware.After,
    48  	)
    49  
    50  	// Mock example handler taking the request input and returning a response
    51  	mockHandler := middleware.HandlerFunc(func(ctx context.Context, in interface{}) (
    52  		output interface{}, metadata middleware.Metadata, err error,
    53  	) {
    54  		resp := &http.Response{
    55  			StatusCode: 200,
    56  			Header:     http.Header{},
    57  		}
    58  		resp.Header.Set("foo-name", "abc")
    59  		resp.Header.Set("bar-count", "123")
    60  
    61  		// The handler's returned response will be available as the
    62  		// DeserializeOutput.RawResponse field.
    63  		return &Response{
    64  			Response: resp,
    65  		}, metadata, nil
    66  	})
    67  
    68  	// Use the stack to decorate the handler then invoke the decorated handler
    69  	// with the inputs.
    70  	handler := middleware.DecorateHandler(mockHandler, stack)
    71  	result, metadata, err := handler.Handle(context.Background(), struct{}{})
    72  	if err != nil {
    73  		fmt.Fprintf(os.Stderr, "failed to call operation, %v", err)
    74  		return
    75  	}
    76  
    77  	// Cast the result returned by the handler to the expected Output type.
    78  	res := result.(*Output)
    79  	fmt.Println("FooName", res.FooName)
    80  	fmt.Println("BarCount", res.BarCount)
    81  	fmt.Println("Metadata:", "example-meta:", metadata.Get("example-meta"))
    82  
    83  	// Output:
    84  	// FooName abc
    85  	// BarCount 123
    86  	// Metadata: example-meta: meta-value
    87  }
    88  

View as plain text