httpparams.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. import (
  17. "encoding/json"
  18. "fmt"
  19. "strings"
  20. )
  21. /**
  22. 构造Http请求参数的struct
  23. */
  24. const (
  25. JSON = "application/json"
  26. BINARY = "application/octet-stream"
  27. )
  28. type contentBody struct {
  29. jsonBody string
  30. bytesBody []byte
  31. contentType string
  32. }
  33. type (
  34. HttpParams struct {
  35. method string // "设置方法: get, post"
  36. api string
  37. version string
  38. ak string
  39. sk string
  40. requestUrl string
  41. params map[string]string // "form 参数对"
  42. headers map[string]string // "http headers"
  43. ct contentBody // "设置传输的jsonBody 或者 byte[]"
  44. }
  45. Builder func(map[string]HttpParams) string
  46. )
  47. func NewHttpParams(reqUrl string) *HttpParams {
  48. hp := HttpParams{
  49. requestUrl: reqUrl,
  50. method: "get",
  51. params: make(map[string]string),
  52. headers: make(map[string]string),
  53. }
  54. hp.ct.contentType = "unknown"
  55. return &hp
  56. }
  57. func (hp *HttpParams) SetApi(api string) *HttpParams {
  58. hp.api = api
  59. return hp
  60. }
  61. func (hp *HttpParams) SetVersion(version string) *HttpParams {
  62. hp.version = version
  63. return hp
  64. }
  65. func (hp *HttpParams) SetAK(ak string) *HttpParams {
  66. hp.ak = ak
  67. return hp
  68. }
  69. func (hp *HttpParams) SetSK(sk string) *HttpParams {
  70. hp.sk = sk
  71. return hp
  72. }
  73. func (hp *HttpParams) SetRequest(reqUrl string) *HttpParams {
  74. hp.requestUrl = reqUrl
  75. return hp
  76. }
  77. func (hp *HttpParams) SetMethod(method string) *HttpParams {
  78. hp.method = method
  79. return hp
  80. }
  81. func (hp *HttpParams) AddParam(key string, value string) *HttpParams {
  82. hp.params[key] = value
  83. return hp
  84. }
  85. func (hp *HttpParams) AddHeader(key string, value string) *HttpParams {
  86. hp.headers[key] = value
  87. return hp
  88. }
  89. func (hp *HttpParams) SetContentBody(jsonStr string, byteArr []byte) error {
  90. if jsonStr != "" && byteArr != nil {
  91. errRtn := HttpCallerException{Message: "can not set jsonStr and byteArr parameters together!"}
  92. return errRtn
  93. }
  94. if jsonStr != "" {
  95. hp.ct.jsonBody = jsonStr
  96. hp.ct.contentType = JSON
  97. }
  98. if byteArr != nil {
  99. hp.ct.bytesBody = byteArr //TODO: copy or pointer ?
  100. hp.ct.contentType = BINARY
  101. }
  102. return nil
  103. }
  104. /**
  105. 校验设置的请求参数项是否有效
  106. */
  107. func (hp *HttpParams) Validate() *HttpCallerException {
  108. hp.method = strings.ToLower(hp.method)
  109. if hp.method != "get" && hp.method != "post" {
  110. return &HttpCallerException{Message: "bad method, only support 'get' or 'post'"}
  111. }
  112. if hp.ak != "" && hp.sk == "" {
  113. return &HttpCallerException{Message: "bad request params, ak and sk must be defined together"}
  114. }
  115. if hp.api == "" || hp.version == "" {
  116. return &HttpCallerException{Message: "bad request params, api or version is not defined"}
  117. }
  118. return nil
  119. }
  120. /**
  121. 打印设置的参数值
  122. */
  123. func (hp *HttpParams) Print() {
  124. j, _ := json.Marshal(*hp)
  125. fmt.Println(string(j))
  126. }