hog

package module
v0.0.0-...-afb977d Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2025 License: MIT Imports: 14 Imported by: 0

README

Go Report Card GoDoc

hog is a library which provides a set of extensions on Go's standard http.Client library, so all interfaces are just a syntax sugar on the standard ones.

Major additional concepts are:

  • SOE: Simple, Original, Extended

  • Native http client types where is it possible, like: http.Header, url.Values

  • Possibility to set original http.Client to painless integration of existing codebase: h := hog.NewClient(client)

  • hog.Get("https://httpbin.org/get") and hog.Post("https://httpbin.org/post") to quick result

  • Possibility to get original http.Response: hog.Get("https://httpbin.org/get").Response()

Usage

Simple

Get request as string result:

package main

import (
	"fmt"
	"github.com/aaapi-net/hog"
)

func main() {
	result, err := hog.Get("https://httpbin.org/get").AsString()
	fmt.Println(result, err)
}

Post request with json body as map result:

package main

import (
	"fmt"
	"net/url"
	"github.com/aaapi-net/hog"
)

func main() {
	result, err := hog.Post("https://httpbin.org/get").
		Form(url.Values{
			"name": {"alice"},
			"age":  {"16"},
		}).
		AsMap()

	fmt.Println(result, err)
}

Original

Get request as http.Response result:

package main

import (
	"fmt"
	"github.com/aaapi-net/hog"
)

func main() {
	originalResponse, err := hog.Get("https://httpbin.org/get").Response()
	fmt.Println(originalResponse.Status, err)
}

Get request as []byte and http.Response result:

package main

import (
	"fmt"
	"github.com/aaapi-net/hog"
)

func main() {
	bytes, originalResponse, err := hog.Get("https://httpbin.org/get").AsBytesResponse()
	fmt.Println(string(bytes), originalResponse.StatusCode, err)
}

Extended

Get Request as string result:

package main

import (
	"context"
	"fmt"
	"github.com/aaapi-net/hog"
)

func main() {
	h := hog.New()
    
	result, err := h.
                Context(context.Background()).
                Get("https://httpbin.org/get").
                SetValue("id", "777"). // https://httpbin.org/get?id=777
                SetHeader("go", "lang").
                AsString()
	
	fmt.Println(result, err)
}

Post Request With Json Body as string result:

package main

import (
	"context"
	"fmt"
	"net/http"
	"net/url"
	"github.com/aaapi-net/hog"
)

func main() {
	h := hog.NewConfig(true, 60)    // secure: true, timeout: 60

	result, err := h.
		Context(context.Background()).
		Post("https://httpbin.org/post").
		// set all query values in one function
		Query(url.Values{"firstName": []string{"Joe"}, "lastName": []string{"Doe"}}). // https://httpbin.org/post?firstName=Joe&lastName=Doe
		// multiple headers in one function
		Headers(http.Header{"go": {"lang", "is", "awesome"}}).
		// interface{} as json body: {"name": "alice", "age": 16}
		Json(map[string]interface{}{
			"name": "alice",
			"age":  16,
		}).AsString()

	fmt.Println(result, err)
}

hog is a usable go http.Client

Alternatives

https://github.com/dghubble/sling

https://github.com/nahid/gohttp

https://github.com/cizixs/gohttp

https://github.com/BRUHItsABunny/gOkHttp

https://github.com/franela/goreq

Also see

  • hog a usable go http.Client
  • liam a mail go library

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BodyBytes

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

BodyBytes handles binary request bodies.

func (*BodyBytes) AsBytes

func (bb *BodyBytes) AsBytes() (result []byte, err error)

AsBytes executes the request and returns the response body as bytes.

func (*BodyBytes) AsBytesResponse

func (bb *BodyBytes) AsBytesResponse() (result []byte, response *http.Response, err error)

AsBytesResponse executes the request and returns the response body as bytes along with the response.

func (*BodyBytes) AsMap

func (bb *BodyBytes) AsMap() (result map[string]interface{}, err error)

AsMap unmarshals the response body into a map[string]interface{}.

func (*BodyBytes) AsString

func (bb *BodyBytes) AsString() (result string, err error)

AsString executes the request and returns the response body as a string.

func (*BodyBytes) AsStringResponse

func (bb *BodyBytes) AsStringResponse() (result string, response *http.Response, err error)

AsStringResponse executes the request and returns the response body as a string along with the response.

func (*BodyBytes) Response

func (bb *BodyBytes) Response() (response *http.Response, err error)

Response executes the request with binary body and returns the raw response.

func (*BodyBytes) ToStruct

func (bb *BodyBytes) ToStruct(out interface{}) (err error)

ToStruct unmarshals the response body into the provided struct. The out parameter should be a pointer to the target struct.

func (*BodyBytes) ToStructResponse

func (bb *BodyBytes) ToStructResponse(out interface{}) (response *http.Response, err error)

ToStructResponse unmarshals the response body into the provided struct and returns the response. The out parameter should be a pointer to the target struct.

type BodyForm

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

BodyForm handles form urlencoded request bodies.

func (*BodyForm) AsBytes

func (bf *BodyForm) AsBytes() (result []byte, err error)

AsBytes executes the request and returns the response body as bytes.

func (*BodyForm) AsBytesResponse

func (bf *BodyForm) AsBytesResponse() (result []byte, response *http.Response, err error)

AsBytesResponse executes the request and returns the response body as bytes along with the response.

func (*BodyForm) AsMap

func (bf *BodyForm) AsMap() (result map[string]interface{}, err error)

AsMap unmarshals the response body into a map[string]interface{}.

func (*BodyForm) AsString

func (bf *BodyForm) AsString() (result string, err error)

AsString executes the request and returns the response body as a string.

func (*BodyForm) AsStringResponse

func (bf *BodyForm) AsStringResponse() (result string, response *http.Response, err error)

AsStringResponse executes the request and returns the response body as a string along with the response.

func (*BodyForm) Response

func (bf *BodyForm) Response() (response *http.Response, err error)

Response executes the request with form body and returns the raw response.

func (*BodyForm) ToStruct

func (bf *BodyForm) ToStruct(out interface{}) (err error)

ToStruct unmarshals the response body into the provided struct. The out parameter should be a pointer to the target struct.

func (*BodyForm) ToStructResponse

func (bf *BodyForm) ToStructResponse(out interface{}) (response *http.Response, err error)

ToStructResponse unmarshals the response body into the provided struct and returns the response. The out parameter should be a pointer to the target struct.

type BodyJson

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

BodyJson handles JSON request bodies with appropriate Content-Type headers.

func (*BodyJson) AsBytes

func (bj *BodyJson) AsBytes() (result []byte, err error)

AsBytes executes the request and returns the response body as bytes.

func (*BodyJson) AsBytesResponse

func (bj *BodyJson) AsBytesResponse() (result []byte, response *http.Response, err error)

AsBytesResponse executes the request and returns the response body as bytes along with the response.

func (*BodyJson) AsMap

func (bj *BodyJson) AsMap() (result map[string]interface{}, err error)

AsMap unmarshals the response body into a map[string]interface{}.

func (*BodyJson) AsString

func (bj *BodyJson) AsString() (result string, err error)

AsString executes the request and returns the response body as a string.

func (*BodyJson) AsStringResponse

func (bj *BodyJson) AsStringResponse() (result string, response *http.Response, err error)

AsStringResponse executes the request and returns the response body as a string along with the response.

func (*BodyJson) Response

func (bj *BodyJson) Response() (response *http.Response, err error)

Response executes the request with JSON body and returns the raw response.

func (*BodyJson) ToStruct

func (bj *BodyJson) ToStruct(out interface{}) (err error)

ToStruct unmarshals the response body into the provided struct. The out parameter should be a pointer to the target struct.

func (*BodyJson) ToStructResponse

func (bj *BodyJson) ToStructResponse(out interface{}) (response *http.Response, err error)

ToStructResponse unmarshals the response body into the provided struct and returns the response. The out parameter should be a pointer to the target struct.

type BodyXml

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

BodyXml handles XML request bodies with appropriate Content-Type headers.

func (*BodyXml) AsBytes

func (bx *BodyXml) AsBytes() (result []byte, err error)

AsBytes executes the request and returns the response body as bytes.

func (*BodyXml) AsBytesResponse

func (bx *BodyXml) AsBytesResponse() (result []byte, response *http.Response, err error)

AsBytesResponse executes the request and returns the response body as bytes along with the response.

func (*BodyXml) AsMap

func (bx *BodyXml) AsMap() (result map[string]interface{}, err error)

AsMap unmarshals the response body into a map[string]interface{}.

func (*BodyXml) AsString

func (bx *BodyXml) AsString() (result string, err error)

AsString executes the request and returns the response body as a string.

func (*BodyXml) AsStringResponse

func (bx *BodyXml) AsStringResponse() (result string, response *http.Response, err error)

AsStringResponse executes the request and returns the response body as a string along with the response.

func (*BodyXml) MarshalSettings

func (bx *BodyXml) MarshalSettings(prefix, indent string) *BodyXml

MarshalSettings configures XML marshaling with the specified prefix and indent.

func (*BodyXml) Response

func (bx *BodyXml) Response() (response *http.Response, err error)

Response executes the request with XML body and returns the raw response.

func (*BodyXml) ToStruct

func (bx *BodyXml) ToStruct(out interface{}) (err error)

ToStruct unmarshals the response body into the provided struct. The out parameter should be a pointer to the target struct.

func (*BodyXml) ToStructResponse

func (bx *BodyXml) ToStructResponse(out interface{}) (response *http.Response, err error)

ToStructResponse unmarshals the response body into the provided struct and returns the response. The out parameter should be a pointer to the target struct.

type Error

type Error struct {
	Op      string // Operation where the error occurred
	Message string // Human-readable error description
	Err     error  // Underlying error, if any
}

Error represents an HTTP request processing error with context.

func (*Error) Error

func (e *Error) Error() string

Error returns a formatted error string.

type HGet

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

func Get

func Get(url string) *HGet

func GetF

func GetF(format string, a ...any) *HGet

func (*HGet) AsBytes

func (h *HGet) AsBytes() (result []byte, err error)

AsBytes executes the GET request and returns the response body as bytes.

func (*HGet) AsBytesResponse

func (h *HGet) AsBytesResponse() (result []byte, response *http.Response, err error)

AsBytesResponse executes the GET request and returns the response body as bytes along with the response.

func (*HGet) AsMap

func (h *HGet) AsMap() (result map[string]interface{}, err error)

AsMap unmarshals the GET response body into a map[string]interface{}.

func (*HGet) AsMapResponse

func (h *HGet) AsMapResponse() (result map[string]interface{}, response *http.Response, err error)

AsMapResponse unmarshals the GET response body into a map and returns it along with the response.

func (*HGet) AsString

func (h *HGet) AsString() (result string, err error)

AsString executes the GET request and returns the response body as a string.

func (*HGet) AsStringResponse

func (h *HGet) AsStringResponse() (result string, response *http.Response, err error)

AsStringResponse executes the GET request and returns the response body as a string along with the response.

func (*HGet) Headers

func (h *HGet) Headers(headers http.Header) *HGet

Headers sets all headers for this GET request.

func (*HGet) Query

func (h *HGet) Query(query url.Values) *HGet

Query sets query parameters for this GET request.

func (*HGet) Response

func (h *HGet) Response() (response *http.Response, err error)

Response executes the GET request and returns the raw http.Response.

func (*HGet) SetHeader

func (h *HGet) SetHeader(key, value string) *HGet

SetHeader adds or replaces a single header for this GET request.

func (*HGet) SetValue

func (h *HGet) SetValue(key, value string) *HGet

SetValue adds or updates a single query parameter.

func (*HGet) ToStruct

func (h *HGet) ToStruct(out interface{}) (err error)

ToStruct unmarshals the GET response body into the provided struct. The out parameter should be a pointer to the target struct.

func (*HGet) ToStructResponse

func (h *HGet) ToStructResponse(out interface{}) (response *http.Response, err error)

ToStructResponse unmarshals the GET response body into the provided struct and returns the response. The out parameter should be a pointer to the target struct.

type HMethod

type HMethod interface {
	// contains filtered or unexported methods
}

HMethod defines the interface for HTTP methods with common functionality.

type HPost

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

HPost handles HTTP POST requests with chainable options.

func Post

func Post(url string) *HPost

func PostF

func PostF(url string, a ...any) *HPost

func (*HPost) Bytes

func (h *HPost) Bytes(body []byte) *BodyBytes

Bytes sets a binary request body.

func (*HPost) Form

func (h *HPost) Form(body url.Values) *BodyForm

Form sets a form urlencoded request body.

func (*HPost) Headers

func (h *HPost) Headers(headers http.Header) *HPost

Headers sets all headers for this POST request.

func (*HPost) Json

func (h *HPost) Json(body interface{}) *BodyJson

Json sets a JSON request body.

func (*HPost) Query

func (h *HPost) Query(query url.Values) *HPost

Query sets query parameters for this POST request.

func (*HPost) SetHeader

func (h *HPost) SetHeader(key, value string) *HPost

SetHeader adds or replaces a single header for this POST request.

func (*HPost) SetValue

func (h *HPost) SetValue(key, value string) *HPost

SetValue adds or updates a single query parameter.

func (*HPost) Xml

func (h *HPost) Xml(body interface{}) *BodyXml

Xml sets an XML request body.

type HPut

type HPut struct {
	HPost
}

HPut handles HTTP PUT requests with chainable options.

func Put

func Put(url string) *HPut

func PutF

func PutF(url string, a ...any) *HPut

func (*HPut) Bytes

func (h *HPut) Bytes(body []byte) *BodyBytes

Bytes sets a binary request body.

func (*HPut) Form

func (h *HPut) Form(body url.Values) *BodyForm

Form sets a form urlencoded request body.

func (*HPut) Headers

func (h *HPut) Headers(headers http.Header) *HPut

Headers sets all headers for this PUT request.

func (*HPut) Json

func (h *HPut) Json(body interface{}) *BodyJson

Json sets a JSON request body.

func (*HPut) Query

func (h *HPut) Query(query url.Values) *HPut

Query sets query parameters for this PUT request.

func (*HPut) SetHeader

func (h *HPut) SetHeader(key, value string) *HPut

SetHeader adds or replaces a single header for this PUT request.

func (*HPut) SetValue

func (h *HPut) SetValue(key, value string) *HPut

SetValue adds or updates a single query parameter.

func (*HPut) Xml

func (h *HPut) Xml(body interface{}) *BodyXml

Xml sets an XML request body.

type Hog

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

Hog is a flexible HTTP client that provides a chainable API for HTTP requests.

func New

func New() *Hog

New creates a new Hog instance with default secure configuration.

func NewClient

func NewClient(client http.Client) *Hog

NewClient creates a new Hog instance using an existing http.Client.

func NewConfig

func NewConfig(secure bool, timeout int) *Hog

NewConfig creates a new Hog instance with specified TLS security and timeout. The secure parameter determines if TLS verification is enabled, and timeout is in seconds.

func (*Hog) Context

func (h *Hog) Context(context context.Context) *Hog

Context sets the request context.

func (*Hog) Get

func (h *Hog) Get(url string) *HGet

Get initiates a GET request on this Hog instance.

func (*Hog) GetF

func (h *Hog) GetF(format string, a ...any) *HGet

func (*Hog) Headers

func (h *Hog) Headers(headers http.Header) *Hog

Headers sets all headers for the request.

func (*Hog) LogLevel

func (h *Hog) LogLevel(level LogLevel) *Hog

LogLevel sets the logging verbosity when using the default logger.

func (*Hog) Logger

func (h *Hog) Logger(logger Logger) *Hog

Logger sets a custom logger for the request.

func (*Hog) Post

func (h *Hog) Post(url string) *HPost

Post initiates a POST request on this Hog instance.

func (*Hog) PostF

func (h *Hog) PostF(format string, a ...any) *HPost

PostF initiates a POST request with a formatted URL string.

func (*Hog) Put

func (h *Hog) Put(url string) *HPut

Put initiates a PUT request on this Hog instance.

func (*Hog) PutF

func (h *Hog) PutF(format string, a ...any) *HPut

PutF initiates a PUT request with a formatted URL string.

func (*Hog) RetryCount

func (h *Hog) RetryCount(count int) *Hog

RetryCount sets the number of retry attempts for failed requests.

func (*Hog) SetHeader

func (h *Hog) SetHeader(key, value string) *Hog

SetHeader adds or replaces a single header for the request.

type LogLevel

type LogLevel int

LogLevel defines the verbosity of logging.

const (
	LogLevelNone LogLevel = iota
	LogLevelError
	LogLevelInfo
	LogLevelDebug
)

type Logger

type Logger interface {
	// Debug logs debug level information.
	Debug(args ...interface{})

	// Info logs informational messages.
	Info(args ...interface{})

	// Error logs error information.
	Error(args ...interface{})

	// IsEnabled checks if a specific log level is active.
	IsEnabled(level LogLevel) bool
}

Logger defines methods for request/response logging.

Jump to

Keyboard shortcuts

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