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.

281 lines
6.3 KiB

  1. package requests
  2. import (
  3. "errors"
  4. "fmt"
  5. "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/utils"
  6. "io"
  7. "math/cmplx"
  8. "reflect"
  9. "strconv"
  10. "time"
  11. )
  12. const (
  13. RPC = "RPC"
  14. ROA = "ROA"
  15. HTTP = "HTTP"
  16. HTTPS = "HTTPS"
  17. JSON = "JSON"
  18. XML = "XML"
  19. DefaultHttpPort = "80"
  20. GET = "GET"
  21. PUT = "PUT"
  22. POST = "POST"
  23. DELETE = "DELETE"
  24. PATCH = "PATCH"
  25. HEAD = "HEAD"
  26. OPTIONS = "OPTIONS"
  27. Json = "application/json"
  28. Xml = "application/xml"
  29. Raw = "application/octet-stream"
  30. Form = "application/x-www-form-urlencoded"
  31. Header = "Header"
  32. Query = "Query"
  33. Body = "Body"
  34. Path = "Path"
  35. HeaderSeparator = "\n"
  36. )
  37. var debug utils.Debug
  38. func init() {
  39. debug = utils.Init("request")
  40. }
  41. type AcsRequest interface {
  42. GetReadTimeout() time.Duration
  43. GetConnectTimeout() time.Duration
  44. SetReadTimeout(readTimeOut time.Duration)
  45. SetConnectTimeout(connectTimeOut time.Duration)
  46. SetHTTPSInsecure(isInsecure bool)
  47. GetHTTPSInsecure() *bool
  48. GetQueryParams() map[string]string
  49. GetFormParams() map[string]string
  50. GetMethod() string
  51. GetScheme() string
  52. GetDomain() string
  53. GetActionName() string
  54. GetAcceptFormat() string
  55. GetHeaders() map[string]string
  56. BuildUrl() string
  57. BuildQueries() string
  58. SetScheme(scheme string)
  59. SetContent(content []byte)
  60. SetDomain(host string)
  61. SetStringToSign(stringToSign string)
  62. GetStringToSign() string
  63. GetBodyReader() io.Reader
  64. addHeaderParam(key, value string)
  65. addQueryParam(key, value string)
  66. addFormParam(key, value string)
  67. }
  68. type baseRequest struct {
  69. Scheme string
  70. Method string
  71. Port string
  72. Domain string
  73. From string
  74. ReadTimeout time.Duration
  75. ConnectTimeout time.Duration
  76. isInsecure *bool
  77. AcceptFormat string
  78. actionName string
  79. userAgent map[string]string
  80. product string
  81. version string
  82. QueryParams map[string]string
  83. Headers map[string]string
  84. FormParams map[string]string
  85. Content []byte
  86. queries string
  87. stringToSign string
  88. }
  89. func (request *baseRequest) GetStringToSign() string {
  90. return request.stringToSign
  91. }
  92. func (request *baseRequest) SetContent(content []byte) {
  93. request.Content = content
  94. }
  95. func (request *baseRequest) GetAcceptFormat() string {
  96. return request.AcceptFormat
  97. }
  98. func (request *baseRequest) GetHeaders() map[string]string {
  99. return request.Headers
  100. }
  101. func (request *baseRequest) GetActionName() string {
  102. return request.actionName
  103. }
  104. func (request *baseRequest) SetScheme(scheme string) {
  105. request.Scheme = scheme
  106. }
  107. func (request *baseRequest) SetDomain(host string) {
  108. request.Domain = host
  109. }
  110. func (request *baseRequest) GetScheme() string {
  111. return request.Scheme
  112. }
  113. func (request *baseRequest) GetDomain() string {
  114. return request.Domain
  115. }
  116. func (request *baseRequest) GetMethod() string {
  117. return request.Method
  118. }
  119. func (request *baseRequest) GetFormParams() map[string]string {
  120. return request.FormParams
  121. }
  122. func (request *baseRequest) GetQueryParams() map[string]string {
  123. return request.QueryParams
  124. }
  125. func (request *baseRequest) SetHTTPSInsecure(isInsecure bool) {
  126. request.isInsecure = &isInsecure
  127. }
  128. func (request *baseRequest) GetHTTPSInsecure() *bool {
  129. return request.isInsecure
  130. }
  131. func (request *baseRequest) GetReadTimeout() time.Duration {
  132. return request.ReadTimeout
  133. }
  134. func (request *baseRequest) GetConnectTimeout() time.Duration {
  135. return request.ConnectTimeout
  136. }
  137. func (request *baseRequest) SetReadTimeout(readTimeOut time.Duration) {
  138. request.ReadTimeout = readTimeOut
  139. }
  140. func (request *baseRequest) SetConnectTimeout(connectTimeOut time.Duration) {
  141. request.ConnectTimeout = connectTimeOut
  142. }
  143. func (request *baseRequest) SetStringToSign(stringToSign string) {
  144. request.stringToSign = stringToSign
  145. }
  146. func (request *baseRequest) addHeaderParam(key, val string) {
  147. request.Headers[key] = val
  148. }
  149. func (request *baseRequest) addQueryParam(key, val string) {
  150. request.QueryParams[key] = val
  151. }
  152. func (request *baseRequest) addFormParam(key, val string) {
  153. request.FormParams[key] = val
  154. }
  155. func defaultBaseRequest() (request *baseRequest) {
  156. request = &baseRequest{
  157. Scheme: HTTP,
  158. AcceptFormat: JSON,
  159. Method: GET,
  160. QueryParams: make(map[string]string),
  161. Headers: map[string]string{
  162. "gr-sdk-client": "golang/1.14",
  163. "gr-sdk-invoke-type": "normal",
  164. "Accept-Encoding": Json,
  165. },
  166. FormParams: make(map[string]string),
  167. }
  168. return
  169. }
  170. func InitParam(request AcsRequest) (err error) {
  171. reflectValue := reflect.ValueOf(request).Elem()
  172. err = flatRepeatedList(reflectValue, request, "")
  173. return nil
  174. }
  175. func flatRepeatedList(reflectValue reflect.Value, request AcsRequest, position string) (err error) {
  176. reflectType := reflectValue.Type()
  177. for i := 0; i < reflectType.NumField(); i++ {
  178. field := reflectType.Field(i)
  179. name, isContiansNameTag := field.Tag.Lookup("field")
  180. fieldPosition := position
  181. if fieldPosition == "" {
  182. fieldPosition, _ = field.Tag.Lookup("position")
  183. }
  184. fieldDefault, _ := field.Tag.Lookup("default")
  185. debug("%s %s %s", name, fieldPosition, fieldDefault)
  186. if isContiansNameTag {
  187. var value string
  188. switch field.Type.Kind() {
  189. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  190. value = strconv.FormatInt(reflectValue.Field(i).Int(), 10)
  191. case reflect.Uintptr, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  192. value = strconv.FormatUint(reflectValue.Field(i).Uint(), 10)
  193. case reflect.Float32, reflect.Float64:
  194. value = strconv.FormatFloat(reflectValue.Field(i).Float(), 'E', -1, 64)
  195. case reflect.Bool:
  196. value = strconv.FormatBool(reflectValue.Field(i).Bool())
  197. case reflect.Complex64, reflect.Complex128:
  198. value = fmt.Sprint(cmplx.Sqrt(reflectValue.Field(i).Complex()))
  199. default:
  200. value = reflectValue.Field(i).String()
  201. }
  202. if len(value) == 0 {
  203. value = fieldDefault
  204. }
  205. if value == "0" && fieldDefault != "" && fieldDefault != "0" {
  206. value = fieldDefault
  207. }
  208. err = addParam(request, fieldPosition, name, value)
  209. }
  210. }
  211. return
  212. }
  213. func addParam(request AcsRequest, position, key, value string) (err error) {
  214. if len(value) > 0 {
  215. switch position {
  216. case Header:
  217. request.addHeaderParam(key, value)
  218. case Query:
  219. request.addQueryParam(key, value)
  220. case Body:
  221. request.addFormParam(key, value)
  222. default:
  223. errmsg := fmt.Sprintf("unsupport positions add param `%s`", position)
  224. err = errors.New(errmsg)
  225. }
  226. }
  227. return
  228. }