httptpl

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 26, 2026 License: MIT Imports: 20 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	TypeDefinition = "definition"  // request definition spec/template files
	TypeHttpClient = "http-client" // a IDE http-client file
)

Templates type constants const TypeTemplate = "template" // request definition template files

View Source
const BodySplit = "\n\n"
View Source
const HcFileExt = ".http"

HcFileExt name

View Source
const RequestSplit = "\n###"
View Source
const StartPrefix = "###"

Variables

This section is empty.

Functions

This section is empty.

Types

type DomainConfig

type DomainConfig struct {
	// Name domain name
	Name string `json:"name"`
	// ConfigFile domain config file path
	ConfigFile string `json:"config_file"`
	// PathResolver handler
	PathResolver func(path string) string `json:"-"`

	// TplDir path for request templates.
	//
	// default is: {ConfigFile Dir}/{domainName}
	TplDir string `json:"tpl_dir"`
	// TplExt allowed template file ext list
	TplExt []string `json:"tpl_ext"`

	// Vars global variables
	Vars maputil.Data `json:"vars"`
	// Header global headers for send request
	Header map[string]string `json:"header"`
	// DefaultEnv name
	DefaultEnv string `json:"default_env"`
	// EnvFile The ide http-client.env.json file path
	EnvFile string `json:"env_file"`
	// Envs definition
	Envs EnvsMap `json:"envs"`
	// contains filtered or unexported fields
}

DomainConfig struct. name: domain, topic, collection

func NewDomainConfig

func NewDomainConfig(name, configFile string) *DomainConfig

NewDomainConfig instance

func (*DomainConfig) BuildVars

func (d *DomainConfig) BuildVars(envName, envFile string) (maputil.Data, error)

BuildVars by env name and file

func (*DomainConfig) DefaultTemplates

func (d *DomainConfig) DefaultTemplates() *Templates

DefaultTemplates get by name

func (*DomainConfig) EnvVars

func (d *DomainConfig) EnvVars(env string) maputil.Data

EnvVars get by env name

func (*DomainConfig) Init

func (d *DomainConfig) Init() error

Init some info, load domain config

func (*DomainConfig) Load

func (d *DomainConfig) Load() error

Load templates

func (*DomainConfig) LoadConfig

func (d *DomainConfig) LoadConfig() error

LoadConfig file and env file

func (*DomainConfig) Lookup

func (d *DomainConfig) Lookup(group, name string) (*Template, error)

Lookup get by name and group

func (*DomainConfig) LookupVars

func (d *DomainConfig) LookupVars(env string) (maputil.Data, bool)

LookupVars get by env name

func (*DomainConfig) Template

func (d *DomainConfig) Template(name string) *Template

Template get by file name, will find from tsMap[DomainConfig.Name]

func (*DomainConfig) Templates

func (d *DomainConfig) Templates(group string) (*Templates, bool)

Templates get by name.

Usage:

d.Templates("") // get default
d.Templates("jenkins.http")

type EnvsMap

type EnvsMap map[string]maputil.Data

EnvsMap type

func LoadEnvsByFile

func LoadEnvsByFile(envFile string) (EnvsMap, error)

LoadEnvsByFile handle

func (EnvsMap) Merge

func (m EnvsMap) Merge(s EnvsMap)

Merge other env map

type Manager

type Manager struct {

	// DefaultExt default config file ext, for auto load domain config
	//
	// Allow: json, json5, yaml
	DefaultExt string `json:"default_ext"`
	// DefaultDir default domain config file dir, for auto load domain config
	//
	// path format: {DefaultDir}/{domainName}-domain.{DefaultExt}
	DefaultDir string `json:"default_dir"`
	// Domains config definitions
	Domains map[string]*DomainConfig `json:"domains"`
	// PathResolver handler
	PathResolver func(path string) string
}

Manager HTTP tpl manager

func NewManager

func NewManager() *Manager

NewManager instance

func (*Manager) Domain

func (m *Manager) Domain(name string) (*DomainConfig, error)

Domain get

func (*Manager) Init

func (m *Manager) Init() error

Init some info. should call it before use

type Response

type Response struct {
	Status      int    `json:"status"`
	Struct      any    `json:"struct"`
	ContentType string `json:"content_type"`
	// Required fields in response.
	Required []string `json:"required"`
	// Asserts {name: "$.name == inhere"}
	Asserts map[string]any `json:"asserts"`
}

Response struct

type Template

type Template struct {

	// Index value
	Index int

	// Version info
	Version string `json:"version"`
	Kind    string `json:"kind"`
	Name    string `json:"name"`
	Desc    string `json:"desc"`

	// URL string for request remote, full URL.
	URL    string `json:"url"`
	Method string `json:"method"`
	// Query for request url
	Query map[string]any `json:"query"`
	// Header for request
	Header map[string]string `json:"header"`
	// BasicAuth config
	BasicAuth *httpreq.BasicAuthConf `json:"basic_auth"`
	// Timeout for request, unit: ms
	Timeout int `json:"timeout"`

	// Body for request
	Body any `json:"body"`
	// JSON body data, will auto add content type
	JSON any `json:"json"`
	// Form body data, will auto add content type
	Form map[string]any `json:"form"`
	// BodyFile will read file contents as body
	BodyFile string `json:"body_file"`

	// BeforeSend hook
	BeforeSend func(r *http.Request, body *bytes.Buffer) `json:"-"`
	// AfterSend hook
	AfterSend func(w *httpreq.Resp, err error) `json:"-"`

	// Resp http response data check
	Resp *httpreq.Resp `json:"response"`
	// contains filtered or unexported fields
}

Template definition for http request

TIP:

allow use vars in URL, Query, Header, Body, JSON, Form and BodyFile.

func NewTemplate

func NewTemplate() *Template

NewTemplate instance

func ParseAsTemplate

func ParseAsTemplate(s string) *Template

func (*Template) BuildRequest

func (t *Template) BuildRequest(vars maputil.Data, hs map[string]string) (*http.Request, error)

BuildRequest instance

func (*Template) BuildRequestBody

func (t *Template) BuildRequestBody(vars maputil.Data) (io.Reader, error)

BuildRequestBody for request

func (*Template) ContentType

func (t *Template) ContentType() string

ContentType get

func (*Template) FromHCString

func (t *Template) FromHCString(s string) error

FromHCString parse from hc-file request part contents

func (*Template) FromJSONBytes

func (t *Template) FromJSONBytes(bs []byte) error

FromJSONBytes init definition template

func (*Template) FromJSONFile

func (t *Template) FromJSONFile(p string) error

FromJSONFile init definition template

func (*Template) FromJSONString

func (t *Template) FromJSONString(s string) error

FromJSONString init definition template

func (*Template) Path

func (t *Template) Path() string

Path of template file

func (*Template) RequestString

func (t *Template) RequestString(vars maputil.Data) string

func (*Template) Send

func (t *Template) Send(vars maputil.Data, hs map[string]string, opt *httpreq.Option) error

Send request with variables

func (*Template) SetTimeout

func (t *Template) SetTimeout(ms int)

SetTimeout for request

func (*Template) String

func (t *Template) String() string

String of the request template

func (*Template) Type

func (t *Template) Type() string

Type name

type Templates

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

Templates request set definitions for a group

func NewTemplates

func NewTemplates(name string) *Templates

NewTemplates instance

func ParseAsTemplates

func ParseAsTemplates(s string) *Templates

func ParseClientFile

func ParseClientFile(fpath string) *Templates

func TemplateFile

func TemplateFile(fpath string) *Templates

func (*Templates) All

func (ts *Templates) All() map[string]*Template

All templates

func (*Templates) Each

func (ts *Templates) Each(fn func(t *Template))

Each template

func (*Templates) FindOne

func (ts *Templates) FindOne(keywords string) *Template

func (*Templates) FromHCFile

func (ts *Templates) FromHCFile(hcFile string) error

func (*Templates) FromHCString

func (ts *Templates) FromHCString(s string) error

func (*Templates) Get

func (ts *Templates) Get(name string) *Template

func (*Templates) GetByPath

func (ts *Templates) GetByPath(path string) *Template

GetByPath get Template by request uri path.

func (*Templates) IsDefType

func (ts *Templates) IsDefType() bool

IsDefType templates

func (*Templates) IsHcType

func (ts *Templates) IsHcType() bool

IsHcType templates

func (*Templates) IsTplType

func (ts *Templates) IsTplType() bool

IsTplType templates

func (*Templates) LoadAll

func (ts *Templates) LoadAll() error

LoadAll request definition template files

func (*Templates) LoadTemplate

func (ts *Templates) LoadTemplate(name string) (*Template, error)

LoadTemplate file and register

func (*Templates) Lookup

func (ts *Templates) Lookup(name string) (*Template, error)

func (*Templates) Search

func (ts *Templates) Search(keywords string, limit int) []*Template

func (*Templates) Send

func (ts *Templates) Send(keywords string) error

func (*Templates) String

func (ts *Templates) String() string

Jump to

Keyboard shortcuts

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