httppull_lookup.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 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. func GetLookUpSource() *lookupSource {
  22. return &lookupSource{}
  23. }
  24. type lookupSource struct {
  25. *ClientConf
  26. }
  27. func (l *lookupSource) Open(ctx api.StreamContext) error {
  28. ctx.GetLogger().Infof("lookup source is opened")
  29. return nil
  30. }
  31. func (l *lookupSource) Configure(datasource string, props map[string]interface{}) error {
  32. conf.Log.Infof("Initialized Httppull lookup table with configurations %#v.", props)
  33. if l.ClientConf == nil {
  34. l.ClientConf = &ClientConf{}
  35. }
  36. return l.InitConf(datasource, props)
  37. }
  38. func (l *lookupSource) Lookup(ctx api.StreamContext, _ []string, keys []string, values []interface{}) ([]api.SourceTuple, error) {
  39. resps, err := l.pull(ctx)
  40. if err != nil {
  41. return nil, err
  42. }
  43. matched := l.lookupJoin(resps, keys, values)
  44. var results []api.SourceTuple
  45. meta := make(map[string]interface{})
  46. for _, resp := range matched {
  47. results = append(results, api.NewDefaultSourceTupleWithTime(resp, meta, conf.GetNow()))
  48. }
  49. return results, nil
  50. }
  51. func (l *lookupSource) Close(ctx api.StreamContext) error {
  52. logger := ctx.GetLogger()
  53. logger.Infof("Closing HTTP pull lookup table")
  54. return nil
  55. }
  56. func (l *lookupSource) lookupJoin(dataMap []map[string]interface{}, keys []string, values []interface{}) []map[string]interface{} {
  57. var resps []map[string]interface{}
  58. for _, resp := range dataMap {
  59. match := true
  60. for i, k := range keys {
  61. if val, ok := resp[k]; !ok || val != values[i] {
  62. match = false
  63. break
  64. }
  65. }
  66. if match {
  67. resps = append(resps, resp)
  68. }
  69. }
  70. return resps
  71. }
  72. func (l *lookupSource) pull(ctx api.StreamContext) ([]map[string]interface{}, error) {
  73. // check oAuth token expiration
  74. if l.accessConf != nil && l.accessConf.ExpireInSecond > 0 &&
  75. int(time.Now().Sub(l.tokenLastUpdateAt).Abs().Seconds()) >= l.accessConf.ExpireInSecond {
  76. ctx.GetLogger().Debugf("Refreshing token for HTTP pull")
  77. if err := l.refresh(ctx); err != nil {
  78. ctx.GetLogger().Warnf("Refresh HTTP pull token error: %v", err)
  79. }
  80. }
  81. headers, err := l.parseHeaders(ctx, l.tokens)
  82. if err != nil {
  83. return nil, err
  84. }
  85. ctx.GetLogger().Debugf("httppull source sending request url: %s, headers: %v, body %s", l.config.Url, headers, l.config.Body)
  86. resp, e := httpx.Send(ctx.GetLogger(), l.client, l.config.BodyType, l.config.Method, l.config.Url, headers, true, l.config.Body)
  87. if e != nil {
  88. ctx.GetLogger().Warnf("Found error %s when trying to reach %v ", e, l)
  89. return nil, err
  90. }
  91. ctx.GetLogger().Debugf("httppull source got response %v", resp)
  92. results, _, e := l.parseResponse(ctx, resp, true, nil)
  93. if e != nil {
  94. return nil, err
  95. }
  96. return results, nil
  97. }