rest_sink.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. "io/ioutil"
  21. "net/http"
  22. "net/url"
  23. "strings"
  24. "time"
  25. )
  26. type RestSink struct {
  27. method string
  28. url string
  29. headers map[string]string
  30. bodyType string
  31. timeout int64
  32. sendSingle bool
  33. debugResp bool
  34. insecureSkipVerify bool
  35. client *http.Client
  36. }
  37. var methodsMap = map[string]bool{"GET": true, "HEAD": true, "POST": true, "PUT": true, "DELETE": true, "PATCH": true}
  38. func (ms *RestSink) Configure(ps map[string]interface{}) error {
  39. temp, ok := ps["method"]
  40. if ok {
  41. ms.method, ok = temp.(string)
  42. if !ok {
  43. return fmt.Errorf("rest sink property method %v is not a string", temp)
  44. }
  45. ms.method = strings.ToUpper(strings.Trim(ms.method, ""))
  46. } else {
  47. ms.method = "GET"
  48. }
  49. if _, ok = methodsMap[ms.method]; !ok {
  50. return fmt.Errorf("invalid property method: %s", ms.method)
  51. }
  52. switch ms.method {
  53. case "GET", "HEAD":
  54. ms.bodyType = "none"
  55. default:
  56. ms.bodyType = "json"
  57. }
  58. temp, ok = ps["url"]
  59. if !ok {
  60. return fmt.Errorf("rest sink is missing property url")
  61. }
  62. ms.url, ok = temp.(string)
  63. if !ok {
  64. return fmt.Errorf("rest sink property url %v is not a string", temp)
  65. }
  66. ms.url = strings.Trim(ms.url, "")
  67. temp, ok = ps["headers"]
  68. if ok {
  69. ms.headers = make(map[string]string)
  70. if m, ok := temp.(map[string]interface{}); ok {
  71. for k, v := range m {
  72. if v1, ok1 := v.(string); ok1 {
  73. ms.headers[k] = v1
  74. } else {
  75. return fmt.Errorf("header value %s for header %s is not a string", v, k)
  76. }
  77. }
  78. } else {
  79. return fmt.Errorf("rest sink property headers %v is not a map[string]interface", temp)
  80. }
  81. }
  82. temp, ok = ps["bodyType"]
  83. if ok {
  84. ms.bodyType, ok = temp.(string)
  85. if !ok {
  86. return fmt.Errorf("rest sink property bodyType %v is not a string", temp)
  87. }
  88. ms.bodyType = strings.ToLower(strings.Trim(ms.bodyType, ""))
  89. }
  90. if _, ok = httpx.BodyTypeMap[ms.bodyType]; !ok {
  91. return fmt.Errorf("invalid property bodyType: %s, should be \"none\" or \"form\"", ms.bodyType)
  92. }
  93. temp, ok = ps["timeout"]
  94. if !ok {
  95. ms.timeout = 5000
  96. } else {
  97. to, ok := temp.(float64)
  98. if !ok {
  99. return fmt.Errorf("rest sink property timeout %v is not a number", temp)
  100. }
  101. ms.timeout = int64(to)
  102. }
  103. temp, ok = ps["sendSingle"]
  104. if !ok {
  105. ms.sendSingle = false
  106. } else {
  107. ms.sendSingle, ok = temp.(bool)
  108. if !ok {
  109. return fmt.Errorf("rest sink property sendSingle %v is not a bool", temp)
  110. }
  111. }
  112. temp, ok = ps["debugResp"]
  113. if !ok {
  114. ms.debugResp = false
  115. } else {
  116. ms.debugResp, ok = temp.(bool)
  117. if !ok {
  118. return fmt.Errorf("rest sink property debugResp %v is not a bool", temp)
  119. }
  120. }
  121. temp, ok = ps["insecureSkipVerify"]
  122. if !ok {
  123. ms.insecureSkipVerify = true
  124. } else {
  125. ms.insecureSkipVerify, ok = temp.(bool)
  126. if !ok {
  127. return fmt.Errorf("rest sink property insecureSkipVerify %v is not a bool", temp)
  128. }
  129. }
  130. return nil
  131. }
  132. func (ms *RestSink) Open(ctx api.StreamContext) error {
  133. logger := ctx.GetLogger()
  134. tr := &http.Transport{
  135. TLSClientConfig: &tls.Config{InsecureSkipVerify: ms.insecureSkipVerify},
  136. }
  137. ms.client = &http.Client{
  138. Transport: tr,
  139. Timeout: time.Duration(ms.timeout) * time.Millisecond}
  140. 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)
  141. if _, err := url.Parse(ms.url); err != nil {
  142. return err
  143. }
  144. return nil
  145. }
  146. type MultiErrors []error
  147. func (me MultiErrors) AddError(err error) MultiErrors {
  148. me = append(me, err)
  149. return me
  150. }
  151. func (me MultiErrors) Error() string {
  152. s := make([]string, len(me))
  153. for i, v := range me {
  154. s = append(s, fmt.Sprintf("Error %d with info %s. \n", i, v))
  155. }
  156. return strings.Join(s, " ")
  157. }
  158. func (ms *RestSink) Collect(ctx api.StreamContext, item interface{}) error {
  159. logger := ctx.GetLogger()
  160. v, ok := item.([]byte)
  161. if !ok {
  162. logger.Warnf("rest sink receive non []byte data: %v", item)
  163. }
  164. logger.Debugf("rest sink receive %s", item)
  165. resp, err := ms.Send(v, logger)
  166. if err != nil {
  167. return fmt.Errorf("rest sink fails to send out the data: %s", err)
  168. } else {
  169. defer resp.Body.Close()
  170. logger.Debugf("rest sink got response %v", resp)
  171. if resp.StatusCode < 200 || resp.StatusCode > 299 {
  172. if buf, bodyErr := ioutil.ReadAll(resp.Body); bodyErr != nil {
  173. logger.Errorf("%s\n", bodyErr)
  174. return fmt.Errorf("rest sink fails to err http return code: %d and error message %s.", resp.StatusCode, bodyErr)
  175. } else {
  176. logger.Errorf("%s\n", string(buf))
  177. return fmt.Errorf("rest sink fails to err http return code: %d and error message %s.", resp.StatusCode, string(buf))
  178. }
  179. } else {
  180. if ms.debugResp {
  181. if buf, bodyErr := ioutil.ReadAll(resp.Body); bodyErr != nil {
  182. logger.Errorf("%s\n", bodyErr)
  183. } else {
  184. logger.Infof("Response content: %s\n", string(buf))
  185. }
  186. }
  187. }
  188. }
  189. return nil
  190. }
  191. func (ms *RestSink) Send(v interface{}, logger api.Logger) (*http.Response, error) {
  192. return httpx.Send(logger, ms.client, ms.bodyType, ms.method, ms.url, ms.headers, ms.sendSingle, v)
  193. }
  194. func (ms *RestSink) Close(ctx api.StreamContext) error {
  195. logger := ctx.GetLogger()
  196. logger.Infof("Closing rest sink")
  197. return nil
  198. }