goHttpProMaxPlus

package module
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

README

goHttpProMaxPlus

Go quickly request http and Support for AOP

go快速请求Http, 同时支持AOP

要不是公司用go,我才不会写他呢!!!

以下英语均为机翻

Import dependence 导入依赖

go get github.com/971181317/goHttpProMaxPlus

Quick start 快速开始

First build a server with the gin framework

首先用gin框架搭建一个服务器

import "github.com/gin-gonic/gin"

r := gin.Default()
r.GET("/", func(c *gin.Context) {
    c.JSON(200, gin.H{
        "msg": "This is a GET method",
    })
})

r.POST("/", func(c *gin.Context) {
    c.JSON(200, gin.H{
        "msg": "This is a POST method",
    })
})

r.Run() // listen and serve on 0.0.0.0:8080

Next use goHttpProMaxPlus to quickly request Get and Post

接下来使用goHttpProMaxPlus快速请求GetPost

import . "github.com/971181317/goHttpProMaxPlus"

resp, err := GetDefaultClient().Get("http://localhost:8080")
if err != nil {
    panic(err)
}
fmt.Println(resp.GetRespStr())

resp, err = GetDefaultClient().Post("http://localhost:8080")
if err != nil {
    panic(err)
}
fmt.Println(resp.GetRespStr())

result

结果

{"msg":"This is a GET method"}
{"msg":"This is a POST method"}

It's pretty easy, right?

芜湖,起飞(老师,老师,我已经会get和post了)!!!

But what if you want to use Header, Cookie, or Form?

但如果想使用HeaderCookie或者Form怎么办呢?

// cookie, header, form: map[string]string
// data: *io.Reader
GetDefaultClient().GetWithCookieAndHeader(url, cookie, header)
GetDefaultClient().PostWithForm(url, form)
GetDefaultClient().PostWithCookieHeaderAndForm(url, cookie, header, form)
GetDefaultClient().PostWithIoData(url sting, data)
GetDefaultClient().PostWithCookieHeaderAndIoData(url, cookie, header, data)

AOP

5 Aspects:

  1. BeforeClientBuild
  2. AfterClientBuild
  3. BeforeRequestBuild
  4. AfterRequestBuild
  5. AfterResponseCreate

Custom HTTP request 自定义请求

4 Steps:

  1. create client
// AspectModel 切片模组
type AspectModel func(...interface{})

// HttpClient A client send request
type HttpClient struct {
	c                   *http.Client
	BeforeClientBuild   AspectModel
	AfterClientBuild    AspectModel
	BeforeRequestBuild  AspectModel
	AfterRequestBuild   AspectModel
	AfterResponseCreate AspectModel
	AspectArgs          []interface{} // Only the most recent assignment will be kept
}

create it and use aspect

// This method execute BeforeClientBuild and AfterClientBuild.
func NewClientX(client *http.Client,
	beforeClientBuild, afterClientBuild, beforeRequestBuild, afterRequestBuild, afterResponseCreate AspectModel,
	args ...interface{}) *HttpClient
  1. create request
// Forms > Json > Xml > File > ReaderBody
type HttpRequest struct {
	Method     HttpMethod
	URL        string
	Cookies    map[string]string
	Headers    map[string]string
	Queries    map[string]string
	Forms      map[string]string
	Json       *string
	Xml        *string
	File       *os.File
	ReaderBody *io.Reader
}

Two types of assignment

// 1. chain
req := NewHttpRequest().
		SetMethod(POST).
		SetURL("http://localhost:8080").
		AppendHeader("header", "headerValue").
		AppendCookie("cookie", "cookieValue").
		AppendForm("form", "formValue").
		AppendQuery("query", "queryValue")

// 2.
req := &HttpRequest{
    Method     HttpMethod
	URL        "http://localhost:8080"
	Cookies    ...
	Headers    ...
	Queries    ...
	Forms      ...
    ...
}
  1. do request
resp, err := client.Do(req)
  1. get request body
type HttpResponse struct {
	resp    *http.Response
	Headers map[string]string
	body    io.ReadCloser
	bodyStr *string
}

func (hr *HttpResponse) ParseJson(v interface{})
func (hr *HttpResponse) GetRespStr() string

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultClient = &HttpClient{
	&http.Client{Timeout: 5 * time.Second},
	defaultAspect,
	defaultAspect,
	defaultAspect,
	defaultAspect,
	defaultAspect,
	nil,
}

Functions

This section is empty.

Types

type AspectModel

type AspectModel func(*HttpRequest, *HttpResponse, ...interface{})

AspectModel 切片模组

type HttpClient

type HttpClient struct {
	BeforeClientBuild   AspectModel
	AfterClientBuild    AspectModel
	BeforeRequestBuild  AspectModel
	AfterRequestBuild   AspectModel
	AfterResponseCreate AspectModel
	AspectArgs          []interface{} // Only the most recent assignment will be kept
	// contains filtered or unexported fields
}

HttpClient A client send request

func GetDefaultClient

func GetDefaultClient() *HttpClient

GetDefaultClient !!! This method will not execute BeforeClientBuild and AfterClientBuild. 这个方法不会执行 BeforeClientBuild 和 AfterClientBuild。

func NewClient

func NewClient(client *http.Client) *HttpClient

NewClient !!! This method will not execute BeforeClientBuild and AfterClientBuild. 这个方法不会执行 BeforeClientBuild 和 AfterClientBuild。

func NewClientX

func NewClientX(client *http.Client,
	beforeClientBuild, afterClientBuild, beforeRequestBuild, afterRequestBuild, afterResponseCreate AspectModel,
	args ...interface{}) *HttpClient

NewClientX This method execute BeforeClientBuild and AfterClientBuild. 这个方法执行 BeforeClientBuild 和 AfterClientBuild。

func (HttpClient) Do

func (client HttpClient) Do(req *HttpRequest) (*HttpResponse, error)

Do run with Aspect

func (HttpClient) Get

func (client HttpClient) Get(url string) (*HttpResponse, error)

func (HttpClient) GetAspectArgs

func (client HttpClient) GetAspectArgs() []interface{}

func (HttpClient) GetWithCookieAndHeader

func (client HttpClient) GetWithCookieAndHeader(url string, cookie, header map[string]string) (*HttpResponse, error)

func (HttpClient) Post

func (client HttpClient) Post(url string) (*HttpResponse, error)

func (HttpClient) PostWithCookieHeaderAndForm

func (client HttpClient) PostWithCookieHeaderAndForm(url string, cookie, header, form map[string]string) (*HttpResponse, error)

func (HttpClient) PostWithCookieHeaderAndIoData

func (client HttpClient) PostWithCookieHeaderAndIoData(url string, cookie, header map[string]string, data *io.Reader) (*HttpResponse, error)

func (HttpClient) PostWithForm

func (client HttpClient) PostWithForm(url string, form map[string]string) (*HttpResponse, error)

func (HttpClient) PostWithIoData

func (client HttpClient) PostWithIoData(url string, data *io.Reader) (*HttpResponse, error)

type HttpMethod

type HttpMethod string

func (HttpMethod) String

func (hm HttpMethod) String() string

type HttpRequest

type HttpRequest struct {
	Method HttpMethod

	// Queries will overwrite the queries in the URL
	// Queries 会覆盖 URL 中的参数
	URL string

	Cookies map[string]string
	Headers map[string]string

	// Queries will overwrite the queries in the URL
	// Queries 会覆盖 URL 中的参数
	Queries map[string]string

	// When creating the HttpRequest, we have already added "Content-Type: application/x-www-form-urlencoded" for you .
	// 在创建 HttpRequest 的时候,我们已经帮你加好了 Content-Type: application/x-www-form-urlencoded
	// If you want to modify, please add "Content-Type" sauce to the Headers
	// 如果你想修改的话,请在 Headers 中加入“Content-Type”
	// The HTTP client ignores Form and uses Body instead.
	Forms map[string]string

	// When creating the HttpRequest, we have already added "Content-Type: application/json" for you .
	// 在创建 HttpRequest 的时候,我们已经帮你加好了 Content-Type
	// If you want to modify, please add "Content-Type" sauce to the Headers
	// 如果你想修改的话,请在 Headers 中加入“Content-Type”
	Json *string

	// When creating the HttpRequest, we have already added "Content-Type: text/xml" for you .
	// 在创建 HttpRequest 的时候,我们已经帮你加好了 Content-Type: application/x-www-form-urlencoded
	// If you want to modify, please add "Content-Type" sauce to the Headers
	// 如果你想修改的话,请在 Headers 中加入“Content-Type”
	Xml *string

	// When creating the HttpRequest, we have already added "Content-Type: multipart/form-data" for you .
	// 在创建 HttpRequest 的时候,我们已经帮你加好了 Content-Type
	// If you want to modify, please add "Content-Type: " sauce to the Headers
	// 如果你想修改的话,请在 Headers 中加入“Content-Type”
	File *os.File

	// Please add "Content-Type" sauce to the Headers
	// 请在 Headers 中加入“Content-Type”
	ReaderBody *io.Reader
}

HttpRequest Forms > Json > Xml > File > ReaderBody

func NewHttpRequest

func NewHttpRequest() *HttpRequest

func (*HttpRequest) AppendCookie

func (hr *HttpRequest) AppendCookie(name, value string) *HttpRequest

func (*HttpRequest) AppendCookies

func (hr *HttpRequest) AppendCookies(cookies map[string]string) *HttpRequest

func (*HttpRequest) AppendForm

func (hr *HttpRequest) AppendForm(name, value string) *HttpRequest

func (*HttpRequest) AppendForms

func (hr *HttpRequest) AppendForms(forms map[string]string) *HttpRequest

func (*HttpRequest) AppendHeader

func (hr *HttpRequest) AppendHeader(name, value string) *HttpRequest

func (*HttpRequest) AppendHeaders

func (hr *HttpRequest) AppendHeaders(headers map[string]string) *HttpRequest

func (*HttpRequest) AppendQueries

func (hr *HttpRequest) AppendQueries(params map[string]string) *HttpRequest

func (*HttpRequest) AppendQuery

func (hr *HttpRequest) AppendQuery(name, value string) *HttpRequest

func (*HttpRequest) BuildRequest

func (hr *HttpRequest) BuildRequest() (*http.Request, error)

func (*HttpRequest) ClearAllCookie

func (hr *HttpRequest) ClearAllCookie() *HttpRequest

func (*HttpRequest) ClearAllForm

func (hr *HttpRequest) ClearAllForm() *HttpRequest

func (*HttpRequest) ClearAllHeader

func (hr *HttpRequest) ClearAllHeader() *HttpRequest

func (*HttpRequest) ClearAllQuery

func (hr *HttpRequest) ClearAllQuery() *HttpRequest

func (*HttpRequest) DelCookie

func (hr *HttpRequest) DelCookie(name string) *HttpRequest

func (*HttpRequest) DelForm

func (hr *HttpRequest) DelForm(name string) *HttpRequest

func (*HttpRequest) DelHeader

func (hr *HttpRequest) DelHeader(name string) *HttpRequest

func (*HttpRequest) DelQuery

func (hr *HttpRequest) DelQuery(name string) *HttpRequest

func (*HttpRequest) GetCookie

func (hr *HttpRequest) GetCookie(name string) string

func (*HttpRequest) GetForm

func (hr *HttpRequest) GetForm(name string) string

func (*HttpRequest) GetHeader

func (hr *HttpRequest) GetHeader(name string) string

func (*HttpRequest) GetQuery

func (hr *HttpRequest) GetQuery(name string) string

func (*HttpRequest) ReplaceCookies

func (hr *HttpRequest) ReplaceCookies(cookies map[string]string)

func (*HttpRequest) ReplaceForms

func (hr *HttpRequest) ReplaceForms(header map[string]string) *HttpRequest

func (*HttpRequest) ReplaceHeaders

func (hr *HttpRequest) ReplaceHeaders(header map[string]string) *HttpRequest

func (*HttpRequest) ReplaceQuery

func (hr *HttpRequest) ReplaceQuery(param map[string]string) *HttpRequest

func (*HttpRequest) SetFileBody

func (hr *HttpRequest) SetFileBody(file *os.File) *HttpRequest

func (*HttpRequest) SetFileBodyPath

func (hr *HttpRequest) SetFileBodyPath(path string) *HttpRequest

func (*HttpRequest) SetJsonBody

func (hr *HttpRequest) SetJsonBody(json string) *HttpRequest

func (*HttpRequest) SetJsonBodyStruct added in v1.0.4

func (hr *HttpRequest) SetJsonBodyStruct(v interface{}) *HttpRequest

func (*HttpRequest) SetMethod

func (hr *HttpRequest) SetMethod(method HttpMethod) *HttpRequest

func (*HttpRequest) SetReaderBody

func (hr *HttpRequest) SetReaderBody(reader *io.Reader) *HttpRequest

func (*HttpRequest) SetURL

func (hr *HttpRequest) SetURL(URL string) *HttpRequest

type HttpResponse

type HttpResponse struct {
	Headers map[string]string
	Body    io.ReadCloser
	// contains filtered or unexported fields
}

func CreateResponse

func CreateResponse(resp *http.Response) *HttpResponse

func (*HttpResponse) GetRespStr

func (hr *HttpResponse) GetRespStr() string

func (*HttpResponse) GetResponse

func (hr *HttpResponse) GetResponse() *http.Response

GetResponse It return http.Response struct

func (*HttpResponse) ParseJson

func (hr *HttpResponse) ParseJson(v interface{})

Directories

Path Synopsis
demo

Jump to

Keyboard shortcuts

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