service.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package aws
  2. import (
  3. "github.com/ks3sdklib/aws-sdk-go/aws/retry"
  4. "github.com/ks3sdklib/aws-sdk-go/internal/endpoints"
  5. "net/http"
  6. "net/http/httputil"
  7. "regexp"
  8. )
  9. // A Service implements the base service request and response handling
  10. // used by all services.
  11. type Service struct {
  12. Config *Config
  13. Handlers Handlers
  14. ManualSend bool
  15. ServiceName string
  16. APIVersion string
  17. Endpoint string
  18. SigningName string
  19. SigningRegion string
  20. JSONVersion string
  21. TargetPrefix string
  22. RetryRule retry.RetryRule
  23. ShouldRetry func(error) bool
  24. MaxRetries int
  25. }
  26. var schemeRE = regexp.MustCompile("^([^:]+)://")
  27. // NewService will return a pointer to a new Server object initialized.
  28. func NewService(config *Config) *Service {
  29. svc := &Service{Config: config}
  30. svc.Initialize()
  31. return svc
  32. }
  33. // Initialize initializes the service.
  34. func (s *Service) Initialize() {
  35. if s.Config == nil {
  36. s.Config = &Config{}
  37. }
  38. if s.Config.HTTPClient == nil {
  39. s.Config.HTTPClient = http.DefaultClient
  40. }
  41. if s.RetryRule == nil {
  42. s.RetryRule = s.Config.RetryRule
  43. }
  44. if s.ShouldRetry == nil {
  45. s.ShouldRetry = s.Config.ShouldRetry
  46. }
  47. s.MaxRetries = s.Config.MaxRetries
  48. s.Handlers.Validate.PushBack(ValidateEndpointHandler)
  49. s.Handlers.Build.PushBack(UserAgentHandler)
  50. s.Handlers.Sign.PushBack(BuildContentLength)
  51. s.Handlers.Send.PushBack(SendHandler)
  52. s.Handlers.AfterRetry.PushBack(AfterRetryHandler)
  53. s.Handlers.ValidateResponse.PushBack(ValidateResponseHandler)
  54. s.AddDebugHandlers()
  55. s.buildEndpoint()
  56. if !s.Config.DisableParamValidation {
  57. s.Handlers.Validate.PushBack(ValidateParameters)
  58. }
  59. }
  60. // buildEndpoint builds the endpoint values the service will use to make requests with.
  61. func (s *Service) buildEndpoint() {
  62. if s.Config.Endpoint != "" {
  63. s.Endpoint = s.Config.Endpoint
  64. } else {
  65. s.Endpoint, s.SigningRegion =
  66. endpoints.EndpointForRegion(s.ServiceName, s.Config.Region)
  67. }
  68. if s.Endpoint != "" && !schemeRE.MatchString(s.Endpoint) {
  69. scheme := "https"
  70. if s.Config.DisableSSL {
  71. scheme = "http"
  72. }
  73. s.Endpoint = scheme + "://" + s.Endpoint
  74. }
  75. }
  76. // AddDebugHandlers injects debug logging handlers into the service to log request
  77. // debug information.
  78. func (s *Service) AddDebugHandlers() {
  79. if s.Config.LogLevel < Debug {
  80. return
  81. }
  82. s.Handlers.Send.PushFront(func(r *Request) {
  83. logBody := r.Config.LogHTTPBody
  84. dumpedBody, _ := httputil.DumpRequestOut(r.HTTPRequest, logBody)
  85. r.Config.LogDebug("---[ REQUEST ]-----------------------------")
  86. r.Config.LogDebug("%s", string(dumpedBody))
  87. r.Config.LogDebug("-----------------------------------------------------")
  88. })
  89. s.Handlers.Send.PushBack(func(r *Request) {
  90. r.Config.LogDebug("---[ RESPONSE ]--------------------------------------")
  91. if r.HTTPResponse != nil {
  92. logBody := r.Config.LogHTTPBody
  93. dumpedBody, _ := httputil.DumpResponse(r.HTTPResponse, logBody)
  94. r.Config.LogDebug("%s", string(dumpedBody))
  95. } else if r.Error != nil {
  96. r.Config.LogDebug("%s", r.Error.Error())
  97. }
  98. r.Config.LogDebug("-----------------------------------------------------")
  99. })
  100. }