fetch

package module
v0.0.0-...-4b92eff Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2023 License: MIT Imports: 10 Imported by: 2

README

fork from github.com/rodkranz/fetch

Install

Default

go get git.jdev.run/pkg/fetch

Import

import (
  "git.jdev.run/pkg/fetch"
)

Test

To run the project test

go test -v --cover

Example:

Simple
client := fetch.NewDefault()
response, err := client.Get("http://www.google.com/", nil)
Custom Headers
opt := fetch.Options{
    Header: http.Header{
        "Content-Type": []string{"application/json"},
        "User-Agent":   []string{"XPTO-Agent-user"},
    },
}

f := fetch.New(&opt)
rsp, err := f.GetWithContext(context.Background(), "http://www.google.com", nil)
Simple JSON POST
login := map[string]interface{}{
	"username": "rodkranz",
	"password": "loremIpsum",
}
response, err := fetch.NewDefault().
		IsJSON().
		Post("http://www.google.com/", fetch.NewReader(login))

Documentation

Index

Constants

View Source
const DefaultTimeout = time.Duration(30 * time.Second)

DefaultTimeout defined timeout default for any request

Variables

View Source
var ErrEmptyBody = fmt.Errorf("the body of response is empty")

ErrEmptyBody returns when there is no body to read

Functions

func MustBytes

func MustBytes(b []byte, _ error) []byte

MustBytes ignore any error and return a slice of bytes

func MustString

func MustString(s string, _ error) string

MustString ignore any error and return a string

func NewReader

func NewReader(input interface{}) *strings.Reader

NewReader this function will return a new reader if the format is JSON Valid format it will be convert before send, if is not json will send as come. Use a POINT for input variable

func NewStructIO deprecated

func NewStructIO(input interface{}) *strings.Reader

NewStructIO this function will create a new reader for your request. if input is json format will convert to json or send directly

Deprecated: Use NewReader instead.

Types

type Fetch

type Fetch struct {
	*http.Client
	Option *Options
}

Fetch use http default but defined with a timeout.

func New

func New(opt *Options) *Fetch

New get new fetcher and you need to specify the netTransport.

func NewDefault

func NewDefault() *Fetch

NewDefault get fetcher with netTransport and timeout defined

func (*Fetch) Delete

func (f *Fetch) Delete(url string, reader io.Reader) (*Response, error)

Delete do request with HTTP using HTTP Verb DELETE

func (*Fetch) DeleteWithContext

func (f *Fetch) DeleteWithContext(ctx context.Context, url string, reader io.Reader) (*Response, error)

DeleteWithContext execute DoWithContext but define request to method DELETE

func (*Fetch) Do

func (f *Fetch) Do(req *http.Request) (*Response, error)

Do execute any kind of request

func (*Fetch) DoWithContext

func (f *Fetch) DoWithContext(ctx context.Context, req *http.Request) (*Response, error)

DoWithContext execute any kind of request passing context

func (*Fetch) Get

func (f *Fetch) Get(url string, reader io.Reader) (*Response, error)

Get do request with HTTP using HTTP Verb GET

func (*Fetch) GetWithContext

func (f *Fetch) GetWithContext(ctx context.Context, url string, reader io.Reader) (*Response, error)

GetWithContext execute DoWithContext but define request to method GET

func (*Fetch) IsJSON

func (f *Fetch) IsJSON() *Fetch

IsJSON add Content-Type as JSON in header.

func (*Fetch) Options

func (f *Fetch) Options(url string, reader io.Reader) (*Response, error)

Options do request with HTTP using HTTP Verb OPTIONS

func (*Fetch) OptionsWithContext

func (f *Fetch) OptionsWithContext(ctx context.Context, url string, reader io.Reader) (*Response, error)

OptionsWithContext execute DoWithContext but define request to method OPTIONS

func (*Fetch) Patch

func (f *Fetch) Patch(url string, reader io.Reader) (*Response, error)

Patch do request with HTTP using HTTP Verb PATCH

func (*Fetch) PatchWithContext

func (f *Fetch) PatchWithContext(ctx context.Context, url string, reader io.Reader) (*Response, error)

PatchWithContext execute DoWithContext but define request to method PATCH

func (*Fetch) Post

func (f *Fetch) Post(url string, reader io.Reader) (*Response, error)

Post do request with HTTP using HTTP Verb POST

func (*Fetch) PostWithContext

func (f *Fetch) PostWithContext(ctx context.Context, url string, reader io.Reader) (*Response, error)

PostWithContext execute DoWithContext but define request to method POST

func (*Fetch) Put

func (f *Fetch) Put(url string, reader io.Reader) (*Response, error)

Put do request with HTTP using HTTP Verb PUT

func (*Fetch) PutWithContext

func (f *Fetch) PutWithContext(ctx context.Context, url string, reader io.Reader) (*Response, error)

PutWithContext execute DoWithContext but define request to method PUT

type Options

type Options struct {
	Header    http.Header
	Timeout   time.Duration
	Host      string
	Transport *http.Transport
}

Options default for any request in client

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions returns options with timeout defined

type Response

type Response struct {
	*http.Response
	// contains filtered or unexported fields
}

Response helper work with response from http.Client

func (*Response) BodyIsEmpty

func (r *Response) BodyIsEmpty() bool

BodyIsEmpty return if body is empty or not.

func (*Response) Bytes

func (r *Response) Bytes() (_ []byte, err error)

Bytes return the Response in array of bytes.

func (*Response) Decode

func (r *Response) Decode(i interface{}) error

Decode body result into interface object.

func (*Response) String

func (r *Response) String() (s string)

String return the Response in string format. If has any error will return errors as string.

func (*Response) ToString

func (r *Response) ToString() (string, error)

String return the Response in string format or error.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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