src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

interceptor_middleware.go (8583B)


      1 package http
      2 
      3 import (
      4 	"context"
      5 	"errors"
      6 
      7 	"github.com/aws/smithy-go/middleware"
      8 )
      9 
     10 type ictxKey struct{}
     11 
     12 func withIctx(ctx context.Context) context.Context {
     13 	return middleware.WithStackValue(ctx, ictxKey{}, &InterceptorContext{})
     14 }
     15 
     16 func getIctx(ctx context.Context) *InterceptorContext {
     17 	return middleware.GetStackValue(ctx, ictxKey{}).(*InterceptorContext)
     18 }
     19 
     20 // InterceptExecution runs Before/AfterExecutionInterceptors.
     21 type InterceptExecution struct {
     22 	BeforeExecution []BeforeExecutionInterceptor
     23 	AfterExecution  []AfterExecutionInterceptor
     24 }
     25 
     26 // ID identifies the middleware.
     27 func (m *InterceptExecution) ID() string {
     28 	return "InterceptExecution"
     29 }
     30 
     31 // HandleInitialize runs the interceptors.
     32 func (m *InterceptExecution) HandleInitialize(
     33 	ctx context.Context, in middleware.InitializeInput, next middleware.InitializeHandler,
     34 ) (
     35 	out middleware.InitializeOutput, md middleware.Metadata, err error,
     36 ) {
     37 	ctx = withIctx(ctx)
     38 	getIctx(ctx).Input = in.Parameters
     39 
     40 	for _, i := range m.BeforeExecution {
     41 		if err := i.BeforeExecution(ctx, getIctx(ctx)); err != nil {
     42 			return out, md, err
     43 		}
     44 	}
     45 
     46 	out, md, err = next.HandleInitialize(ctx, in)
     47 
     48 	for _, i := range m.AfterExecution {
     49 		if err := i.AfterExecution(ctx, getIctx(ctx)); err != nil {
     50 			return out, md, err
     51 		}
     52 	}
     53 
     54 	return out, md, err
     55 }
     56 
     57 // InterceptBeforeSerialization runs BeforeSerializationInterceptors.
     58 type InterceptBeforeSerialization struct {
     59 	Interceptors []BeforeSerializationInterceptor
     60 }
     61 
     62 // ID identifies the middleware.
     63 func (m *InterceptBeforeSerialization) ID() string {
     64 	return "InterceptBeforeSerialization"
     65 }
     66 
     67 // HandleSerialize runs the interceptors.
     68 func (m *InterceptBeforeSerialization) HandleSerialize(
     69 	ctx context.Context, in middleware.SerializeInput, next middleware.SerializeHandler,
     70 ) (
     71 	out middleware.SerializeOutput, md middleware.Metadata, err error,
     72 ) {
     73 	for _, i := range m.Interceptors {
     74 		if err := i.BeforeSerialization(ctx, getIctx(ctx)); err != nil {
     75 			return out, md, err
     76 		}
     77 	}
     78 
     79 	return next.HandleSerialize(ctx, in)
     80 }
     81 
     82 // InterceptAfterSerialization runs AfterSerializationInterceptors.
     83 type InterceptAfterSerialization struct {
     84 	Interceptors []AfterSerializationInterceptor
     85 }
     86 
     87 // ID identifies the middleware.
     88 func (m *InterceptAfterSerialization) ID() string {
     89 	return "InterceptAfterSerialization"
     90 }
     91 
     92 // HandleSerialize runs the interceptors.
     93 func (m *InterceptAfterSerialization) HandleSerialize(
     94 	ctx context.Context, in middleware.SerializeInput, next middleware.SerializeHandler,
     95 ) (
     96 	out middleware.SerializeOutput, md middleware.Metadata, err error,
     97 ) {
     98 	getIctx(ctx).Request = in.Request.(*Request)
     99 
    100 	for _, i := range m.Interceptors {
    101 		if err := i.AfterSerialization(ctx, getIctx(ctx)); err != nil {
    102 			return out, md, err
    103 		}
    104 	}
    105 
    106 	return next.HandleSerialize(ctx, in)
    107 }
    108 
    109 // InterceptBeforeRetryLoop runs BeforeRetryLoopInterceptors.
    110 type InterceptBeforeRetryLoop struct {
    111 	Interceptors []BeforeRetryLoopInterceptor
    112 }
    113 
    114 // ID identifies the middleware.
    115 func (m *InterceptBeforeRetryLoop) ID() string {
    116 	return "InterceptBeforeRetryLoop"
    117 }
    118 
    119 // HandleFinalize runs the interceptors.
    120 func (m *InterceptBeforeRetryLoop) HandleFinalize(
    121 	ctx context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler,
    122 ) (
    123 	out middleware.FinalizeOutput, md middleware.Metadata, err error,
    124 ) {
    125 	for _, i := range m.Interceptors {
    126 		if err := i.BeforeRetryLoop(ctx, getIctx(ctx)); err != nil {
    127 			return out, md, err
    128 		}
    129 	}
    130 
    131 	return next.HandleFinalize(ctx, in)
    132 }
    133 
    134 // InterceptBeforeSigning runs BeforeSigningInterceptors.
    135 type InterceptBeforeSigning struct {
    136 	Interceptors []BeforeSigningInterceptor
    137 }
    138 
    139 // ID identifies the middleware.
    140 func (m *InterceptBeforeSigning) ID() string {
    141 	return "InterceptBeforeSigning"
    142 }
    143 
    144 // HandleFinalize runs the interceptors.
    145 func (m *InterceptBeforeSigning) HandleFinalize(
    146 	ctx context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler,
    147 ) (
    148 	out middleware.FinalizeOutput, md middleware.Metadata, err error,
    149 ) {
    150 	for _, i := range m.Interceptors {
    151 		if err := i.BeforeSigning(ctx, getIctx(ctx)); err != nil {
    152 			return out, md, err
    153 		}
    154 	}
    155 
    156 	return next.HandleFinalize(ctx, in)
    157 }
    158 
    159 // InterceptAfterSigning runs AfterSigningInterceptors.
    160 type InterceptAfterSigning struct {
    161 	Interceptors []AfterSigningInterceptor
    162 }
    163 
    164 // ID identifies the middleware.
    165 func (m *InterceptAfterSigning) ID() string {
    166 	return "InterceptAfterSigning"
    167 }
    168 
    169 // HandleFinalize runs the interceptors.
    170 func (m *InterceptAfterSigning) HandleFinalize(
    171 	ctx context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler,
    172 ) (
    173 	out middleware.FinalizeOutput, md middleware.Metadata, err error,
    174 ) {
    175 	for _, i := range m.Interceptors {
    176 		if err := i.AfterSigning(ctx, getIctx(ctx)); err != nil {
    177 			return out, md, err
    178 		}
    179 	}
    180 
    181 	return next.HandleFinalize(ctx, in)
    182 }
    183 
    184 // InterceptTransmit runs BeforeTransmitInterceptors and AfterTransmitInterceptors.
    185 type InterceptTransmit struct {
    186 	BeforeTransmit []BeforeTransmitInterceptor
    187 	AfterTransmit  []AfterTransmitInterceptor
    188 }
    189 
    190 // ID identifies the middleware.
    191 func (m *InterceptTransmit) ID() string {
    192 	return "InterceptTransmit"
    193 }
    194 
    195 // HandleDeserialize runs the interceptors.
    196 func (m *InterceptTransmit) HandleDeserialize(
    197 	ctx context.Context, in middleware.DeserializeInput, next middleware.DeserializeHandler,
    198 ) (
    199 	out middleware.DeserializeOutput, md middleware.Metadata, err error,
    200 ) {
    201 	for _, i := range m.BeforeTransmit {
    202 		if err := i.BeforeTransmit(ctx, getIctx(ctx)); err != nil {
    203 			return out, md, err
    204 		}
    205 	}
    206 
    207 	out, md, err = next.HandleDeserialize(ctx, in)
    208 	if err != nil {
    209 		return out, md, err
    210 	}
    211 
    212 	// the root of the decorated middleware guarantees this will be here
    213 	// (client.go: ClientHandler.Handle)
    214 	getIctx(ctx).Response = out.RawResponse.(*Response)
    215 
    216 	for _, i := range m.AfterTransmit {
    217 		if err := i.AfterTransmit(ctx, getIctx(ctx)); err != nil {
    218 			return out, md, err
    219 		}
    220 	}
    221 
    222 	return out, md, err
    223 }
    224 
    225 // InterceptBeforeDeserialization runs BeforeDeserializationInterceptors.
    226 type InterceptBeforeDeserialization struct {
    227 	Interceptors []BeforeDeserializationInterceptor
    228 }
    229 
    230 // ID identifies the middleware.
    231 func (m *InterceptBeforeDeserialization) ID() string {
    232 	return "InterceptBeforeDeserialization"
    233 }
    234 
    235 // HandleDeserialize runs the interceptors.
    236 func (m *InterceptBeforeDeserialization) HandleDeserialize(
    237 	ctx context.Context, in middleware.DeserializeInput, next middleware.DeserializeHandler,
    238 ) (
    239 	out middleware.DeserializeOutput, md middleware.Metadata, err error,
    240 ) {
    241 	out, md, err = next.HandleDeserialize(ctx, in)
    242 	if err != nil {
    243 		var terr *RequestSendError
    244 		if errors.As(err, &terr) {
    245 			return out, md, err
    246 		}
    247 	}
    248 
    249 	for _, i := range m.Interceptors {
    250 		if err := i.BeforeDeserialization(ctx, getIctx(ctx)); err != nil {
    251 			return out, md, err
    252 		}
    253 	}
    254 
    255 	return out, md, err
    256 }
    257 
    258 // InterceptAfterDeserialization runs AfterDeserializationInterceptors.
    259 type InterceptAfterDeserialization struct {
    260 	Interceptors []AfterDeserializationInterceptor
    261 }
    262 
    263 // ID identifies the middleware.
    264 func (m *InterceptAfterDeserialization) ID() string {
    265 	return "InterceptAfterDeserialization"
    266 }
    267 
    268 // HandleDeserialize runs the interceptors.
    269 func (m *InterceptAfterDeserialization) HandleDeserialize(
    270 	ctx context.Context, in middleware.DeserializeInput, next middleware.DeserializeHandler,
    271 ) (
    272 	out middleware.DeserializeOutput, md middleware.Metadata, err error,
    273 ) {
    274 	out, md, err = next.HandleDeserialize(ctx, in)
    275 	if err != nil {
    276 		var terr *RequestSendError
    277 		if errors.As(err, &terr) {
    278 			return out, md, err
    279 		}
    280 	}
    281 
    282 	getIctx(ctx).Output = out.Result
    283 
    284 	for _, i := range m.Interceptors {
    285 		if err := i.AfterDeserialization(ctx, getIctx(ctx)); err != nil {
    286 			return out, md, err
    287 		}
    288 	}
    289 
    290 	return out, md, err
    291 }
    292 
    293 // InterceptAttempt runs AfterAttemptInterceptors.
    294 type InterceptAttempt struct {
    295 	BeforeAttempt []BeforeAttemptInterceptor
    296 	AfterAttempt  []AfterAttemptInterceptor
    297 }
    298 
    299 // ID identifies the middleware.
    300 func (m *InterceptAttempt) ID() string {
    301 	return "InterceptAttempt"
    302 }
    303 
    304 // HandleFinalize runs the interceptors.
    305 func (m *InterceptAttempt) HandleFinalize(
    306 	ctx context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler,
    307 ) (
    308 	out middleware.FinalizeOutput, md middleware.Metadata, err error,
    309 ) {
    310 	for _, i := range m.BeforeAttempt {
    311 		if err := i.BeforeAttempt(ctx, getIctx(ctx)); err != nil {
    312 			return out, md, err
    313 		}
    314 	}
    315 
    316 	out, md, err = next.HandleFinalize(ctx, in)
    317 
    318 	for _, i := range m.AfterAttempt {
    319 		if err := i.AfterAttempt(ctx, getIctx(ctx)); err != nil {
    320 			return out, md, err
    321 		}
    322 	}
    323 
    324 	return out, md, err
    325 }