http

package
v0.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

README

HTTP API

Extract metadata from any external HTTP API using a user-defined Tengo script.

Usage

source:
  scope: production
  type: http
  config:
    request:
      route_pattern: "/api/v1/users"
      url: "https://example.com/api/v1/users"
      method: "GET"
      headers:
        "Api-Token": "my-secret-token"
      content_type: application/json
      accept: application/json
      timeout: 5s
    success_codes: [200]
    concurrency: 5
    script:
      engine: tengo
      source: |
        for u in response.body.users {
          entity := new_entity("user")
          entity.urn = format("urn:my_svc:%s:user:%s", recipe_scope, u.id)
          entity.name = u.full_name
          entity.source = "my_svc"
          entity.properties.email = u.email
          entity.properties.status = u.active ? "active" : "suspended"
          emit(entity)
        }

Configuration

Key Type Required Default Description
request.route_pattern string Yes Route pattern used as http.route metric tag.
request.url string Yes HTTP endpoint URL.
request.method string No GET HTTP method (GET or POST).
request.headers map[string]string No HTTP request headers.
request.content_type string Yes Content type for the request body (only application/json).
request.accept string Yes Accept header / response decode format (only application/json).
request.body object No Request body.
request.query_params []{key, value} No Query parameters appended to the URL.
request.timeout string No 5s Request timeout.
success_codes []int No [200] HTTP status codes considered successful.
concurrency int No 5 Concurrency for child requests via execute_request.
script.engine string Yes Script engine (only tengo).
script.source string Yes Tengo script source code.
script.max_allocs int No 5000 Max object allocations during script execution.
script.max_const_objects int No 500 Max constant objects in compiled script.
before_script.engine string No Script engine for a pre-request script.
before_script.source string No Tengo script executed before the main request.
Notes
  • Only application/json is supported for request/response encoding.
  • Query params in request.query_params take precedence over those in request.url.
  • The script runs only if the response status code matches success_codes.
  • The Tengo os stdlib module is not available.

Script interface

The following globals are available in the Tengo script:

recipe_scope

The scope value from the recipe (string).

response

HTTP response object with status_code, header, and body. Header names are lower-cased.

{
  "status_code": "200",
  "header": { "content-type": "application/json" },
  "body": { "users": [...] }
}
new_entity(type) -> entity

Creates a new entity map of the given type (e.g. "user", "table", "topic", "job", "dashboard", "bucket", "model", "application", etc.).

Set fields on the returned map:

  • entity.urn - Entity URN (string)
  • entity.name - Entity name (string)
  • entity.source - Source system name (string)
  • entity.description - Description (string)
  • entity.properties.* - Type-specific metadata as flat key-value pairs
emit(entity)

Emits the entity as a Record to the pipeline.

execute_request(...requests) -> []response

Executes one or more HTTP requests concurrently (up to concurrency). Each request object supports the same fields as request in the configuration. Returns an array where each item is either a response or an error.

reqs := []
for item in response.body.items {
  reqs = append(reqs, {
    url: format("https://api.example.com/items/%s", item.id),
    method: "GET",
    content_type: "application/json",
    accept: "application/json",
    timeout: "5s"
  })
}
results := execute_request(reqs...)
for r in results {
  if is_error(r) { continue }
  entity := new_entity("job")
  entity.urn = format("urn:my_svc:%s:job:%s", recipe_scope, r.body.id)
  entity.name = r.body.name
  entity.source = "my_svc"
  emit(entity)
}
exit()

Terminates script execution.

Entities

The output depends entirely on the user-defined script. The script can emit zero or more entities of any supported type via new_entity and emit.

Edges

This extractor does not emit edges directly. Edges can be constructed within the script if needed.

Contributing

Refer to the contribution guidelines for information on contributing to this module.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Request      RequestConfig `mapstructure:"request"`
	SuccessCodes []int         `mapstructure:"success_codes" validate:"dive,gte=200,lt=300" default:"[200]"`
	Concurrency  int           `mapstructure:"concurrency" validate:"gte=1,lte=100" default:"5"`
	Script       Script        `mapstructure:"script"`
	BeforeScript *Script       `mapstructure:"before_script"`
}

Config holds the set of configuration for the HTTP extractor.

type Extractor

type Extractor struct {
	plugins.BaseExtractor
	// contains filtered or unexported fields
}

Extractor is responsible for executing an HTTP request as per configuration and executing the script with the response to extract entities from within the script.

func New

func New(logger log.Logger) *Extractor

New returns a pointer to an initialized Extractor Object

func (*Extractor) Extract

func (e *Extractor) Extract(ctx context.Context, emit plugins.Emit) error

Extract executes an HTTP request as per the configuration and if successful, executes the script. The script has access to the response and can use the same to emit entities from within the script.

func (*Extractor) Init

func (e *Extractor) Init(ctx context.Context, config plugins.Config) error

Init initializes the extractor

type QueryParam

type QueryParam struct {
	Key   string `mapstructure:"key" validate:"required"`
	Value string `mapstructure:"value" validate:"required"`
}

type RequestConfig

type RequestConfig struct {
	RoutePattern string            `mapstructure:"route_pattern" default:""`
	URL          string            `mapstructure:"url" validate:"required,url"`
	QueryParams  []QueryParam      `mapstructure:"query_params" validate:"dive"`
	Method       string            `mapstructure:"method" validate:"oneof=GET POST" default:"GET"`
	Headers      map[string]string `mapstructure:"headers"`
	ContentType  string            `mapstructure:"content_type" validate:"required,oneof=application/json"`
	Accept       string            `mapstructure:"accept" validate:"required,oneof=application/json"`
	Body         any               `mapstructure:"body"`
	Timeout      time.Duration     `mapstructure:"timeout" validate:"min=1ms" default:"5s"`
}

type Script added in v0.11.0

type Script struct {
	Engine          string `mapstructure:"engine" validate:"required,oneof=tengo"`
	Source          string `mapstructure:"source" validate:"required"`
	MaxAllocs       int64  `mapstructure:"max_allocs" validate:"gt=100" default:"5000"`
	MaxConstObjects int    `mapstructure:"max_const_objects" validate:"gt=10" default:"500"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL