fetch

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2023 License: MIT Imports: 7 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// WithHeader ...
	WithHeader = func(key string, values ...string) RequestOption {
		return func(req *http.Request) *http.Request {
			for _, value := range values {
				req.Header.Add(key, value)
			}
			return req
		}
	}

	// WithHeaders ...
	WithHeaders = func(header map[string][]string) RequestOption {
		return func(req *http.Request) *http.Request {
			for key, values := range header {
				for _, value := range values {
					req.Header.Add(key, value)
				}
			}
			return req
		}
	}

	// WithSetHeader set k,v in header
	WithSetHeader = func(key, value string) RequestOption {
		return func(req *http.Request) *http.Request {
			req.Header.Set(key, value)
			return req
		}
	}

	// WithoutHeader ...
	WithoutHeader = func(key string) RequestOption {
		return func(req *http.Request) *http.Request {
			req.Header.Del(key)
			return req
		}
	}

	// WithContentType ...
	WithContentType = func(contentType string) RequestOption {
		return func(req *http.Request) *http.Request {
			req.Header.Set("Content-Type", contentType)
			return req
		}
	}

	// WithContentTypeJSON set content type as json
	WithContentTypeJSON = func() RequestOption {
		return WithContentType("application/json")
	}

	// WithAuthToken set "Authorization" header with token
	WithAuthToken = func(token string) RequestOption {
		return func(req *http.Request) *http.Request {
			req.Header.Set("Authorization", token)
			return req
		}
	}

	// WithContext wrap request with context
	WithContext = func(ctx context.Context) RequestOption {
		return func(req *http.Request) *http.Request {
			return req.WithContext(ctx)
		}
	}
)

Functions

func CtxGet

func CtxGet(ctx context.Context, url string, opts ...RequestOption) ([]byte, error)

CtxGet ...

func CtxPatch

func CtxPatch(ctx context.Context, url string, body io.Reader, opts ...RequestOption) ([]byte, error)

CtxPatch ...

func CtxPost

func CtxPost(ctx context.Context, url string, body io.Reader, opts ...RequestOption) ([]byte, error)

CtxPost ...

func DefaultClient

func DefaultClient() *http.Client

DefaultClient return default client

func DoRequest

func DoRequest(method string, url string, body io.Reader) (statusCode int, content []byte, err error)

DoRequest 进行HTTP请求

func DoRequestWithContext

func DoRequestWithContext(ctx context.Context, method string, url string, opts []RequestOption, body io.Reader) (statusCode int, content []byte, err error)

DoRequestWithContext 进行HTTP请求

func DoRequestWithOptions

func DoRequestWithOptions(method string, url string, opts []RequestOption, body io.Reader) (statusCode int, content []byte, respHeaders http.Header, err error)

DoRequestWithOptions 进行HTTP请求并返回响应头

func Get

func Get(url string, opts ...RequestOption) ([]byte, error)

Get ...

func Patch

func Patch(url string, body io.Reader, opts ...RequestOption) ([]byte, error)

Patch ...

func Post

func Post(url string, body io.Reader, opts ...RequestOption) ([]byte, error)

Post ...

func SetDefaultClient

func SetDefaultClient(client *http.Client)

SetDefaultClient set client replace default client

Types

type JSONResult added in v0.1.6

type JSONResult struct {
	Code RespCode `json:"code" example:"200"`
	Msg  string   `json:"msg" example:""`

	Data interface{} `json:"data,omitempty"`

	// Pagination 用于存放分页数据,对于不需要分页的 API,将不会出现这个字段
	Pagination *Pagination `json:"pagination,omitempty"`
}

JSONResult 表示 API 返回的数据

type Pagination added in v0.1.6

type Pagination struct {
	Page     int `json:"page" example:"1"`
	Total    int `json:"total" example:"1024"`
	PageSize int `json:"page_size" example:"20"`
}

Pagination 分页的基本信息

type RequestOption

type RequestOption func(req *http.Request) *http.Request

RequestOption ...

type RespCode added in v0.1.6

type RespCode int

RespCode 漏洞响应状态码

const (
	CodeOK        RespCode = 200 // 正常
	CodeBadReq    RespCode = 400 // 请求格式异常
	CodeNeedAuth  RespCode = 401 // 需要登陆
	CodeForbidden RespCode = 403 // 无权限访问
	CodeServerErr RespCode = 500 // 服务端异常
)

定义一些 API 的响应码,语义上尽量与 HTTP 状态码对齐

Jump to

Keyboard shortcuts

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