hog

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

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

Go to latest
Published: Apr 18, 2022 License: MIT Imports: 11 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

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
}

func (*BodyBytes) AsBytes

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

func (*BodyBytes) AsBytesResponse

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

func (BodyBytes) AsMap

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

func (BodyBytes) AsString

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

func (*BodyBytes) AsStringResponse

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

func (*BodyBytes) Response

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

func (BodyBytes) ToStruct

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

func (*BodyBytes) ToStructResponse

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

type BodyForm

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

func (*BodyForm) AsBytes

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

func (*BodyForm) AsBytesResponse

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

func (BodyForm) AsMap

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

func (BodyForm) AsString

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

func (*BodyForm) AsStringResponse

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

func (*BodyForm) Response

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

func (BodyForm) ToStruct

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

func (*BodyForm) ToStructResponse

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

type BodyJson

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

func (*BodyJson) AsBytes

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

func (*BodyJson) AsBytesResponse

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

func (BodyJson) AsMap

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

func (BodyJson) AsString

func (h BodyJson) AsString() (result string, err error)

func (*BodyJson) AsStringResponse

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

func (*BodyJson) Response

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

func (BodyJson) ToStruct

func (h BodyJson) ToStruct(out interface{}) (err error)

func (*BodyJson) ToStructResponse

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

type BodyXml

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

func (*BodyXml) AsBytes

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

func (*BodyXml) AsBytesResponse

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

func (BodyXml) AsMap

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

func (BodyXml) AsString

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

func (*BodyXml) AsStringResponse

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

func (*BodyXml) MarshalSettings

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

func (*BodyXml) Response

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

func (BodyXml) ToStruct

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

func (*BodyXml) ToStructResponse

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

type HGet

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

func Get

func Get(url string) *HGet

func (*HGet) AsBytes

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

func (*HGet) AsBytesResponse

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

func (HGet) AsMap

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

func (*HGet) AsMapResponse

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

func (*HGet) AsString

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

func (*HGet) AsStringResponse

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

func (*HGet) Headers

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

func (*HGet) Query

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

func (*HGet) Response

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

func (*HGet) SetHeader

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

func (*HGet) SetValue

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

func (HGet) ToStruct

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

func (*HGet) ToStructResponse

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

type HMethod

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

type HPost

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

func Post

func Post(url string) *HPost

func (*HPost) Bytes

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

func (*HPost) Form

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

func (*HPost) Headers

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

func (*HPost) Json

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

func (*HPost) Query

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

func (*HPost) SetHeader

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

func (*HPost) SetValue

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

func (*HPost) Xml

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

type HPut

type HPut struct {
	HPost
}

func Put

func Put(url string) *HPut

func (*HPut) Bytes

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

func (*HPut) Form

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

func (*HPut) Headers

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

func (*HPut) Json

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

func (*HPut) Query

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

func (*HPut) SetHeader

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

func (*HPut) SetValue

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

func (*HPut) Xml

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

type Hog

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

func New

func New() *Hog

func NewClient

func NewClient(client http.Client) *Hog

func NewConfig

func NewConfig(secure bool, timeout int) *Hog

func (*Hog) Context

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

func (*Hog) Get

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

func (*Hog) Post

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

func (*Hog) Put

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

Jump to

Keyboard shortcuts

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