openapi

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package openapi provides a generic provider for OpenAPI/Swagger based services. This allows quick integration of any HTTP service that provides OpenAPI documentation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIKeyAuth

type APIKeyAuth struct {
	// Name is the header or query parameter name (e.g., "X-API-Key")
	Name string `yaml:"name" json:"name"`

	// Value is the API key value
	Value string `yaml:"value" json:"value"`

	// In specifies where to put the key: "header" or "query"
	In string `yaml:"in" json:"in"`
}

APIKeyAuth defines API key authentication.

type APIMethod

type APIMethod struct {
	// Name is the method name used for calling (e.g., "get_role", "ban_user")
	// Falls back to operationId or generated from path if not set
	Name string `yaml:"name" json:"name"`

	// OperationId from OpenAPI spec (unique identifier for the operation)
	OperationID string `yaml:"operation_id" json:"operation_id"`

	// Summary is a short summary of what the operation does (from OpenAPI)
	Summary string `yaml:"summary" json:"summary"`

	// Description is a verbose explanation of the operation behavior (from OpenAPI)
	Description string `yaml:"description" json:"description"`

	// Path is the API path (e.g., "/api/role/{role_id}")
	Path string `yaml:"path" json:"path"`

	// Method is the HTTP method (GET, POST, PUT, DELETE, etc.)
	Method string `yaml:"method" json:"method"`

	// Tags for categorization (from OpenAPI tags)
	// Operations may be grouped by tags for UI organization
	Tags []string `yaml:"tags" json:"tags"`

	// Parameters defines how to map request parameters
	Parameters []ParameterMapping `yaml:"parameters" json:"parameters"`

	// RequestBody defines how to map the request body
	RequestBody *RequestBodyMapping `yaml:"request_body" json:"request_body"`

	// ResponseMapping defines how to transform the response
	ResponseMapping *ResponseMapping `yaml:"response_mapping" json:"response_mapping"`

	// Deprecated marks this operation as deprecated (from OpenAPI)
	Deprecated bool `yaml:"deprecated" json:"deprecated"`
}

APIMethod defines a single API method that can be called. It closely follows OpenAPI 3.0 operation object structure.

type AuthConfig

type AuthConfig struct {
	// Type can be: "none", "bearer", "basic", "api_key", "custom"
	Type string `yaml:"type" json:"type"`

	// Bearer token for "bearer" type
	Token string `yaml:"token" json:"token"`

	// Username for "basic" type
	Username string `yaml:"username" json:"username"`

	// Password for "basic" type
	Password string `yaml:"password" json:"password"`

	// APIKey configuration for "api_key" type
	APIKey *APIKeyAuth `yaml:"api_key" json:"api_key"`

	// Custom header configuration for "custom" type
	CustomHeaders map[string]string `yaml:"custom_headers" json:"custom_headers"`
}

AuthConfig defines authentication methods.

type Config

type Config struct {
	// BaseURL is the base URL of the OpenAPI service (e.g., "http://api.example.com")
	BaseURL string `yaml:"base_url" json:"base_url"`

	// Auth specifies the authentication method
	Auth *AuthConfig `yaml:"auth" json:"auth"`

	// Methods defines the API methods available through this provider
	// If empty, will attempt to discover from OpenAPI spec
	Methods []APIMethod `yaml:"methods" json:"methods"`

	// OpenAPISpec is the URL or local path to the OpenAPI/Swagger specification
	// If provided, the provider will auto-discover available methods
	OpenAPISpec string `yaml:"openapi_spec" json:"openapi_spec"`

	// Timeout for HTTP requests (supports "10s", "1m", "500ms" or seconds as number)
	Timeout Duration `yaml:"timeout" json:"timeout"`

	// RetryCount specifies how many times to retry on failure
	RetryCount int `yaml:"retry_count" json:"retry_count"`

	// Headers are default headers to include in all requests
	Headers map[string]string `yaml:"headers" json:"headers"`

	// Transform allows response transformation
	Transform *TransformConfig `yaml:"transform" json:"transform"`
}

Config holds the OpenAPI provider specific configuration.

type Duration

type Duration time.Duration

Duration is a custom duration type that supports parsing from both human-readable strings (like "10s", "1m", "500ms") and integers (nanoseconds).

func (Duration) Duration

func (d Duration) Duration() time.Duration

Duration returns the time.Duration value.

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler for Duration.

type MethodDetails

type MethodDetails struct {
	Name        string
	OperationID string
	Summary     string
	Description string
	Tags        []string
	Deprecated  bool
	Parameters  []ParameterMapping
}

MethodDetails contains the metadata for a method extracted from OpenAPI. This is used when registering functions to the LocalStore.

type ParameterMapping

type ParameterMapping struct {
	// Name is the parameter name in the API request
	Name string `yaml:"name" json:"name"`

	// In specifies where the parameter goes: "path", "query", "header", "cookie"
	In string `yaml:"in" json:"in"`

	// Description of the parameter (from OpenAPI)
	Description string `yaml:"description" json:"description"`

	// From specifies the field name in the input JSON (e.g., "role_id")
	// If empty, uses Name
	From string `yaml:"from" json:"from"`

	// Required indicates if this parameter is required (from OpenAPI)
	Required bool `yaml:"required" json:"required"`

	// Deprecated marks this parameter as deprecated (from OpenAPI)
	Deprecated bool `yaml:"deprecated" json:"deprecated"`

	// Default value if not provided (from OpenAPI)
	Default interface{} `yaml:"default" json:"default"`

	// Type of the parameter: "string", "number", "integer", "boolean", "array", "object"
	Type string `yaml:"type" json:"type"`

	// Format of the parameter type: e.g., "int32", "int64", "float", "double", "date-time"
	Format string `yaml:"format" json:"format"`

	// Enum allowed values for this parameter (from OpenAPI)
	Enum []interface{} `yaml:"enum" json:"enum"`
}

ParameterMapping defines how to map a parameter. Follows OpenAPI 3.0 parameter object structure.

type Provider

type Provider struct {
	// contains filtered or unexported fields
}

Provider implements the provider.Provider interface for OpenAPI based services. It provides a generic way to call any HTTP API with configurable authentication, parameter mapping, and response transformation.

func NewProvider

func NewProvider() *Provider

NewProvider creates a new OpenAPI provider.

func (*Provider) Call

func (p *Provider) Call(ctx context.Context, method string, request []byte) ([]byte, error)

Call invokes an API method.

func (*Provider) Close

func (p *Provider) Close() error

Close closes the provider.

func (*Provider) GetMethodDetails

func (p *Provider) GetMethodDetails() map[string]*MethodDetails

GetMethodDetails returns the complete details for all methods. This is used by Agent to register functions with full metadata.

func (*Provider) Init

func (p *Provider) Init(ctx context.Context, config provider.ProviderConfig) error

Init initializes the provider with configuration.

func (*Provider) IsEnabled

func (p *Provider) IsEnabled() bool

IsEnabled returns whether the provider is enabled.

func (*Provider) Name

func (p *Provider) Name() string

Name returns the provider name.

func (*Provider) SupportedMethods

func (p *Provider) SupportedMethods() []string

SupportedMethods returns the list of supported methods.

type RequestBodyMapping

type RequestBodyMapping struct {
	// Type specifies the body format: "json", "form", "text"
	Type string `yaml:"type" json:"type"`

	// Template is a Go template for building the body
	// Example: {"user_id": "{{ .user_id }}", "reason": "{{ .reason }}"}
	Template string `yaml:"template" json:"template"`

	// Fields defines field mappings (used when type is "json")
	Fields map[string]string `yaml:"fields" json:"fields"`
}

RequestBodyMapping defines how to map the request body.

type ResponseMapping

type ResponseMapping struct {
	// ExtractPath is a JSON path to extract from response (e.g., "data.items")
	ExtractPath string `yaml:"extract_path" json:"extract_path"`

	// Wrap indicates whether to wrap the response in a standard envelope
	Wrap bool `yaml:"wrap" json:"wrap"`

	// SuccessField is the field that indicates success (e.g., "code == 0")
	SuccessField string `yaml:"success_field" json:"success_field"`

	// SuccessValue is the value that indicates success
	SuccessValue interface{} `yaml:"success_value" json:"success_value"`
}

ResponseMapping defines how to transform the response.

type TransformConfig

type TransformConfig struct {
	// SuccessField specifies the field that indicates success
	SuccessField string `yaml:"success_field" json:"success_field"`

	// SuccessValue is the value that indicates success
	SuccessValue interface{} `yaml:"success_value" json:"success_value"`

	// DataField specifies the field containing the actual data
	DataField string `yaml:"data_field" json:"data_field"`

	// ErrorField specifies the field containing error message
	ErrorField string `yaml:"error_field" json:"error_field"`
}

TransformConfig defines global response transformation.

Jump to

Keyboard shortcuts

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