Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

269 Zeilen
6.0 KiB

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