request

package module
v0.11.3 Latest Latest
Warning

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

Go to latest
Published: May 10, 2024 License: MIT Imports: 17 Imported by: 3

README

request

通过预定任务发送网络请求

使用
package request_test

import (
	"testing"

	"github.com/Drelf2018/gorms"
	"github.com/Drelf2018/request"
)

var db = gorms.SetSQLite("jobs.db").AutoMigrate(&request.Job{})
var records = 0

func TestFind(t *testing.T) {
	r := gorms.MustFind[request.Job]()
	records = len(r)
	t.Log(r)
}

func TestCreate(t *testing.T) {
	if records < 2 {
		err := db.Create(
			request.NewGet(
				"https://www.baidu.com",
			).SetQuery(
				request.M{"uid": "12138"},
			).SetHeader(
				request.M{"auth": "admin"},
			).SetCookie("buvid3=BE308E31; i-wanna-go-back=-1;"),
		).Error
		if err != nil {
			t.Fatal(err)
		}
	}
}

func TestDo(t *testing.T) {
	job := request.NewPost(
		"https://postman-echo.com/post",
	).SetData(
		request.M{"data": "abc"},
	).SetQuery(
		request.M{"uid": "114514"},
	).SetHeader(
		request.M{"auth": "admin"},
	).SetCookie(
		"buvid3=ABCDE;",
	).SetReferer(
		"postman-echo.com",
	)
	resp := job.Do()
	err := resp.Error()
	if err != nil {
		t.Fatal(err)
	}
	t.Log(resp.Data())
}
进阶用法
package request_test

import (
	"fmt"
	"net/http"
	"testing"

	"github.com/Drelf2018/request"
)

type Replie struct {
	Member struct {
		Mid   string `json:"mid"`
		Uname string `json:"uname"`
	} `json:"member"`
	Content struct {
		Message string `json:"message"`
	} `json:"content"`
}

func (r Replie) String() string {
	return fmt.Sprintf("%v(%v): %v", r.Member.Uname, r.Member.Mid, r.Content.Message)
}

type ApiData struct {
	Code int `json:"code"`
	Data struct {
		Replies []Replie `json:"replies"`
	} `json:"data"`
}

func TestTypeSession(t *testing.T) {
	session, err := request.NewTypeSession[ApiData](http.MethodGet, "https://api.bilibili.com/x/v2/reply", request.M{
		"pn":   "1",
		"type": "11",
		"oid":  "307611044",
		"sort": "0",
	}.Query)
	if err != nil {
		t.Fatal(err)
	}
	data, err := session.Do()
	if err != nil {
		t.Fatal(err)
	}
	t.Log(data)
}

Documentation

Index

Constants

View Source
const UserAgent = "" /* 129-byte string literal not displayed */

Variables

View Source
var (
	ErrType    = errors.New("request: failed to unmarshal JSONB value")
	ErrOddArgs = errors.New("request: odd number of parameters passed in")
)
View Source
var DefaultDownloader = &Downloader{
	Sinaimg: New(http.MethodGet, "").SetReferer("https://weibo.com/"),
	Default: &http.Request{
		Method: http.MethodGet,
		Header: http.Header{"User-Agent": {UserAgent}},
	},
}
View Source
var Headers = M{
	"Accept-Language": "zh-CN,zh;q=0.9",
	"Accept-Encoding": "gzip, deflate, br",
	"Accept":          "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
	"User-Agent":      UserAgent,
}
View Source
var UserAgentHeader = M{"User-Agent": UserAgent}

Functions

func GenerateURL added in v0.11.0

func GenerateURL(url string) (*urlpkg.URL, error)

func HasPort added in v0.10.0

func HasPort(s string) bool

Given a string of the form "host", "host:port", or "[ipv6::address]:port", return true if the string includes a port.

func Json added in v0.9.0

func Json[T any](job *Job) (out T)

func ModifyURL added in v0.10.0

func ModifyURL(req *http.Request, url string) error

func RemoveEmptyPort added in v0.10.0

func RemoveEmptyPort(host string) string

removeEmptyPort strips the empty port in ":port" to "" as mandated by RFC 3986 Section 6.2.3.

func SplitName added in v0.11.0

func SplitName(name string) (protocol, path string)

Types

type Cookie struct {
	Text string
}

func (*Cookie) Cookies added in v0.10.0

func (c *Cookie) Cookies(*url.URL) []*http.Cookie

func (*Cookie) Scan added in v0.10.0

func (c *Cookie) Scan(val any) error

func (*Cookie) SetCookies added in v0.10.0

func (c *Cookie) SetCookies(_ *url.URL, cookies []*http.Cookie)

func (*Cookie) String added in v0.10.0

func (c *Cookie) String() string

func (*Cookie) Value added in v0.10.0

func (c *Cookie) Value() (driver.Value, error)

func (*Cookie) Write added in v0.10.0

func (c *Cookie) Write(p []byte) (n int, err error)

type CookieJar added in v0.10.0

type CookieJar interface {
	ScanValuer
	http.CookieJar
}

type DownloadSystem added in v0.11.0

type DownloadSystem struct {
	Root       string
	Downloader DownloaderInterface
}

func DefaultDownloadSystem added in v0.11.0

func DefaultDownloadSystem(root string) *DownloadSystem

func (DownloadSystem) Open added in v0.11.0

func (ds DownloadSystem) Open(name string) (http.File, error)

type Downloader added in v0.11.0

type Downloader struct {
	Sinaimg *Job
	Default *http.Request
}

func (*Downloader) Get added in v0.11.0

func (d *Downloader) Get(url string) (resp *http.Response, err error)

type DownloaderInterface added in v0.11.0

type DownloaderInterface interface {
	Get(url string) (*http.Response, error)
}

type Job

type Job struct {
	Method string      `form:"method" yaml:"method" json:"method"`
	Url    string      `form:"url"    yaml:"url"    json:"url"`
	Data   M           `form:"data"   yaml:"data"   json:"data"`
	Query  M           `form:"query"  yaml:"query"  json:"query"`
	Header M           `form:"header" yaml:"header" json:"header"`
	Cookie *Cookie     `form:"cookie" yaml:"cookie" json:"cookie"`
	Client http.Client `form:"-"      yaml:"-"      json:"-" gorm:"-"`
}

请求任务

func New

func New(method, url string, opts ...Option) (job *Job)

func NewGet added in v0.10.0

func NewGet(url string, opts ...Option) *Job

func NewPost added in v0.10.0

func NewPost(url string, opts ...Option) *Job

func (*Job) Do added in v0.10.0

func (job *Job) Do() *Response

发送请求

func (*Job) DoWithContext added in v0.10.0

func (job *Job) DoWithContext(ctx ...context.Context) *Response

发送请求

func (*Job) Plain added in v0.11.0

func (job *Job) Plain() (*http.Response, error)

func (*Job) Request

func (job *Job) Request() (req *http.Request, err error)

func (*Job) SetCookie added in v0.10.0

func (job *Job) SetCookie(cookie string) *Job

func (*Job) SetData added in v0.10.0

func (job *Job) SetData(data M) *Job

func (*Job) SetHeader added in v0.10.0

func (job *Job) SetHeader(header M) *Job

func (*Job) SetQuery added in v0.10.0

func (job *Job) SetQuery(query M) *Job

func (*Job) SetReferer added in v0.10.0

func (job *Job) SetReferer(referer string) *Job

func (*Job) SetURL added in v0.11.0

func (job *Job) SetURL(url string) *Job

type M

type M map[string]string

func (M) Add added in v0.9.0

func (m M) Add(k, v string)

func (M) Clone added in v0.10.0

func (m M) Clone() (p M)

func (M) Data added in v0.10.0

func (m M) Data(job *Job)

func (M) Del added in v0.9.0

func (m M) Del(k string)

func (M) Encode added in v0.10.0

func (m M) Encode() string

func (M) GormDataType added in v0.1.1

func (m M) GormDataType() string

GormDataType gorm common data type

func (M) Header added in v0.10.0

func (m M) Header(job *Job)

func (M) Query added in v0.10.0

func (m M) Query(job *Job)

func (M) Read added in v0.10.0

func (m M) Read(p []byte) (int, error)

func (*M) Scan

func (m *M) Scan(val any) error

gorm 读取

参考: https://github.com/go-gorm/datatypes/blob/master/json_map.go

func (M) Set added in v0.9.0

func (m M) Set(k, v string)

func (M) SetAll added in v0.10.0

func (m M) SetAll(s ...string) error

func (M) SetAny added in v0.10.0

func (m M) SetAny(k string, v any) error

func (M) SetMap added in v0.10.0

func (m M) SetMap(p M)

func (M) SetTrimmed added in v0.10.0

func (m M) SetTrimmed(k, v string)

func (M) Value

func (m M) Value() (driver.Value, error)

func (M) WriteHeader added in v0.10.0

func (m M) WriteHeader(header http.Header)

type Option

type Option func(*Job)

func SetCookie added in v0.10.0

func SetCookie(cookie string) Option

func SetData added in v0.10.0

func SetData(data M) Option

func SetHeader added in v0.10.0

func SetHeader(header M) Option

func SetQuery added in v0.10.0

func SetQuery(query M) Option

func SetReferer added in v0.10.0

func SetReferer(referer string) Option

func SetURL added in v0.10.0

func SetURL(url string) Option

type Response added in v0.10.0

type Response struct {
	*http.Response

	*Job
	// contains filtered or unexported fields
}

func Get

func Get(url string, opts ...Option) *Response

func NewError added in v0.10.0

func NewError(err error) *Response

func NewResponse added in v0.10.0

func NewResponse(resp *http.Response) *Response

func Post

func Post(url string, opts ...Option) *Response

func (*Response) Content added in v0.10.0

func (r *Response) Content() []byte

func (*Response) Data added in v0.10.0

func (r *Response) Data() map[string]any

func (*Response) Error added in v0.10.0

func (r *Response) Error() error

func (*Response) Json added in v0.10.0

func (r *Response) Json(out any) error

func (*Response) MarshalJSON added in v0.10.0

func (r *Response) MarshalJSON() ([]byte, error)

func (*Response) ReadCloser added in v0.10.0

func (r *Response) ReadCloser() io.ReadCloser

func (*Response) Text added in v0.10.0

func (r *Response) Text() string

func (*Response) Write added in v0.10.0

func (r *Response) Write(name string) error

func (*Response) Yaml added in v0.10.0

func (r *Response) Yaml(out any) error

type ScanValuer added in v0.10.0

type ScanValuer interface {
	sql.Scanner
	driver.Valuer
}

type Session added in v0.10.0

type Session struct {
	Client  http.Client
	Request *http.Request
}

func NewSession added in v0.10.0

func NewSession(method string, opts ...Option) (*Session, error)

func NewSessionFromJob added in v0.10.0

func NewSessionFromJob(job *Job) (*Session, error)

func (*Session) Do added in v0.10.0

func (s *Session) Do(url ...string) *Response

type TypeSession added in v0.10.0

type TypeSession[T any] Session

func NewTypeSession added in v0.10.0

func NewTypeSession[T any](method, url string, opts ...Option) (*TypeSession[T], error)

func NewTypeSessionFromJob added in v0.10.0

func NewTypeSessionFromJob[T any](job *Job) (*TypeSession[T], error)

func (*TypeSession[T]) Do added in v0.10.0

func (t *TypeSession[T]) Do() (out T, err error)

func (*TypeSession[T]) Must added in v0.10.0

func (t *TypeSession[T]) Must() (out T)

func (*TypeSession[T]) Session added in v0.10.0

func (t *TypeSession[T]) Session() *Session

Jump to

Keyboard shortcuts

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