httppull_source.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2021-2023 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 http
  15. import (
  16. "github.com/lf-edge/ekuiper/pkg/infra"
  17. "time"
  18. "github.com/lf-edge/ekuiper/internal/conf"
  19. "github.com/lf-edge/ekuiper/internal/pkg/httpx"
  20. "github.com/lf-edge/ekuiper/pkg/api"
  21. )
  22. type PullSource struct {
  23. ClientConf
  24. }
  25. func (hps *PullSource) Configure(device string, props map[string]interface{}) error {
  26. conf.Log.Infof("Initialized Httppull source with configurations %#v.", props)
  27. return hps.InitConf(device, props)
  28. }
  29. func (hps *PullSource) Open(ctx api.StreamContext, consumer chan<- api.SourceTuple, errCh chan<- error) {
  30. ctx.GetLogger().Infof("Opening HTTP pull source with conf %+v", hps.config)
  31. // trigger refresh token timer
  32. if hps.accessConf != nil && hps.accessConf.ExpireInSecond > 0 {
  33. go infra.SafeRun(func() error {
  34. ctx.GetLogger().Infof("Starting refresh token for %d seconds", hps.accessConf.ExpireInSecond/2)
  35. ticker := time.NewTicker(time.Duration(hps.accessConf.ExpireInSecond/2) * time.Second)
  36. defer ticker.Stop()
  37. for {
  38. select {
  39. case <-ticker.C:
  40. ctx.GetLogger().Debugf("Refreshing token")
  41. hps.refresh(ctx)
  42. case <-ctx.Done():
  43. ctx.GetLogger().Infof("Closing refresh token timer")
  44. return nil
  45. }
  46. }
  47. })
  48. }
  49. hps.initTimerPull(ctx, consumer, errCh)
  50. }
  51. func (hps *PullSource) Close(ctx api.StreamContext) error {
  52. logger := ctx.GetLogger()
  53. logger.Infof("Closing HTTP pull source")
  54. return nil
  55. }
  56. func (hps *PullSource) initTimerPull(ctx api.StreamContext, consumer chan<- api.SourceTuple, _ chan<- error) {
  57. logger := ctx.GetLogger()
  58. logger.Infof("Starting HTTP pull source with interval %d", hps.config.Interval)
  59. ticker := time.NewTicker(time.Millisecond * time.Duration(hps.config.Interval))
  60. defer ticker.Stop()
  61. var omd5 = ""
  62. for {
  63. select {
  64. case <-ticker.C:
  65. headers, err := hps.parseHeaders(ctx, hps.tokens)
  66. if err != nil {
  67. continue
  68. }
  69. ctx.GetLogger().Debugf("rest sink sending request url: %s, headers: %v, body %s", hps.config.Url, headers, hps.config.Body)
  70. if resp, e := httpx.Send(logger, hps.client, hps.config.BodyType, hps.config.Method, hps.config.Url, headers, true, []byte(hps.config.Body)); e != nil {
  71. logger.Warnf("Found error %s when trying to reach %v ", e, hps)
  72. } else {
  73. logger.Debugf("rest sink got response %v", resp)
  74. result, _, e := hps.parseResponse(ctx, resp, true, &omd5)
  75. if e != nil {
  76. logger.Errorf("Parse response error %v", e)
  77. continue
  78. }
  79. if result == nil {
  80. logger.Debugf("no data to send for incremental")
  81. continue
  82. }
  83. meta := make(map[string]interface{})
  84. select {
  85. case consumer <- api.NewDefaultSourceTuple(result, meta):
  86. logger.Debugf("send data to device node")
  87. case <-ctx.Done():
  88. return
  89. }
  90. }
  91. case <-ctx.Done():
  92. return
  93. }
  94. }
  95. }