httpparams.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. Copyright 1999-2017 Alibaba Group Holding Ltd.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. CSB-HTTP-SDK based on GO language.
  13. */
  14. package csbhttp
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "strings"
  19. )
  20. /**
  21. 构造Http请求参数的struct
  22. */
  23. const (
  24. JSON = "application/json"
  25. BINARY = "application/octet-stream"
  26. )
  27. type contentBody struct {
  28. jsonBody string
  29. bytesBody []byte
  30. contentType string
  31. }
  32. type (
  33. HttpParams struct {
  34. method string // "设置方法: get, post"
  35. api string
  36. version string
  37. ak string
  38. sk string
  39. requestUrl string
  40. params map[string]string // "form 参数对"
  41. headers map[string]string // "http headers"
  42. ct contentBody // "设置传输的jsonBody 或者 byte[]"
  43. }
  44. Builder func(map[string]HttpParams) string
  45. )
  46. func NewHttpParams(reqUrl string) *HttpParams {
  47. hp := HttpParams{
  48. requestUrl: reqUrl,
  49. method: "get",
  50. params: make(map[string]string),
  51. headers: make(map[string]string),
  52. }
  53. hp.ct.contentType = "unknown"
  54. return &hp
  55. }
  56. func (hp *HttpParams) SetApi(api string) *HttpParams {
  57. hp.api = api
  58. return hp
  59. }
  60. func (hp *HttpParams) SetVersion(version string) *HttpParams {
  61. hp.version = version
  62. return hp
  63. }
  64. func (hp *HttpParams) SetAK(ak string) *HttpParams {
  65. hp.ak = ak
  66. return hp
  67. }
  68. func (hp *HttpParams) SetSK(sk string) *HttpParams {
  69. hp.sk = sk
  70. return hp
  71. }
  72. func (hp *HttpParams) SetRequest(reqUrl string) *HttpParams {
  73. hp.requestUrl = reqUrl
  74. return hp
  75. }
  76. func (hp *HttpParams) SetMethod(method string) *HttpParams {
  77. hp.method = method
  78. return hp
  79. }
  80. func (hp *HttpParams) AddParam(key string, value string) *HttpParams {
  81. hp.params[key] = value
  82. return hp
  83. }
  84. func (hp *HttpParams) AddHeader(key string, value string) *HttpParams {
  85. hp.headers[key] = value
  86. return hp
  87. }
  88. func (hp *HttpParams) SetContentBody(jsonStr string, byteArr []byte) error {
  89. //if jsonStr != "" && byteArr != nil {
  90. // errRtn := HttpCallerException{Message: "can not set jsonStr and byteArr parameters together!"}
  91. // return errRtn
  92. //}
  93. if jsonStr != "" {
  94. hp.ct.jsonBody = jsonStr
  95. hp.ct.contentType = JSON
  96. }
  97. if byteArr != nil {
  98. hp.ct.bytesBody = byteArr //TODO: copy or pointer ?
  99. hp.ct.contentType = BINARY
  100. }
  101. return nil
  102. }
  103. /**
  104. 校验设置的请求参数项是否有效
  105. */
  106. func (hp *HttpParams) Validate() *HttpCallerException {
  107. hp.method = strings.ToLower(hp.method)
  108. if hp.method != "get" && hp.method != "post" {
  109. return &HttpCallerException{Message: "bad method, only support 'get' or 'post'"}
  110. }
  111. if hp.ak != "" && hp.sk == "" {
  112. return &HttpCallerException{Message: "bad request params, ak and sk must be defined together"}
  113. }
  114. if hp.api == "" || hp.version == "" {
  115. return &HttpCallerException{Message: "bad request params, api or version is not defined"}
  116. }
  117. return nil
  118. }
  119. /**
  120. 打印设置的参数值
  121. */
  122. func (hp *HttpParams) Print() {
  123. j, _ := json.Marshal(*hp)
  124. fmt.Println(string(j))
  125. }