service.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. package service
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "net/http"
  9. "net/url"
  10. "reflect"
  11. "runtime"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "github.com/alibabacloud-go/tea/tea"
  16. )
  17. var defaultUserAgent = fmt.Sprintf("AlibabaCloud (%s; %s) Golang/%s Core/%s TeaDSL/1", runtime.GOOS, runtime.GOARCH, strings.Trim(runtime.Version(), "go"), "0.01")
  18. type RuntimeOptions struct {
  19. Autoretry *bool `json:"autoretry" xml:"autoretry"`
  20. IgnoreSSL *bool `json:"ignoreSSL" xml:"ignoreSSL"`
  21. MaxAttempts *int `json:"maxAttempts" xml:"maxAttempts"`
  22. BackoffPolicy *string `json:"backoffPolicy" xml:"backoffPolicy"`
  23. BackoffPeriod *int `json:"backoffPeriod" xml:"backoffPeriod"`
  24. ReadTimeout *int `json:"readTimeout" xml:"readTimeout"`
  25. ConnectTimeout *int `json:"connectTimeout" xml:"connectTimeout"`
  26. LocalAddr *string `json:"localAddr" xml:"localAddr"`
  27. HttpProxy *string `json:"httpProxy" xml:"httpProxy"`
  28. HttpsProxy *string `json:"httpsProxy" xml:"httpsProxy"`
  29. NoProxy *string `json:"noProxy" xml:"noProxy"`
  30. MaxIdleConns *int `json:"maxIdleConns" xml:"maxIdleConns"`
  31. Socks5Proxy *string `json:"socks5Proxy" xml:"socks5Proxy"`
  32. Socks5NetWork *string `json:"socks5NetWork" xml:"socks5NetWork"`
  33. }
  34. func (s RuntimeOptions) String() string {
  35. return tea.Prettify(s)
  36. }
  37. func (s RuntimeOptions) GoString() string {
  38. return s.String()
  39. }
  40. func (s *RuntimeOptions) SetAutoretry(v bool) *RuntimeOptions {
  41. s.Autoretry = &v
  42. return s
  43. }
  44. func (s *RuntimeOptions) SetIgnoreSSL(v bool) *RuntimeOptions {
  45. s.IgnoreSSL = &v
  46. return s
  47. }
  48. func (s *RuntimeOptions) SetMaxAttempts(v int) *RuntimeOptions {
  49. s.MaxAttempts = &v
  50. return s
  51. }
  52. func (s *RuntimeOptions) SetBackoffPolicy(v string) *RuntimeOptions {
  53. s.BackoffPolicy = &v
  54. return s
  55. }
  56. func (s *RuntimeOptions) SetBackoffPeriod(v int) *RuntimeOptions {
  57. s.BackoffPeriod = &v
  58. return s
  59. }
  60. func (s *RuntimeOptions) SetReadTimeout(v int) *RuntimeOptions {
  61. s.ReadTimeout = &v
  62. return s
  63. }
  64. func (s *RuntimeOptions) SetConnectTimeout(v int) *RuntimeOptions {
  65. s.ConnectTimeout = &v
  66. return s
  67. }
  68. func (s *RuntimeOptions) SetHttpProxy(v string) *RuntimeOptions {
  69. s.HttpProxy = &v
  70. return s
  71. }
  72. func (s *RuntimeOptions) SetHttpsProxy(v string) *RuntimeOptions {
  73. s.HttpsProxy = &v
  74. return s
  75. }
  76. func (s *RuntimeOptions) SetNoProxy(v string) *RuntimeOptions {
  77. s.NoProxy = &v
  78. return s
  79. }
  80. func (s *RuntimeOptions) SetMaxIdleConns(v int) *RuntimeOptions {
  81. s.MaxIdleConns = &v
  82. return s
  83. }
  84. func (s *RuntimeOptions) SetLocalAddr(v string) *RuntimeOptions {
  85. s.LocalAddr = &v
  86. return s
  87. }
  88. func (s *RuntimeOptions) SetSocks5Proxy(v string) *RuntimeOptions {
  89. s.Socks5Proxy = &v
  90. return s
  91. }
  92. func (s *RuntimeOptions) SetSocks5NetWork(v string) *RuntimeOptions {
  93. s.Socks5NetWork = &v
  94. return s
  95. }
  96. func ReadAsString(body io.Reader) (*string, error) {
  97. byt, err := ioutil.ReadAll(body)
  98. if err != nil {
  99. return tea.String(""), err
  100. }
  101. r, ok := body.(io.ReadCloser)
  102. if ok {
  103. r.Close()
  104. }
  105. return tea.String(string(byt)), nil
  106. }
  107. func StringifyMapValue(a map[string]interface{}) map[string]*string {
  108. res := make(map[string]*string)
  109. for key, value := range a {
  110. if value != nil {
  111. switch value.(type) {
  112. case string:
  113. res[key] = tea.String(value.(string))
  114. default:
  115. byt, _ := json.Marshal(value)
  116. res[key] = tea.String(string(byt))
  117. }
  118. }
  119. }
  120. return res
  121. }
  122. func AnyifyMapValue(a map[string]*string) map[string]interface{} {
  123. res := make(map[string]interface{})
  124. for key, value := range a {
  125. res[key] = tea.StringValue(value)
  126. }
  127. return res
  128. }
  129. func ReadAsBytes(body io.Reader) ([]byte, error) {
  130. byt, err := ioutil.ReadAll(body)
  131. if err != nil {
  132. return nil, err
  133. }
  134. r, ok := body.(io.ReadCloser)
  135. if ok {
  136. r.Close()
  137. }
  138. return byt, nil
  139. }
  140. func DefaultString(reaStr, defaultStr *string) *string {
  141. if reaStr == nil {
  142. return defaultStr
  143. }
  144. return reaStr
  145. }
  146. func ToJSONString(a interface{}) *string {
  147. byt, _ := json.Marshal(a)
  148. return tea.String(string(byt))
  149. }
  150. func DefaultNumber(reaNum, defaultNum *int) *int {
  151. if reaNum == nil {
  152. return defaultNum
  153. }
  154. return reaNum
  155. }
  156. func ReadAsJSON(body io.Reader) (result interface{}, err error) {
  157. byt, err := ioutil.ReadAll(body)
  158. if err != nil {
  159. return
  160. }
  161. if string(byt) == "" {
  162. return
  163. }
  164. r, ok := body.(io.ReadCloser)
  165. if ok {
  166. r.Close()
  167. }
  168. d := json.NewDecoder(bytes.NewReader(byt))
  169. d.UseNumber()
  170. err = d.Decode(&result)
  171. return
  172. }
  173. func GetNonce() *string {
  174. return tea.String(getUUID())
  175. }
  176. func Empty(val *string) *bool {
  177. return tea.Bool(val == nil || tea.StringValue(val) == "")
  178. }
  179. func ValidateModel(a interface{}) error {
  180. if a == nil {
  181. return nil
  182. }
  183. err := tea.Validate(a)
  184. return err
  185. }
  186. func EqualString(val1, val2 *string) *bool {
  187. return tea.Bool(tea.StringValue(val1) == tea.StringValue(val2))
  188. }
  189. func EqualNumber(val1, val2 *int) *bool {
  190. return tea.Bool(tea.IntValue(val1) == tea.IntValue(val2))
  191. }
  192. func IsUnset(val interface{}) *bool {
  193. if val == nil {
  194. return tea.Bool(true)
  195. }
  196. v := reflect.ValueOf(val)
  197. if v.Kind() == reflect.Ptr || v.Kind() == reflect.Slice || v.Kind() == reflect.Map {
  198. return tea.Bool(v.IsNil())
  199. }
  200. valType := reflect.TypeOf(val)
  201. valZero := reflect.Zero(valType)
  202. return tea.Bool(valZero == v)
  203. }
  204. func ToBytes(a *string) []byte {
  205. return []byte(tea.StringValue(a))
  206. }
  207. func AssertAsMap(a interface{}) map[string]interface{} {
  208. r := reflect.ValueOf(a)
  209. if r.Kind().String() != "map" {
  210. panic(fmt.Sprintf("%v is not a map[string]interface{}", a))
  211. }
  212. res := make(map[string]interface{})
  213. tmp := r.MapKeys()
  214. for _, key := range tmp {
  215. res[key.String()] = r.MapIndex(key).Interface()
  216. }
  217. return res
  218. }
  219. func AssertAsNumber(a interface{}) *int {
  220. res := 0
  221. switch a.(type) {
  222. case int:
  223. tmp := a.(int)
  224. res = tmp
  225. case *int:
  226. tmp := a.(*int)
  227. res = tea.IntValue(tmp)
  228. default:
  229. panic(fmt.Sprintf("%v is not a int", a))
  230. }
  231. return tea.Int(res)
  232. }
  233. func AssertAsBoolean(a interface{}) *bool {
  234. res := false
  235. switch a.(type) {
  236. case bool:
  237. tmp := a.(bool)
  238. res = tmp
  239. case *bool:
  240. tmp := a.(*bool)
  241. res = tea.BoolValue(tmp)
  242. default:
  243. panic(fmt.Sprintf("%v is not a bool", a))
  244. }
  245. return tea.Bool(res)
  246. }
  247. func AssertAsString(a interface{}) *string {
  248. res := ""
  249. switch a.(type) {
  250. case string:
  251. tmp := a.(string)
  252. res = tmp
  253. case *string:
  254. tmp := a.(*string)
  255. res = tea.StringValue(tmp)
  256. default:
  257. panic(fmt.Sprintf("%v is not a string", a))
  258. }
  259. return tea.String(res)
  260. }
  261. func AssertAsBytes(a interface{}) []byte {
  262. res, ok := a.([]byte)
  263. if !ok {
  264. panic(fmt.Sprintf("%v is not []byte", a))
  265. }
  266. return res
  267. }
  268. func AssertAsReadable(a interface{}) io.Reader {
  269. res, ok := a.(io.Reader)
  270. if !ok {
  271. panic(fmt.Sprintf("%v is not reader", a))
  272. }
  273. return res
  274. }
  275. func AssertAsArray(a interface{}) []interface{} {
  276. r := reflect.ValueOf(a)
  277. if r.Kind().String() != "array" && r.Kind().String() != "slice" {
  278. panic(fmt.Sprintf("%v is not a [x]interface{}", a))
  279. }
  280. aLen := r.Len()
  281. res := make([]interface{}, 0)
  282. for i := 0; i < aLen; i++ {
  283. res = append(res, r.Index(i).Interface())
  284. }
  285. return res
  286. }
  287. func ParseJSON(a *string) interface{} {
  288. mapTmp := make(map[string]interface{})
  289. d := json.NewDecoder(bytes.NewReader([]byte(tea.StringValue(a))))
  290. d.UseNumber()
  291. err := d.Decode(&mapTmp)
  292. if err == nil {
  293. return mapTmp
  294. }
  295. sliceTmp := make([]interface{}, 0)
  296. d = json.NewDecoder(bytes.NewReader([]byte(tea.StringValue(a))))
  297. d.UseNumber()
  298. err = d.Decode(&sliceTmp)
  299. if err == nil {
  300. return sliceTmp
  301. }
  302. if num, err := strconv.Atoi(tea.StringValue(a)); err == nil {
  303. return num
  304. }
  305. if ok, err := strconv.ParseBool(tea.StringValue(a)); err == nil {
  306. return ok
  307. }
  308. if floa64tVal, err := strconv.ParseFloat(tea.StringValue(a), 64); err == nil {
  309. return floa64tVal
  310. }
  311. return nil
  312. }
  313. func ToString(a []byte) *string {
  314. return tea.String(string(a))
  315. }
  316. func ToMap(in interface{}) map[string]interface{} {
  317. if in == nil {
  318. return nil
  319. }
  320. res := tea.ToMap(in)
  321. return res
  322. }
  323. func ToFormString(a map[string]interface{}) *string {
  324. if a == nil {
  325. return tea.String("")
  326. }
  327. res := ""
  328. urlEncoder := url.Values{}
  329. for key, value := range a {
  330. v := fmt.Sprintf("%v", value)
  331. urlEncoder.Add(key, v)
  332. }
  333. res = urlEncoder.Encode()
  334. return tea.String(res)
  335. }
  336. func GetDateUTCString() *string {
  337. return tea.String(time.Now().UTC().Format(http.TimeFormat))
  338. }
  339. func GetUserAgent(userAgent *string) *string {
  340. if userAgent != nil && tea.StringValue(userAgent) != "" {
  341. return tea.String(defaultUserAgent + " " + tea.StringValue(userAgent))
  342. }
  343. return tea.String(defaultUserAgent)
  344. }
  345. func Is2xx(code *int) *bool {
  346. tmp := tea.IntValue(code)
  347. return tea.Bool(tmp >= 200 && tmp < 300)
  348. }
  349. func Is3xx(code *int) *bool {
  350. tmp := tea.IntValue(code)
  351. return tea.Bool(tmp >= 300 && tmp < 400)
  352. }
  353. func Is4xx(code *int) *bool {
  354. tmp := tea.IntValue(code)
  355. return tea.Bool(tmp >= 400 && tmp < 500)
  356. }
  357. func Is5xx(code *int) *bool {
  358. tmp := tea.IntValue(code)
  359. return tea.Bool(tmp >= 500 && tmp < 600)
  360. }
  361. func Sleep(millisecond *int) error {
  362. ms := tea.IntValue(millisecond)
  363. time.Sleep(time.Duration(ms) * time.Millisecond)
  364. return nil
  365. }
  366. func ToArray(in interface{}) []map[string]interface{} {
  367. if tea.BoolValue(IsUnset(in)) {
  368. return nil
  369. }
  370. tmp := make([]map[string]interface{}, 0)
  371. byt, _ := json.Marshal(in)
  372. d := json.NewDecoder(bytes.NewReader(byt))
  373. d.UseNumber()
  374. err := d.Decode(&tmp)
  375. if err != nil {
  376. return nil
  377. }
  378. return tmp
  379. }