client.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package client
  2. import (
  3. "fmt"
  4. "github.com/aws/aws-sdk-go/aws"
  5. "github.com/aws/aws-sdk-go/aws/client/metadata"
  6. "github.com/aws/aws-sdk-go/aws/request"
  7. )
  8. // A Config provides configuration to a service client instance.
  9. type Config struct {
  10. Config *aws.Config
  11. Handlers request.Handlers
  12. Endpoint string
  13. SigningRegion string
  14. SigningName string
  15. // States that the signing name did not come from a modeled source but
  16. // was derived based on other data. Used by service client constructors
  17. // to determine if the signin name can be overridden based on metadata the
  18. // service has.
  19. SigningNameDerived bool
  20. }
  21. // ConfigProvider provides a generic way for a service client to receive
  22. // the ClientConfig without circular dependencies.
  23. type ConfigProvider interface {
  24. ClientConfig(serviceName string, cfgs ...*aws.Config) Config
  25. }
  26. // ConfigNoResolveEndpointProvider same as ConfigProvider except it will not
  27. // resolve the endpoint automatically. The service client's endpoint must be
  28. // provided via the aws.Config.Endpoint field.
  29. type ConfigNoResolveEndpointProvider interface {
  30. ClientConfigNoResolveEndpoint(cfgs ...*aws.Config) Config
  31. }
  32. // A Client implements the base client request and response handling
  33. // used by all service clients.
  34. type Client struct {
  35. request.Retryer
  36. metadata.ClientInfo
  37. Config aws.Config
  38. Handlers request.Handlers
  39. }
  40. // New will return a pointer to a new initialized service client.
  41. func New(cfg aws.Config, info metadata.ClientInfo, handlers request.Handlers, options ...func(*Client)) *Client {
  42. svc := &Client{
  43. Config: cfg,
  44. ClientInfo: info,
  45. Handlers: handlers.Copy(),
  46. }
  47. switch retryer, ok := cfg.Retryer.(request.Retryer); {
  48. case ok:
  49. svc.Retryer = retryer
  50. case cfg.Retryer != nil && cfg.Logger != nil:
  51. s := fmt.Sprintf("WARNING: %T does not implement request.Retryer; using DefaultRetryer instead", cfg.Retryer)
  52. cfg.Logger.Log(s)
  53. fallthrough
  54. default:
  55. maxRetries := aws.IntValue(cfg.MaxRetries)
  56. if cfg.MaxRetries == nil || maxRetries == aws.UseServiceDefaultRetries {
  57. maxRetries = 3
  58. }
  59. svc.Retryer = DefaultRetryer{NumMaxRetries: maxRetries}
  60. }
  61. svc.AddDebugHandlers()
  62. for _, option := range options {
  63. option(svc)
  64. }
  65. return svc
  66. }
  67. // NewRequest returns a new Request pointer for the service API
  68. // operation and parameters.
  69. func (c *Client) NewRequest(operation *request.Operation, params interface{}, data interface{}) *request.Request {
  70. return request.New(c.Config, c.ClientInfo, c.Handlers, c.Retryer, operation, params, data)
  71. }
  72. // AddDebugHandlers injects debug logging handlers into the service to log request
  73. // debug information.
  74. func (c *Client) AddDebugHandlers() {
  75. if !c.Config.LogLevel.AtLeast(aws.LogDebug) {
  76. return
  77. }
  78. c.Handlers.Send.PushFrontNamed(LogHTTPRequestHandler)
  79. c.Handlers.Send.PushBackNamed(LogHTTPResponseHandler)
  80. }