You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

155 lines
4.6 KiB

  1. package ecloudsdkcore
  2. import (
  3. "gitlab.ecloud.com/ecloud/ecloudsdkcore/auth"
  4. "gitlab.ecloud.com/ecloud/ecloudsdkcore/auth/provider"
  5. "gitlab.ecloud.com/ecloud/ecloudsdkcore/config"
  6. "gitlab.ecloud.com/ecloud/ecloudsdkcore/errs"
  7. "gitlab.ecloud.com/ecloud/ecloudsdkcore/http"
  8. "gitlab.ecloud.com/ecloud/ecloudsdkcore/param"
  9. "gitlab.ecloud.com/ecloud/ecloudsdkcore/request"
  10. "gitlab.ecloud.com/ecloud/ecloudsdkcore/response"
  11. "gitlab.ecloud.com/ecloud/ecloudsdkcore/retry"
  12. "gitlab.ecloud.com/ecloud/ecloudsdkcore/utils"
  13. )
  14. // APIClient manages communication
  15. type APIClient struct {
  16. Config *config.Config
  17. HttpClient *http.NetHttpClient
  18. HttpRequest *request.HttpRequest
  19. }
  20. // NewAPIClient creates a new API http.
  21. func NewAPIClient() *APIClient {
  22. return &APIClient{HttpClient: http.NewHttpClient()}
  23. }
  24. // NewCustomizedAPIClient creates a new customized API http.
  25. func NewCustomizedAPIClient(config *config.Config, httpRequest *request.HttpRequest) *APIClient {
  26. return &APIClient{
  27. Config: config,
  28. HttpClient: http.NewHttpClient(),
  29. HttpRequest: httpRequest,
  30. }
  31. }
  32. func DefaultApiClient(config *config.Config, httpRequest *request.HttpRequest) *APIClient {
  33. return NewCustomizedAPIClient(config, httpRequest)
  34. }
  35. // InitConfig init default configuration
  36. func InitConfig(c *config.Config) {
  37. if utils.IsUnSet(c.AutoRetry) {
  38. autoRetry := false
  39. c.AutoRetry = &autoRetry
  40. }
  41. if utils.IsUnSet(c.IgnoreSSL) {
  42. ignoreSSL := true
  43. c.IgnoreSSL = &ignoreSSL
  44. }
  45. if utils.IsUnSet(c.IgnoreGateway) {
  46. ignoreGateway := false
  47. c.IgnoreGateway = &ignoreGateway
  48. }
  49. if utils.IsUnSet(c.CentralTransportEnabled) {
  50. centralTransportEnabled := true
  51. c.CentralTransportEnabled = &centralTransportEnabled
  52. }
  53. }
  54. // Excute entry for http call
  55. func (c *APIClient) Excute(params *param.Params, rc *config.RuntimeConfig,
  56. returnType interface{}) (*response.HttpResponse, error) {
  57. httpReq := c.HttpRequest
  58. httpReq = new(request.HttpRequest)
  59. if err := utils.DeepCopy(httpReq, c.HttpRequest); err != nil {
  60. errs.NewServerRequestError("copy object error", nil)
  61. }
  62. if rc == nil {
  63. rc = &config.RuntimeConfig{}
  64. }
  65. c.resetHttpRequest(httpReq)
  66. if rc == nil {
  67. rc = &config.RuntimeConfig{}
  68. }
  69. cm := utils.Merge(c.Config, rc)
  70. if err := c.buildHttpRequest(params, cm, httpReq); err != nil {
  71. return nil, err
  72. }
  73. httpReq.BuildFinalUrl()
  74. if utils.BoolValue(cm["AutoRetry"].(*bool)) {
  75. retryTemplate := c.buildRetryTemplate(cm)
  76. res, err := retryTemplate.Call(func() (interface{}, error) {
  77. return c.HttpClient.Execute(httpReq, cm, returnType)
  78. })
  79. if err != nil {
  80. return nil, err
  81. }
  82. httpResponse, _ := res.(*response.HttpResponse)
  83. return httpResponse, nil
  84. }
  85. return c.HttpClient.Execute(httpReq, cm, returnType)
  86. }
  87. func (c *APIClient) buildRetryTemplate(cm map[string]interface{}) *retry.Template {
  88. templateBuilder := retry.NewBuilder()
  89. if utils.IsSet(cm["MaxRetryTimes"]) {
  90. templateBuilder.SetRetryTimes(utils.Int32Value(cm["MaxRetryTimes"].(*int32)))
  91. }
  92. if utils.IsSet(cm["RetryPeriod"]) {
  93. templateBuilder.SetRetryPolicy(retry.WaitRetryPolicy(utils.Int64Value((cm["RetryPeriod"].(*int64)))))
  94. } else {
  95. templateBuilder.SetRetryPolicy(retry.NoWait())
  96. }
  97. if utils.IsSet(cm["MaxDuringTime"]) {
  98. templateBuilder.SetMaxDuringTime(utils.Int64Value(cm["MaxDuringTime"].(*int64)))
  99. }
  100. return templateBuilder.Build()
  101. }
  102. func (c *APIClient) buildHttpRequest(params *param.Params, cm map[string]interface{}, httpReq *request.HttpRequest) error {
  103. httpReq.ConvertRequest(params.Request)
  104. err := httpReq.BuildApiParams(params, cm)
  105. if err != nil {
  106. return err
  107. }
  108. if utils.IsUnSet(cm["Provider"]) {
  109. if utils.IsUnSet(c.Config.AccessKey) || utils.IsUnSet(c.Config.SecretKey) {
  110. return errs.NewServerRequestError("accessKey or secretKey can not be null", nil)
  111. }
  112. credential := auth.NewCredentialBuilder().AccessKey(*c.Config.AccessKey).SecretKey(*c.Config.SecretKey).Build()
  113. err := auth.GetCredentialManager(*credential.CredentialType).Sign(httpReq, credential)
  114. if err != nil {
  115. return err
  116. }
  117. return nil
  118. }
  119. p := cm["Provider"].(provider.ICredentialProvider)
  120. credential, err := p.GetCredential()
  121. if err != nil {
  122. return err
  123. }
  124. if utils.IsSet(c.Config.Provider) && p != c.Config.Provider {
  125. otherCredential, err := c.Config.Provider.GetCredential()
  126. if err != nil {
  127. return err
  128. }
  129. credential.CopyValues(otherCredential)
  130. }
  131. if utils.IsUnSet(credential.AccessKey) || utils.IsUnSet(credential.SecretKey) {
  132. credential.AccessKey = (c.Config.AccessKey)
  133. credential.SecretKey = (c.Config.SecretKey)
  134. }
  135. err = auth.GetCredentialManager(*credential.CredentialType).Sign(httpReq, credential)
  136. if err != nil {
  137. return err
  138. }
  139. return nil
  140. }
  141. func (c *APIClient) resetHttpRequest(req *request.HttpRequest) {
  142. req.Reset()
  143. }