gnet

package module
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2023 License: MIT Imports: 16 Imported by: 18

README

gnet (an http client wrapper)

gnet is an http client package to make use of Go built-in net/http

Usage

This package is fully go-getable. So just type go get github.com/rosbit/gnet to install.

package main

import (
	"github.com/rosbit/gnet"
	"fmt"
)

func main() {
	params := map[string]interface{}{
		"a": "b",
		"c": 1,
	}
	headers := map[string]string{
		"X-Param": "x value",
	}

	status, _, resp, err := gnet.Http("http://yourname.com/path/to/url", gnet.DontReadRespBody())
	status, content, resp, err := gnet.Http("http://yourname.com/path/to/url")
	status, content, resp, err := gnet.Http("http://yourname.com/path/to/url", gnet.Params(params))
	status, content, resp, err := gnet.Http("http://yourname.com/path/to/url", gnet.Params(params), gnet.Headers(headers))
	/*
	// POST as request method
	status, _, resp, err := gnet.Http("http://yourname.com/path/to/url", gnet.M("post"), gnet.Params(params), gnet.DontReadRespBody())
	status, content, resp, err := gnet.Http("http://yourname.com/path/to/url", gnet.M("post"), gnet.Params(params))
	status, content, resp, err := gnet.Http("http://yourname.com/path/to/url", gnet.M("post"), gnet.Params(params), gnet.Headers(headers))
	// post body as a JSON 
	status, _, resp, err := gnet.JSON("http://yourname.com/path/to/url", gnet.Params(params), gnet.DontReadRespBody())
	status, content, resp, err := gnet.JSON("http://yourname.com/path/to/url", gnet.Params(params))
	status, content, resp, err := gnet.JSON("http://yourname.com/path/to/url", gnet.Params(params), gnet.Headers(headers))
	// post body as a JSON, even the method is GET
	status, content, resp, err := gnet.JSON("http://yourname.com/path/to/url", gnet.M("GET"), gnet.Params(params), gnet.Headers(headers))
	// request method is GET, request params as a FORM body
	status, content, resp, err := gnet.GetUsingBodyParams("http://yourname.com/path/to/url", gnet.Params(params), gnet.Headers(headers))
	*/
	if err != nil {
		fmt.Printf("%v\n", err)
		return
	}

	fmt.Printf("status: %d\n", status)
	fmt.Printf("reponse content: %s\n", string(content))
	respHeaders := gnet.GetHeaders(resp)
	for k, v := range respHeaders {
		fmt.Printf("%s: %s\n", k, v)
	}
}
Usage as fs
package main

import (
	"github.com/rosbit/gnet"
	"io"
	"os"
	"fmt"
)

func main() {
	// GET
	fp := gnet.Get("http://httpbin.org/get")
	defer fp.Close()
	io.Copy(os.Stdout, fp)

	// POST JSON
	fp2 := gnet.Post("http://httpbin.org/post", gnet.JSONCall(), gnet.Params(map[string]interface{}{"a": "b", "c": 1}))
	defer fp2.Close()
	io.Copy(os.Stdout, fp2)

	// with helper
	status, body, err := gnet.FsCall("http://httpbin.org/post", "POST", gnet.JSONCall(), gnet.Params(map[string]interface{}{"a": "b", "c": 1}))
	if err != nil {
		fmt.Printf("%v\n", err)
		return
	}
	fmt.Printf("status: %d\n", status)
	if body != nil {
		defer body.Close()
		io.Copy(os.Stdout, body)
	}
}
Usage with multi-baseurl
    multiBase, err := gnet.NewBaseUrl(gnet.BaseItem("http://192.168.0.241:8088"), gnet.BaseItem("http://httpbin.org"))
    if err != nil {
         // err
    }
    status, body, _, err := multiBase.Http("/post", gorul.M(http.MethodPost), gnet.Params(params), gorul.Headers(headers))
    multiBase.JSON("/post", gnet.Params(params), gnet.Headers(headers))
    gnet.JSON("/post", gnet.MultiBase(multiBase), gnet.Params(params), gnet.Headers(headers))
Status

The package is not fully tested, so be careful.

Contribution

Pull requests are welcome! Also, if you want to discuss something send a pull request with proposal and changes.

Convention: fork the repository and make changes on your fork in a feature branch.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FsCall

func FsCall(url string, method string, options ...Option) (status int, body io.ReadCloser, err error)

func FsCallAndParseJSON

func FsCallAndParseJSON(url string, method string, res interface{}, options ...Option) (status int, err error)

func GetHeaders

func GetHeaders(resp *http.Response) map[string]string

func GetLastModified

func GetLastModified(resp *http.Response) (time.Time, error)

func GetStatus

func GetStatus(resp *http.Response) (int, string)

func GetUsingBodyParams

func GetUsingBodyParams(url string, options ...Option) (status int, content []byte, resp *http.Response, err error)

func Http

func Http(url string, options ...Option) (status int, content []byte, resp *http.Response, err error)

func HttpCall

func HttpCall(url string, options ...Option) (int, io.ReadCloser, error)

func HttpCallJ

func HttpCallJ(url string, res interface{}, options ...Option) (int, error)

func JSON

func JSON(url string, options ...Option) (status int, content []byte, resp *http.Response, err error)

func JSONCallJ

func JSONCallJ(url string, res interface{}, options ...Option) (int, error)

func JsonCall

func JsonCall(url string, options ...Option) (int, io.ReadCloser, error)

func ModTime

func ModTime(rawurl string) (modTim time.Time, err error)

Types

type BaseItemT added in v0.0.2

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

func BaseItem

func BaseItem(baseUrl string, weight ...uint) BaseItemT

type BaseUrl

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

func NewBaseUrl

func NewBaseUrl(baseItem ...BaseItemT) (b *BaseUrl, err error)

func NewBaseUrl2 added in v0.0.2

func NewBaseUrl2(baseUrl ...string) (b *BaseUrl, err error)

func (*BaseUrl) GetWithBody

func (b *BaseUrl) GetWithBody(uri string, options ...Option) (status int, content []byte, resp *http.Response, err error)

func (*BaseUrl) Http

func (b *BaseUrl) Http(uri string, options ...Option) (status int, content []byte, resp *http.Response, err error)

func (*BaseUrl) JSON

func (b *BaseUrl) JSON(uri string, options ...Option) (status int, content []byte, resp *http.Response, err error)

type File

type File struct {
	Result
	// contains filtered or unexported fields
}

---- implementation of fs.File ----

func Delete

func Delete(url string, options ...Option) *File

func Get

func Get(url string, options ...Option) *File
func Head(url string, options ...Option) *File

func HttpRequest

func HttpRequest(url string, options ...Option) *File

func Post

func Post(url string, options ...Option) *File

func Put

func Put(url string, options ...Option) *File

func (*File) Close

func (f *File) Close() error

func (*File) Read

func (f *File) Read(p []byte) (int, error)

func (*File) Stat

func (f *File) Stat() (*FileInfo, error)

type FileInfo

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

---- implementation of fs.FileInfo ----

func (*FileInfo) IsDir

func (fi *FileInfo) IsDir() bool

abbreviation for Mode().IsDir()

func (*FileInfo) ModTime

func (fi *FileInfo) ModTime() time.Time

modification time

func (*FileInfo) Name

func (fi *FileInfo) Name() string

base name of the file

func (*FileInfo) Size

func (fi *FileInfo) Size() int64

length in bytes for regular files; system-dependent for others

func (*FileInfo) Sys

func (fi *FileInfo) Sys() interface{}

underlying data source (can return nil)

type FnCall

type FnCall func(url string, options ...Option) (status int, body io.ReadCloser, err error)

type FnCallJ

type FnCallJ func(url string, res interface{}, options ...Option) (status int, err error)

type HttpFunc

type HttpFunc func(url string, options ...Option) (int, []byte, *http.Response, error)

type Option

type Option func(*Options)

func BasicAuth added in v0.0.8

func BasicAuth(userName, password string) Option

func BodyLogger

func BodyLogger(writer io.Writer) Option

func DontReadRespBody

func DontReadRespBody() Option

func Headers

func Headers(headers map[string]string) Option

func JSONCall

func JSONCall() Option

func M

func M(method string) Option

func MultiBase

func MultiBase(multiBase *BaseUrl) Option

func Params

func Params(params interface{}) Option

func WithCaCert added in v0.0.9

func WithCaCert(caCert []byte) Option

func WithCaCertFile added in v0.0.9

func WithCaCertFile(caCertFile string) Option

func WithTLSCertFiles added in v0.0.9

func WithTLSCertFiles(certPemFile, keyPemFile string) Option

func WithTLSCerts added in v0.0.9

func WithTLSCerts(certPEMBlock, keyPEMBlock []byte) Option

func WithTimeout

func WithTimeout(timeout int) Option

func WithTimeoutDuration added in v0.0.13

func WithTimeoutDuration(timeout time.Duration) Option

type Options

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

type Request

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

func NewHttpsRequest

func NewHttpsRequest(options ...Option) *Request

func NewHttpsRequestWithCerts

func NewHttpsRequestWithCerts(certPemFile, keyPemFile string, options ...Option) (*Request, error)

func NewRequest

func NewRequest(options ...Option) *Request

func (*Request) GetClient added in v0.0.10

func (g *Request) GetClient() *http.Client

func (*Request) GetUsingBodyParams

func (g *Request) GetUsingBodyParams(url string, params interface{}, header map[string]string) (status int, content []byte, resp *http.Response, err error)

func (*Request) Http

func (g *Request) Http(url, method string, params interface{}, header map[string]string) (status int, content []byte, resp *http.Response, err error)

func (*Request) JSON

func (g *Request) JSON(url, method string, params interface{}, header map[string]string) (status int, content []byte, resp *http.Response, err error)

type Result

type Result struct {
	Status int
	Resp   *http.Response
	Err    error
}

result of HTTP response, returned by FileInfo.Sys()

Jump to

Keyboard shortcuts

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