wget

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2021 License: MIT Imports: 15 Imported by: 6

README

go-wget (http client wrapper)

go-wget is a 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/go-wget to install.

package main

import (
	"github.com/rosbit/go-wget"
	"fmt"
)

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

	status, content, resp, err := wget.Wget("http://yourname.com/path/to/url", "get", params, headers)
	/*
	// POST as request method
	status, content, resp, err := wget.Wget("http://yourname.com/path/to/url", "post", params, headers)
	// post body as a JSON 
	status, content, resp, err := wget.PostJson("http://yourname.com/path/to/url", "", params, headers)
	// post body as a JSON, even the method is GET
	status, content, resp, err := wget.PostJson("http://yourname.com/path/to/url", "GET", params, headers)
	// request method is GET, request params as a FORM body
	status, content, resp, err := wget.GetUsingBodyParams("http://yourname.com/path/to/url", params, 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 := wget.GetHeaders(resp)
	for k, v := range respHeaders {
		fmt.Printf("%s: %s\n", k, v)
	}
}
Usage as fs
package main

import (
	"github.com/rosbit/go-wget"
	"io"
	"os"
	"fmt"
)

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

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

	// with helper
	status, body, err := wget.FsCall("http://httpbin.org/post", "POST", &wget.Args{Params: map[string]interface{}{"a": "b", "c": 1}, JsonCall: true})
	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 := NewBaseUrl(BaseItem("http://192.168.0.241:8088"), BaseItem("http://httpbin.org"))
    if err != nil {
         // err
    }
    status, body, _, err := multiBase.HttpCall("/post", http.MethodPost, params, headers)
    multiBase.JsonCall("/post", http.MethodPost, params, headers)
    PostJson("/post", http.MethodPost, params, headers, Options{MultiBase:multiBase})
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

Overview

*

  • http client implementation
  • Rosbit Xu <me@rosbit.cn>
  • Jan. 8, 2018

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BaseItem added in v1.5.0

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

func FsCall added in v1.4.1

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

func FsCallAndParseJSON added in v1.4.1

func FsCallAndParseJSON(url string, method string, res interface{}, options ...*Args) (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, params interface{}, header map[string]string, options ...Options) (status int, content []byte, resp *http.Response, err error)

func HttpCall added in v1.3.0

func HttpCall(url string, method string, postData interface{}, headers map[string]string) (int, io.ReadCloser, error)

func HttpCallJ added in v1.3.0

func HttpCallJ(url string, method string, postData interface{}, headers map[string]string, res interface{}, options ...Options) (int, error)

func JsonCall added in v1.3.0

func JsonCall(url string, method string, jsonData interface{}, headers map[string]string) (int, io.ReadCloser, error)

func JsonCallJ added in v1.3.0

func JsonCallJ(url string, method string, jsonData interface{}, headers map[string]string, res interface{}, options ...Options) (int, error)

func ModTime

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

func PostJson

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

func Wget

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

Types

type Args added in v1.4.0

type Args struct {
	Params   interface{}
	Headers  map[string]string
	Timeout  int
	JsonCall bool
	Logger   io.Writer
}

arguments for HTTP request

type BaseUrl added in v1.5.0

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

func NewBaseUrl added in v1.5.0

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

func (*BaseUrl) GetWithBody added in v1.5.0

func (b *BaseUrl) GetWithBody(uri string, params interface{}, header map[string]string, options ...Options) (status int, content []byte, resp *http.Response, err error)

func (*BaseUrl) HttpCall added in v1.5.0

func (b *BaseUrl) HttpCall(uri, method string, params interface{}, header map[string]string, options ...Options) (status int, content []byte, resp *http.Response, err error)

func (*BaseUrl) JsonCall added in v1.5.0

func (b *BaseUrl) JsonCall(uri, method string, params interface{}, header map[string]string, options ...Options) (status int, content []byte, resp *http.Response, err error)

type File added in v1.4.0

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

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

func Delete added in v1.4.0

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

func Get added in v1.4.0

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

func HttpRequest added in v1.4.1

func HttpRequest(url string, method string, options ...*Args) *File

func Post added in v1.4.0

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

func Put added in v1.4.0

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

func (*File) Close added in v1.4.0

func (f *File) Close() error

func (*File) Read added in v1.4.0

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

func (*File) Stat added in v1.4.0

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

type FileInfo added in v1.4.0

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

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

func (*FileInfo) IsDir added in v1.4.0

func (fi *FileInfo) IsDir() bool

abbreviation for Mode().IsDir()

func (*FileInfo) ModTime added in v1.4.0

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

modification time

func (*FileInfo) Name added in v1.4.0

func (fi *FileInfo) Name() string

base name of the file

func (*FileInfo) Size added in v1.4.0

func (fi *FileInfo) Size() int64

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

func (*FileInfo) Sys added in v1.4.0

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

underlying data source (can return nil)

type FnCall added in v1.3.0

type FnCall func(url string, method string, params interface{}, headers map[string]string) (status int, body io.ReadCloser, err error)

type FnCallJ added in v1.3.0

type FnCallJ func(url string, method string, params interface{}, headers map[string]string, res interface{}, options ...Options) (status int, err error)

type HttpFunc

type HttpFunc func(string, string, interface{}, map[string]string, ...Options) (int, []byte, *http.Response, error)

type Options added in v1.2.1

type Options struct {
	Timeout          int  // timeout in seconds to wait while connect/send/recv-ing
	DontReadRespBody bool // if it is true, it's your resposibility to get body from http.Response.Body
	DebugWriter      io.Writer
	MultiBase        *BaseUrl
}

type Request

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

func NewHttpsRequest

func NewHttpsRequest(connectTimeout int, options ...Options) *Request

func NewHttpsRequestWithCerts added in v1.0.3

func NewHttpsRequestWithCerts(connectTimeout int, certPemFile, keyPemFile string, options ...Options) (*Request, error)

func NewRequest

func NewRequest(connectTimeout int, options ...Options) *Request

func (*Request) GetUsingBodyParams added in v1.0.1

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

func (*Request) PostJson added in v1.0.1

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

func (*Request) Run

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

type Result added in v1.4.0

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