Documentation
¶
Index ¶
- Constants
- func ConvertOpenAPIToFunctions(spec *OpenAPISpec) map[string]*FunctionDefinition
- func ExecuteFunction(client *APIClient, fn *FunctionDefinition, arguments map[string]any) (any, error)
- func NewHTTPClient(config *AuthConfig, opts ...func(*http.Client)) *http.Client
- type APIClient
- type AuthConfig
- type AuthRoundTripper
- type AuthType
- type Components
- type FunctionDefinition
- type Info
- type MediaType
- type OpenAPISpec
- type Operation
- type Parameter
- type PathItem
- type RequestBody
- type Schema
Constants ¶
const ( AuthTypeNone AuthType = "none" AuthTypeBasic = "basic" AuthTypeAPIKeyHeader = "apikey-header" AuthTypeAPIKeyCookie = "apikey-cookie" AuthTypeBearer = "bearer" AuthTypeOAuth2 = "oauth2" AuthTypeCookie = "cookie" )
Variables ¶
This section is empty.
Functions ¶
func ConvertOpenAPIToFunctions ¶
func ConvertOpenAPIToFunctions(spec *OpenAPISpec) map[string]*FunctionDefinition
ConvertOpenAPIToFunctions converts OpenAPI spec to LLM function definitions
func ExecuteFunction ¶
func ExecuteFunction(client *APIClient, fn *FunctionDefinition, arguments map[string]any) (any, error)
ExecuteFunction calls the registered function handler
func NewHTTPClient ¶
func NewHTTPClient(config *AuthConfig, opts ...func(*http.Client)) *http.Client
NewHTTPClient creates an http.Client with the specified authentication. Usage examples
Example 1: Basic Auth
client1 := NewHTTPClient(&AuthConfig{
Type: AuthTypeBasic,
Username: "admin",
Password: "secret",
})
Example 2: API Key in header
client2 := NewHTTPClient(&AuthConfig{
Type: AuthTypeAPIKeyHeader,
APIKeyName: "X-API-Key",
APIKeyValue: "12345-abcde",
})
Example 3: Bearer Token
client3 := NewHTTPClient(&AuthConfig{
Type: AuthTypeBearer,
Token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
})
Example 4: OAuth2 with custom TokenSource oauth2Config := &oauth2.Config{...} ts := oauth2Config.TokenSource(context.Background(), token)
client4 := NewHTTPClient(&AuthConfig{
Type: AuthTypeOAuth2,
TokenSource: nil, // can pass oauth2.TokenSource
Token: "access_token_here", // fallback
})
Example 5: Cookie Auth
client5 := NewHTTPClient(&AuthConfig{
Type: AuthTypeCookie,
Cookies: []*http.Cookie{
{Name: "sessionid", Value: "abc123"},
{Name: "csrftoken", Value: "xyz789"},
},
})
Use it resp, err := client1.Get("https://httpbin.org/basic-auth/admin/secret")
if err != nil {
panic(err)
}
defer resp.Body.Close() fmt.Println("Status:", resp.Status)
Types ¶
type APIClient ¶
type APIClient struct {
BaseURL *url.URL
HTTPClient *http.Client // change this to client with authenticate
}
APIClient handles HTTP requests to the API
func NewAPIClient ¶
func NewAPIClient(baseURL string, authConfig *AuthConfig, opts ...func(*http.Client)) (*APIClient, error)
NewAPIClient creates a new API client
type AuthConfig ¶
type AuthConfig struct {
Type AuthType
// Basic Auth
Username string
Password string
// API Key
APIKeyName string // name of header or cookie
APIKeyValue string
// Bearer / OAuth2 / OpenID
Token string
// OAuth2 TokenSource (allows automatic token refresh)
TokenSource oauth2.TokenSource
// Cookie Auth: list of cookies (name=value)
Cookies []*http.Cookie
}
AuthConfig contains the authentication configuration.
type AuthRoundTripper ¶
type AuthRoundTripper struct {
// contains filtered or unexported fields
}
AuthRoundTripper wraps http.RoundTripper and adds authentication.
type Components ¶
type Components struct {
Parameters map[string]Parameter `json:"parameters,omitempty" yaml:"parameters,omitempty"`
}
Components contains reusable objects
type FunctionDefinition ¶
type FunctionDefinition struct {
Name string `json:"name" yaml:"name"`
Description string `json:"description" yaml:"description"`
Parameters Schema `json:"parameters" yaml:"parameters"`
OapiMethod string `json:"-" yaml:"-"`
OapiPath string `json:"-" yaml:"-"`
PathParams []string `json:"-" yaml:"-"`
QueryParams []string `json:"-" yaml:"-"`
}
FunctionDefinition represents an LLM function definition
type Info ¶
type Info struct {
Title string `json:"title" yaml:"title"`
Description string `json:"description" yaml:"description"`
Version string `json:"version" yaml:"version"`
}
Info contains API metadata
type MediaType ¶
type MediaType struct {
Schema *Schema `json:"schema" yaml:"schema"`
}
MediaType represents media type in request/response body
type OpenAPISpec ¶
type OpenAPISpec struct {
OpenAPI string `json:"openapi" yaml:"openapi"`
Paths map[string]PathItem `json:"paths" yaml:"paths"`
Info Info `json:"info" yaml:"info"`
Components *Components `json:"components,omitempty" yaml:"components,omitempty"`
}
OpenAPISpec represents an OpenAPI 3.x specification
func UnmarshalOpenAPISpec ¶
func UnmarshalOpenAPISpec(data []byte) (*OpenAPISpec, error)
UnmarshalOpenAPISpec unmarshals OpenAPI spec from data, automatically detecting format (JSON or YAML)
func UnmarshalOpenAPISpecFromJSON ¶
func UnmarshalOpenAPISpecFromJSON(data []byte) (*OpenAPISpec, error)
UnmarshalOpenAPISpecFromJSON unmarshals OpenAPI spec from JSON data
func UnmarshalOpenAPISpecFromYAML ¶
func UnmarshalOpenAPISpecFromYAML(data []byte) (*OpenAPISpec, error)
UnmarshalOpenAPISpecFromYAML unmarshals OpenAPI spec from YAML data
type Operation ¶
type Operation struct {
Summary string `json:"summary" yaml:"summary"`
Description string `json:"description" yaml:"description"`
Parameters []Parameter `json:"parameters,omitempty" yaml:"parameters,omitempty"`
RequestBody *RequestBody `json:"requestBody,omitempty" yaml:"requestBody,omitempty"`
}
Operation represents an API operation
type Parameter ¶
type Parameter struct {
Name string `json:"name" yaml:"name"`
In string `json:"in" yaml:"in"`
Description string `json:"description" yaml:"description"`
Required bool `json:"required" yaml:"required"`
Schema *Schema `json:"schema" yaml:"schema"`
Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"`
}
Parameter represents an API parameter
type PathItem ¶
type PathItem struct {
Get *Operation `json:"get" yaml:"get"`
Post *Operation `json:"post" yaml:"post"`
Put *Operation `json:"put" yaml:"put"`
Delete *Operation `json:"delete" yaml:"delete"`
Patch *Operation `json:"patch" yaml:"patch"`
}
PathItem represents a path in the OpenAPI spec
type RequestBody ¶
RequestBody represents the request body definition
type Schema ¶
type Schema struct {
Type string `json:"type" yaml:"type"`
Properties map[string]*Schema `json:"properties,omitempty" yaml:"properties,omitempty"`
Required []string `json:"required,omitempty" yaml:"required,omitempty"`
Format string `json:"format,omitempty" yaml:"format,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
}
Schema represents JSON Schema