handlers.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package aws
  2. // A Handlers provides a collection of request handlers for various
  3. // stages of handling requests.
  4. type Handlers struct {
  5. Validate HandlerList
  6. Build HandlerList
  7. Sign HandlerList
  8. Send HandlerList
  9. ValidateResponse HandlerList
  10. Unmarshal HandlerList
  11. UnmarshalMeta HandlerList
  12. UnmarshalError HandlerList
  13. Retry HandlerList
  14. AfterRetry HandlerList
  15. CheckCrc64 HandlerList
  16. }
  17. // copy returns of this handler's lists.
  18. func (h *Handlers) copy() Handlers {
  19. return Handlers{
  20. Validate: h.Validate.copy(),
  21. Build: h.Build.copy(),
  22. Sign: h.Sign.copy(),
  23. Send: h.Send.copy(),
  24. ValidateResponse: h.ValidateResponse.copy(),
  25. Unmarshal: h.Unmarshal.copy(),
  26. UnmarshalError: h.UnmarshalError.copy(),
  27. UnmarshalMeta: h.UnmarshalMeta.copy(),
  28. Retry: h.Retry.copy(),
  29. AfterRetry: h.AfterRetry.copy(),
  30. CheckCrc64: h.CheckCrc64.copy(),
  31. }
  32. }
  33. // Clear removes callback functions for all handlers
  34. func (h *Handlers) Clear() {
  35. h.Validate.Clear()
  36. h.Build.Clear()
  37. h.Send.Clear()
  38. h.Sign.Clear()
  39. h.Unmarshal.Clear()
  40. h.UnmarshalMeta.Clear()
  41. h.UnmarshalError.Clear()
  42. h.ValidateResponse.Clear()
  43. h.Retry.Clear()
  44. h.AfterRetry.Clear()
  45. h.CheckCrc64.Clear()
  46. }
  47. // A HandlerList manages zero or more handlers in a list.
  48. type HandlerList struct {
  49. list []func(*Request)
  50. }
  51. // copy creates a copy of the handler list.
  52. func (l *HandlerList) copy() HandlerList {
  53. var n HandlerList
  54. n.list = append([]func(*Request){}, l.list...)
  55. return n
  56. }
  57. // Clear clears the handler list.
  58. func (l *HandlerList) Clear() {
  59. l.list = []func(*Request){}
  60. }
  61. // Len returns the number of handlers in the list.
  62. func (l *HandlerList) Len() int {
  63. return len(l.list)
  64. }
  65. // PushBack pushes handlers f to the back of the handler list.
  66. func (l *HandlerList) PushBack(f ...func(*Request)) {
  67. l.list = append(l.list, f...)
  68. }
  69. // PushFront pushes handlers f to the front of the handler list.
  70. func (l *HandlerList) PushFront(f ...func(*Request)) {
  71. l.list = append(f, l.list...)
  72. }
  73. // Run executes all handlers in the list with a given request object.
  74. func (l *HandlerList) Run(r *Request) {
  75. for _, f := range l.list {
  76. f(r)
  77. }
  78. }