httpclient

package
v0.0.0-...-ab8afbf Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2021 License: MIT Imports: 8 Imported by: 0

README

HTTP Client

搭建Http客户端.

Http客户端主要分为两点: Client 和 业务方法.

Client 构建主要为

  1. NewClient(): 创建客户端
  2. NewRequest(): 创建一个Request. 包括构建URL, 添加路径参数, 构建body.
  3. Do(): 发起请求, 构建Response, 处理超时与error.

参考 client.go, request.go, Response.go


业务上定义自己的结构体和业务方法即可, 然后调用 NewRequest和Do 实现基础功能.

具体参考 demo.go

Documentation

Index

Constants

View Source
const (
	BaseURL = "https://localhost.com"

	// ContentTypeJSON HTTP Content-Type header for JSON data
	ContentTypeJSON = "application/json; charset=utf-8"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthorizedRequest

type AuthorizedRequest struct{}

func (*AuthorizedRequest) Authorized

func (a *AuthorizedRequest) Authorized()

type AuthorizedRequester

type AuthorizedRequester interface {
	Authorized()
}

AuthorizedRequester 接口用于判断请求是否需要提供AccessToken

type Client

type Client struct {
	BaseURL *url.URL `json:"base_url"`
	Token   string   `json:"token"`
	// contains filtered or unexported fields
}

func NewClient

func NewClient(opts Options) *Client

func (*Client) Do

Do 发起HTTP调用

func (*Client) GetDemo

func (c *Client) GetDemo(ctx context.Context, request *DemoRequest) (*DemoResponse, *http.Response, error)

func (*Client) NewRequest

func (c *Client) NewRequest(method, uri string, params url.Values, body interface{}) (*http.Request, error)

NewRequest 创建一个 Request. 通过 body 实现的方法判断对Request进行哪些定制

type CustomizedEncoderRequester

type CustomizedEncoderRequester interface {
	ConfigureEncoder(encoder *json.Encoder)
}

type DemoRequest

type DemoRequest struct {
	Limit int
}

func (DemoRequest) Authorized

func (d DemoRequest) Authorized()

func (DemoRequest) EmptyBody

func (d DemoRequest) EmptyBody()

实现继承, 从而在NewRequest时添加相应属性

func (*DemoRequest) GetParams

func (d *DemoRequest) GetParams() url.Values

type DemoResponse

type DemoResponse struct {
	Response
	Words string `json:"words"`
}

type EmptyBodyRequest

type EmptyBodyRequest struct{}

func (*EmptyBodyRequest) EmptyBody

func (b *EmptyBodyRequest) EmptyBody()

type EmptyBodyRequester

type EmptyBodyRequester interface {
	EmptyBody()
}

EmptyBodyRequester 接口用于判断请求体是否为空

type Options

type Options struct {
	Token      string `json:"token"`
	HTTPClient *http.Client
}

type Response

type Response struct {
	ErrorCode    int    `json:"errcode,omitempty"`
	ErrorMessage string `json:"errmsg,omitempty"`
}

Response 接口响应通用字段

func (*Response) GetErrorCode

func (r *Response) GetErrorCode() int

GetErrorCode 获取错误码

func (*Response) GetErrorMessage

func (r *Response) GetErrorMessage() string

GetErrorMessage 获取错误信息

type ResponseCarrier

type ResponseCarrier interface {
	GetErrorCode() int
	GetErrorMessage() string
}

ResponseCarrier 接口响应接口

type ResponseError

type ResponseError struct {
	Code     int
	Message  string
	Response *http.Response
}

ResponseError 接口响应错误

func (*ResponseError) Error

func (e *ResponseError) Error() string

Error 实现 error 接口

Jump to

Keyboard shortcuts

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