httppull_source.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. "time"
  17. "github.com/lf-edge/ekuiper/internal/conf"
  18. "github.com/lf-edge/ekuiper/internal/pkg/httpx"
  19. "github.com/lf-edge/ekuiper/pkg/api"
  20. "github.com/lf-edge/ekuiper/pkg/infra"
  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. omd5 := ""
  62. for {
  63. select {
  64. case <-ticker.C:
  65. rcvTime := conf.GetNow()
  66. headers, err := hps.parseHeaders(ctx, hps.tokens)
  67. if err != nil {
  68. continue
  69. }
  70. ctx.GetLogger().Debugf("rest sink sending request url: %s, headers: %v, body %s", hps.config.Url, headers, hps.config.Body)
  71. 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 {
  72. logger.Warnf("Found error %s when trying to reach %v ", e, hps)
  73. } else {
  74. logger.Debugf("rest sink got response %v", resp)
  75. results, _, e := hps.parseResponse(ctx, resp, true, &omd5)
  76. if e != nil {
  77. logger.Errorf("Parse response error %v", e)
  78. continue
  79. }
  80. if results == nil {
  81. logger.Debugf("no data to send for incremental")
  82. continue
  83. }
  84. meta := make(map[string]interface{})
  85. for _, result := range results {
  86. select {
  87. case consumer <- api.NewDefaultSourceTupleWithTime(result, meta, rcvTime):
  88. logger.Debugf("send data to device node")
  89. case <-ctx.Done():
  90. return
  91. }
  92. }
  93. }
  94. case <-ctx.Done():
  95. return
  96. }
  97. }
  98. }