httppull_source.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. )
  21. type PullSource struct {
  22. ClientConf
  23. }
  24. func (hps *PullSource) Configure(device string, props map[string]interface{}) error {
  25. conf.Log.Infof("Initialized Httppull source with configurations %#v.", props)
  26. return hps.InitConf(device, props)
  27. }
  28. func (hps *PullSource) Open(ctx api.StreamContext, consumer chan<- api.SourceTuple, errCh chan<- error) {
  29. ctx.GetLogger().Infof("Opening HTTP pull source with conf %+v", hps.config)
  30. hps.initTimerPull(ctx, consumer, errCh)
  31. }
  32. func (hps *PullSource) Close(ctx api.StreamContext) error {
  33. logger := ctx.GetLogger()
  34. logger.Infof("Closing HTTP pull source")
  35. return nil
  36. }
  37. func (hps *PullSource) initTimerPull(ctx api.StreamContext, consumer chan<- api.SourceTuple, _ chan<- error) {
  38. logger := ctx.GetLogger()
  39. logger.Infof("Starting HTTP pull source with interval %d", hps.config.Interval)
  40. ticker := time.NewTicker(time.Millisecond * time.Duration(hps.config.Interval))
  41. defer ticker.Stop()
  42. omd5 := ""
  43. for {
  44. select {
  45. case <-ticker.C:
  46. rcvTime := conf.GetNow()
  47. headers, err := hps.parseHeaders(ctx, hps.tokens)
  48. if err != nil {
  49. continue
  50. }
  51. // check oAuth token expiration
  52. if hps.accessConf != nil && hps.accessConf.ExpireInSecond > 0 &&
  53. int(time.Now().Sub(hps.tokenLastUpdateAt).Abs().Seconds()) >= hps.accessConf.ExpireInSecond {
  54. ctx.GetLogger().Debugf("Refreshing token")
  55. if err := hps.refresh(ctx); err != nil {
  56. ctx.GetLogger().Warnf("Refresh token error: %v", err)
  57. }
  58. }
  59. ctx.GetLogger().Debugf("rest sink sending request url: %s, headers: %v, body %s", hps.config.Url, headers, hps.config.Body)
  60. 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 {
  61. logger.Warnf("Found error %s when trying to reach %v ", e, hps)
  62. } else {
  63. logger.Debugf("rest sink got response %v", resp)
  64. results, _, e := hps.parseResponse(ctx, resp, true, &omd5)
  65. if e != nil {
  66. logger.Errorf("Parse response error %v", e)
  67. continue
  68. }
  69. if results == nil {
  70. logger.Debugf("no data to send for incremental")
  71. continue
  72. }
  73. meta := make(map[string]interface{})
  74. for _, result := range results {
  75. select {
  76. case consumer <- api.NewDefaultSourceTupleWithTime(result, meta, rcvTime):
  77. logger.Debugf("send data to device node")
  78. case <-ctx.Done():
  79. return
  80. }
  81. }
  82. }
  83. case <-ctx.Done():
  84. return
  85. }
  86. }
  87. }