gorequests

package module
v1.0.17 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2023 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package gorequests send http request with human style.

for send GET request:

gorequests.New(http.MethodGet, "https://httpbin.org/get")

for send POST request:

gorequests.New(http.MethodPost, "https://httpbin.org/post).WithBody("text body")

for send http upload request

gorequests.New(http.MethodPost, "https://httpbin.org/post).WithFile("1.txt", strings.NewReader("hi"), "file", nil)

for send json request

gorequests.New(http.MethodPost, "https://httpbin.org/post).WithJSON(map[string]string{"key": "val"})

request with timeout

gorequests.New(http.MethodGet, "https://httpbin.org/get).WithTimeout(time.Second)

request with context

gorequests.New(http.MethodGet, "https://httpbin.org/get).WithContext(context.TODO())

request with no redirect

gorequests.New(http.MethodGet, "https://httpbin.org/status/302).WithRedirect(false)
Example (Factory)
package main

import (
	"fmt"
	"net/http"
	"time"

	"github.com/bitholic/gorequests"
)

func main() {
	// I hope to set fixed parameters every time I initiate a request

	// Then, every request created by this factory will not log
	fac := gorequests.NewFactory(
		gorequests.WithLogger(gorequests.NewDiscardLogger()),
		gorequests.WithTimeout(time.Second*10),
	)

	// Send sample request
	text, err := fac.New(http.MethodGet, "https://httpbin.org/get").Text()
	if err != nil {
		panic(err)
	}
	fmt.Println("text", text)
}
Output:

Example (New)
package main

import (
	"fmt"
	"net/http"
	"time"

	"github.com/bitholic/gorequests"
)

func main() {
	text, err := gorequests.New(http.MethodGet, "https://httpbin.org/get").WithTimeout(time.Second * 10).Text()
	if err != nil {
		panic(err)
	}
	fmt.Println("text", text)
}
Output:

Example (NewSession)
package main

import (
	"fmt"
	"net/http"
	"time"

	"github.com/bitholic/gorequests"
)

func main() {
	session := gorequests.NewSession("/tmp/gorequests-session.txt")
	text, err := session.New(http.MethodGet, "https://jsonplaceholder.typicode.com/todos/1").WithTimeout(time.Second * 10).Text()
	if err != nil {
		panic(err)
	}
	fmt.Println("text", text)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Factory

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

func NewFactory

func NewFactory(options ...RequestOption) *Factory

func (*Factory) New

func (r *Factory) New(method, url string) *Request

type LogMessage

type LogMessage struct {
	Method string `json:"method"`
	Url    string `json:"url"`

	RequestBody   string      `json:"request_body"`
	RequestHeader http.Header `json:"request_header"`
	RequestTime   string      `json:"request_time"`

	ResponseBody      string      `json:"response_body"`
	ResponseHeader    http.Header `json:"response_header"`
	ResponseStateCode int         `json:"response_state_code"`
	ResponseTime      string      `json:"response_time"`

	TimeConsuming int64  `json:"time_consuming"` // milliseconds
	ErrorMessage  string `json:"error_message"`

	LogId       string             `json:"log_id"`
	RequestType RequestMessageType `json:"request_type"`
}

type LogProducer

type LogProducer interface {
	SendLogMessage(ctx context.Context, data []byte) error
}

func NewDiscardLogProducer added in v1.0.10

func NewDiscardLogProducer() LogProducer

func NewPrinterLogProducer

func NewPrinterLogProducer() LogProducer

func NewRmqLogProducer

func NewRmqLogProducer(producer Producer, topic string) LogProducer

type Logger

type Logger interface {
	Info(ctx context.Context, format string, v ...interface{})
	Error(ctx context.Context, format string, v ...interface{})
}

func NewDiscardLogger

func NewDiscardLogger() Logger

func NewStdoutLogger

func NewStdoutLogger() Logger

type Producer added in v1.0.16

type Producer interface {
	Send(ctx context.Context, data []byte) error
}

type Request

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

func New

func New(method, url string) *Request

func (*Request) Bytes

func (r *Request) Bytes() ([]byte, error)

func (*Request) Context

func (r *Request) Context() context.Context

Context request context.Context

func (*Request) LogMessage

func (r *Request) LogMessage() *LogMessage

LogMessage unit test only

func (*Request) Map

func (r *Request) Map() (map[string]interface{}, error)

func (*Request) Method

func (r *Request) Method() string

Method request method

func (*Request) MustBytes

func (r *Request) MustBytes() []byte

func (*Request) MustMap

func (r *Request) MustMap() map[string]interface{}

func (*Request) MustResponse

func (r *Request) MustResponse() *http.Response

func (*Request) MustResponseCookiesByKey

func (r *Request) MustResponseCookiesByKey(key string) []string

func (*Request) MustResponseHeaderByKey

func (r *Request) MustResponseHeaderByKey(key string) string

func (*Request) MustResponseHeaders

func (r *Request) MustResponseHeaders() http.Header

func (*Request) MustResponseHeadersByKey

func (r *Request) MustResponseHeadersByKey(key string) []string

func (*Request) MustResponseStatus

func (r *Request) MustResponseStatus() int

func (*Request) MustText

func (r *Request) MustText() string

func (*Request) MustUnmarshal

func (r *Request) MustUnmarshal(val interface{})

func (*Request) RequestFullURL

func (r *Request) RequestFullURL() string

RequestFullURL request full url, contain query param

func (*Request) RequestHeader

func (r *Request) RequestHeader() http.Header

RequestHeader request header

func (*Request) Response

func (r *Request) Response() (*http.Response, error)

func (*Request) ResponseHeaderByKey

func (r *Request) ResponseHeaderByKey(key string) (string, error)

func (*Request) ResponseHeaders

func (r *Request) ResponseHeaders() (http.Header, error)

func (*Request) ResponseHeadersByKey

func (r *Request) ResponseHeadersByKey(key string) ([]string, error)

func (*Request) ResponseStatus

func (r *Request) ResponseStatus() (int, error)

func (*Request) SetDoError added in v1.0.12

func (r *Request) SetDoError(err error) *Request

func (*Request) SetError

func (r *Request) SetError(err error) *Request

func (*Request) SetFullUrl

func (r *Request) SetFullUrl(url string)

func (*Request) SetLogProducer added in v1.0.13

func (r *Request) SetLogProducer(producer LogProducer)

func (*Request) Text

func (r *Request) Text() (string, error)

func (*Request) Timeout

func (r *Request) Timeout() time.Duration

Timeout request timeout

func (*Request) URL

func (r *Request) URL() string

URL request url

func (*Request) Unmarshal

func (r *Request) Unmarshal(val interface{}) error

func (*Request) WithBody

func (r *Request) WithBody(body interface{}) *Request

WithBody set request body, support: io.Reader, []byte, string, interface{}(as json format)

func (*Request) WithContext

func (r *Request) WithContext(ctx context.Context) *Request

WithContext setup request context.Context

func (*Request) WithFile

func (r *Request) WithFile(filename string, file io.Reader, fileKey string, params map[string]string) *Request

WithFile set file to body and set some multi-form k-v map

func (*Request) WithForm

func (r *Request) WithForm(body map[string]string) *Request

WithForm set body and set Content-Type to multiform

func (*Request) WithFormURLEncoded

func (r *Request) WithFormURLEncoded(body map[string]string) *Request

WithFormURLEncoded set body and set Content-Type to application/x-www-form-urlencoded

func (*Request) WithHeader

func (r *Request) WithHeader(k, v string) *Request

WithHeader set one header k-v map

func (*Request) WithHeaders

func (r *Request) WithHeaders(kv map[string]string) *Request

WithHeaders set multi header k-v map

func (*Request) WithIgnoreSSL

func (r *Request) WithIgnoreSSL(ignore bool) *Request

WithIgnoreSSL ignore ssl verify

func (*Request) WithJSON

func (r *Request) WithJSON(body interface{}) *Request

WithJSON set body same as WithBody, and set Content-Type to application/json

func (*Request) WithLogId added in v1.0.16

func (r *Request) WithLogId(logId string) *Request

func (*Request) WithLogProducer added in v1.0.13

func (r *Request) WithLogProducer(producer LogProducer) *Request

func (*Request) WithLogger

func (r *Request) WithLogger(logger Logger) *Request

WithLogger set logger

func (*Request) WithQuery

func (r *Request) WithQuery(k, v string) *Request

WithQuery set one query k-v map

func (*Request) WithQueryStruct

func (r *Request) WithQueryStruct(v interface{}) *Request

WithQueryStruct set multi query k-v map

func (*Request) WithQuerys

func (r *Request) WithQuerys(kv map[string]string) *Request

WithQuerys set multi query k-v map

func (*Request) WithRedirect

func (r *Request) WithRedirect(b bool) *Request

WithRedirect set allow or not-allow redirect with Location header

func (*Request) WithTimeout

func (r *Request) WithTimeout(timeout time.Duration) *Request

WithTimeout setup request timeout

func (*Request) WithURLCookie

func (r *Request) WithURLCookie(uri string) *Request

WithURLCookie set cookie of uri

type RequestMessageType added in v1.0.7

type RequestMessageType int
const (
	RequestMessageTypeUnknown RequestMessageType = 0
	RequestMessageTypeIn      RequestMessageType = 1
	RequestMessageTypeOut     RequestMessageType = 2
)

type RequestOption

type RequestOption func(req *Request) error

func WithHeader

func WithHeader(key, val string) RequestOption

func WithLogger

func WithLogger(logger Logger) RequestOption

func WithQuery

func WithQuery(key, val string) RequestOption

func WithTimeout

func WithTimeout(timeout time.Duration) RequestOption

type Session

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

func NewSession

func NewSession(cookiefile string, options ...RequestOption) *Session

same cookie-file has same session instance

func (*Session) AddOpts

func (r *Session) AddOpts(options ...RequestOption)

func (*Session) CookieFile

func (r *Session) CookieFile() string

func (*Session) Jar

func (r *Session) Jar() http.CookieJar

func (*Session) New

func (r *Session) New(method, url string) *Request

Jump to

Keyboard shortcuts

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