rest_sink.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // Copyright 2021 EMQ Technologies Co., Ltd.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package sink
  15. import (
  16. "crypto/tls"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/internal/pkg/httpx"
  19. "github.com/lf-edge/ekuiper/pkg/api"
  20. "github.com/lf-edge/ekuiper/pkg/errorx"
  21. "io/ioutil"
  22. "net/http"
  23. "net/url"
  24. "strings"
  25. "time"
  26. )
  27. type RestSink struct {
  28. method string
  29. url string
  30. headers map[string]string
  31. headersTemplate string
  32. bodyType string
  33. timeout int64
  34. sendSingle bool
  35. debugResp bool
  36. insecureSkipVerify bool
  37. client *http.Client
  38. }
  39. var methodsMap = map[string]bool{"GET": true, "HEAD": true, "POST": true, "PUT": true, "DELETE": true, "PATCH": true}
  40. func (ms *RestSink) Configure(ps map[string]interface{}) error {
  41. temp, ok := ps["method"]
  42. if ok {
  43. ms.method, ok = temp.(string)
  44. if !ok {
  45. return fmt.Errorf("rest sink property method %v is not a string", temp)
  46. }
  47. ms.method = strings.ToUpper(strings.Trim(ms.method, ""))
  48. } else {
  49. ms.method = "GET"
  50. }
  51. if _, ok = methodsMap[ms.method]; !ok {
  52. return fmt.Errorf("invalid property method: %s", ms.method)
  53. }
  54. switch ms.method {
  55. case "GET", "HEAD":
  56. ms.bodyType = "none"
  57. default:
  58. ms.bodyType = "json"
  59. }
  60. temp, ok = ps["url"]
  61. if !ok {
  62. return fmt.Errorf("rest sink is missing property url")
  63. }
  64. ms.url, ok = temp.(string)
  65. if !ok {
  66. return fmt.Errorf("rest sink property url %v is not a string", temp)
  67. }
  68. ms.url = strings.Trim(ms.url, "")
  69. temp, ok = ps["headers"]
  70. if ok {
  71. switch h := temp.(type) {
  72. case map[string]interface{}:
  73. ms.headers = make(map[string]string)
  74. for k, v := range h {
  75. if v1, ok1 := v.(string); ok1 {
  76. ms.headers[k] = v1
  77. } else {
  78. return fmt.Errorf("header value %s for header %s is not a string", v, k)
  79. }
  80. }
  81. case string:
  82. ms.headersTemplate = h
  83. default:
  84. return fmt.Errorf("rest sink property headers %v is not a map[string]interface", temp)
  85. }
  86. }
  87. temp, ok = ps["bodyType"]
  88. if ok {
  89. ms.bodyType, ok = temp.(string)
  90. if !ok {
  91. return fmt.Errorf("rest sink property bodyType %v is not a string", temp)
  92. }
  93. ms.bodyType = strings.ToLower(strings.Trim(ms.bodyType, ""))
  94. }
  95. if _, ok = httpx.BodyTypeMap[ms.bodyType]; !ok {
  96. return fmt.Errorf("invalid property bodyType: %s, should be \"none\" or \"form\"", ms.bodyType)
  97. }
  98. temp, ok = ps["timeout"]
  99. if !ok {
  100. ms.timeout = 5000
  101. } else {
  102. to, ok := temp.(float64)
  103. if !ok {
  104. return fmt.Errorf("rest sink property timeout %v is not a number", temp)
  105. }
  106. ms.timeout = int64(to)
  107. }
  108. temp, ok = ps["sendSingle"]
  109. if !ok {
  110. ms.sendSingle = false
  111. } else {
  112. ms.sendSingle, ok = temp.(bool)
  113. if !ok {
  114. return fmt.Errorf("rest sink property sendSingle %v is not a bool", temp)
  115. }
  116. }
  117. temp, ok = ps["debugResp"]
  118. if !ok {
  119. ms.debugResp = false
  120. } else {
  121. ms.debugResp, ok = temp.(bool)
  122. if !ok {
  123. return fmt.Errorf("rest sink property debugResp %v is not a bool", temp)
  124. }
  125. }
  126. temp, ok = ps["insecureSkipVerify"]
  127. if !ok {
  128. ms.insecureSkipVerify = true
  129. } else {
  130. ms.insecureSkipVerify, ok = temp.(bool)
  131. if !ok {
  132. return fmt.Errorf("rest sink property insecureSkipVerify %v is not a bool", temp)
  133. }
  134. }
  135. return nil
  136. }
  137. func (ms *RestSink) Open(ctx api.StreamContext) error {
  138. logger := ctx.GetLogger()
  139. tr := &http.Transport{
  140. TLSClientConfig: &tls.Config{InsecureSkipVerify: ms.insecureSkipVerify},
  141. }
  142. ms.client = &http.Client{
  143. Transport: tr,
  144. Timeout: time.Duration(ms.timeout) * time.Millisecond}
  145. logger.Infof("open rest sink with configuration: {method: %s, url: %s, bodyType: %s, timeout: %d,header: %v, sendSingle: %v, insecureSkipVerify: %v", ms.method, ms.url, ms.bodyType, ms.timeout, ms.headers, ms.sendSingle, ms.insecureSkipVerify)
  146. if _, err := url.Parse(ms.url); err != nil {
  147. return err
  148. }
  149. return nil
  150. }
  151. type MultiErrors []error
  152. func (me MultiErrors) AddError(err error) MultiErrors {
  153. me = append(me, err)
  154. return me
  155. }
  156. func (me MultiErrors) Error() string {
  157. s := make([]string, len(me))
  158. for i, v := range me {
  159. s = append(s, fmt.Sprintf("Error %d with info %s. \n", i, v))
  160. }
  161. return strings.Join(s, " ")
  162. }
  163. func (ms *RestSink) Collect(ctx api.StreamContext, item interface{}) error {
  164. logger := ctx.GetLogger()
  165. logger.Debugf("rest sink receive %s", item)
  166. output, transed, err := ctx.TransformOutput()
  167. if err != nil {
  168. logger.Warnf("rest sink decode data error: %v", err)
  169. return nil
  170. }
  171. var d = item
  172. if transed {
  173. d = output
  174. }
  175. resp, err := ms.Send(ctx, d, logger)
  176. if err != nil {
  177. return fmt.Errorf("rest sink fails to send out the data: %s", err)
  178. } else {
  179. defer resp.Body.Close()
  180. logger.Debugf("rest sink got response %v", resp)
  181. if resp.StatusCode < 200 || resp.StatusCode > 299 {
  182. if buf, bodyErr := ioutil.ReadAll(resp.Body); bodyErr != nil {
  183. logger.Errorf("%s\n", bodyErr)
  184. return fmt.Errorf("%s: http return code: %d and error message %s", errorx.IOErr, resp.StatusCode, bodyErr)
  185. } else {
  186. logger.Errorf("%s\n", string(buf))
  187. return fmt.Errorf("%s: http return code: %d and error message %s", errorx.IOErr, resp.StatusCode, string(buf))
  188. }
  189. } else {
  190. if ms.debugResp {
  191. if buf, bodyErr := ioutil.ReadAll(resp.Body); bodyErr != nil {
  192. logger.Errorf("%s\n", bodyErr)
  193. } else {
  194. logger.Infof("Response content: %s\n", string(buf))
  195. }
  196. }
  197. }
  198. }
  199. return nil
  200. }
  201. func (ms *RestSink) Send(ctx api.StreamContext, v interface{}, logger api.Logger) (*http.Response, error) {
  202. temp, err := ctx.ParseDynamicProp(ms.bodyType, v)
  203. if err != nil {
  204. return nil, err
  205. }
  206. bodyType, ok := temp.(string)
  207. if !ok {
  208. return nil, fmt.Errorf("the value %v of dynamic prop %s for bodyType is not a string", ms.bodyType, temp)
  209. }
  210. temp, err = ctx.ParseDynamicProp(ms.method, v)
  211. if err != nil {
  212. return nil, err
  213. }
  214. method, ok := temp.(string)
  215. if !ok {
  216. return nil, fmt.Errorf("the value %v of dynamic prop %s for method is not a string", ms.method, temp)
  217. }
  218. temp, err = ctx.ParseDynamicProp(ms.url, v)
  219. if err != nil {
  220. return nil, err
  221. }
  222. url, ok := temp.(string)
  223. if !ok {
  224. return nil, fmt.Errorf("the value %v of dynamic prop %s for url is not a string", ms.url, temp)
  225. }
  226. var headers map[string]string
  227. if ms.headers != nil {
  228. headers = ms.headers
  229. } else if ms.headersTemplate != "" {
  230. temp, err = ctx.ParseDynamicProp(ms.headersTemplate, v)
  231. if err != nil {
  232. return nil, err
  233. }
  234. headers, ok = temp.(map[string]string)
  235. if !ok {
  236. return nil, fmt.Errorf("the value %v of dynamic prop %s for headers is not a map[string]string", ms.headersTemplate, temp)
  237. }
  238. }
  239. return httpx.Send(logger, ms.client, bodyType, method, url, headers, ms.sendSingle, v)
  240. }
  241. func (ms *RestSink) Close(ctx api.StreamContext) error {
  242. logger := ctx.GetLogger()
  243. logger.Infof("Closing rest sink")
  244. return nil
  245. }