gohttp

package
v1.2.164 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

README

Http 请求封装

package main

import (
	"context"
	"fmt"
	gocontext "github.com/gif-gif/go.io/go-context"
	"github.com/gif-gif/go.io/go-http"
	golog "github.com/gif-gif/go.io/go-log"
	"github.com/gif-gif/go.io/goio"
	"time"
)

func main() {
	goio.Init(goio.DEVELOPMENT)
	type httpRequest struct {
		Email string `json:"email"`
	}

	req := &gohttp.Request{
		Url: "/main",
		Urls: []string{
			"/main1",
			"/main2",
			"/main3",
		},
		QueryParams: map[string]string{"name": "jk"},
		Timeout:     time.Second * 2,
		Body: &httpRequest{
			Email: "test@gmail.com",
		},
	}

	gh := &gohttp.GoHttp[gohttp.Response]{
		Request: req,
		BaseUrl: "http://localhost",
		Headers: map[string]string{
			"User-Agent": "github.com/gif-gif/go.io",
		},
	}

	rst, err := gh.HttpPostJson(context.Background())
	if err != nil {
		golog.WithTag("http").Error(err.Error())
	} else {
		fmt.Println(rst)
	}
}

  • Custom Root Certificates and Client Certificates
  • Custom Root Certificates and Client Certificates from string
  • Save HTTP Response into File

Documentation

Index

Constants

View Source
const (
	TAG = "gohttp"

	CONTENT_TYPE_XML  = "application/xml"
	CONTENT_TYPE_JSON = "application/json"
	CONTENT_TYPE_FORM = "application/x-www-form-urlencoded"
)
View Source
const (
	POST   = "post"
	GET    = "get"
	PUT    = "put"
	DELETE = "delete"
)
View Source
const (
	HttpUnknownError = 1000
	HttpRetryError   = 2000
	HttpParamsError  = 3000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type GoHttp added in v1.2.55

type GoHttp[T any] struct {
	BaseUrl string
	Headers map[string]string
	Request *Request
}

func (*GoHttp[T]) AddGlobalHeader added in v1.2.55

func (g *GoHttp[T]) AddGlobalHeader(k, v string)

func (*GoHttp[T]) GetBaseUrl added in v1.2.55

func (g *GoHttp[T]) GetBaseUrl() string

func (*GoHttp[T]) GetGlobalHeaders added in v1.2.55

func (g *GoHttp[T]) GetGlobalHeaders() map[string]string

func (*GoHttp[T]) HttpConcurrencyRequest added in v1.2.55

func (g *GoHttp[T]) HttpConcurrencyRequest() (*T, error)

带多个Urls重试逻辑,并发请求,速度快先到达后 直接返回,其他请求取消

func (*GoHttp[T]) HttpGet added in v1.2.55

func (g *GoHttp[T]) HttpGet(context context.Context) (*T, error)

func (*GoHttp[T]) HttpPost added in v1.2.55

func (g *GoHttp[T]) HttpPost(context context.Context) (*T, error)

func (*GoHttp[T]) HttpPostJson added in v1.2.55

func (g *GoHttp[T]) HttpPostJson(context context.Context) (*T, error)

func (*GoHttp[T]) HttpRequest added in v1.2.55

func (g *GoHttp[T]) HttpRequest(context context.Context) (*T, error)

带多个Urls重试逻辑

func (*GoHttp[T]) RemoveGlobalHeader added in v1.2.55

func (g *GoHttp[T]) RemoveGlobalHeader(k string)

func (*GoHttp[T]) SetBaseUrl added in v1.2.55

func (g *GoHttp[T]) SetBaseUrl(base string)

type Request

type Request struct {
	Url          string
	Urls         []string // 如果有值,当url 请求失败时继续用这里的接口尝试,直到成功返回或者全部失败
	Method       string
	Body         interface{}       //post body 参数
	QueryParams  map[string]string //get 参数
	ParamsValues url.Values        //get 参数
	FormData     map[string]string //formdata 参数
	Headers      map[string]string

	Files             map[string]string //上传文件列表
	FileName          string            //文件名称
	MultipartFormData map[string]string
	FileBytes         []byte

	Timeout       time.Duration
	RetryCount    int
	RetryWaitTime time.Duration

	//gotrace infos
	TraceInfo     resty.TraceInfo
	ResponseTime  time.Duration
	ResponseProto string
	Response      *resty.Response
	// contains filtered or unexported fields
}

func (*Request) AddUrl added in v1.2.55

func (r *Request) AddUrl(url string)

重复会去重

func (*Request) SetBody added in v1.2.55

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

func (*Request) SetHeader

func (r *Request) SetHeader(name string, value string)

func (*Request) SetMethod added in v1.2.55

func (r *Request) SetMethod(method string)

func (*Request) SetQueryParams added in v1.2.55

func (r *Request) SetQueryParams(name string, value string)

func (*Request) SetRetryCount added in v1.2.55

func (r *Request) SetRetryCount(tryCount int)

func (*Request) SetRetryWaitTime added in v1.2.55

func (r *Request) SetRetryWaitTime(waitTime time.Duration)

func (*Request) SetTimeout

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

type Response added in v1.2.55

type Response struct {
	Code int64       `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func HttpBadRequest added in v1.2.55

func HttpBadRequest(msg string) *Response

请求格式错误,比如参数格式、参数字段名等 不正确

func HttpFail added in v1.2.55

func HttpFail(msg string) *Response

请求失败

func HttpFailForCode added in v1.2.55

func HttpFailForCode(code int64, msg string) *Response

如需返回特殊错误码,调用此接口

func HttpForbidden added in v1.2.55

func HttpForbidden(msg string) *Response

用户已进行身份认证,但权限不够

func HttpNotFound added in v1.2.55

func HttpNotFound(msg string) *Response

接口不存在

func HttpServerError added in v1.2.55

func HttpServerError(msg string) *Response

服务器内部错误

func HttpSuccess added in v1.2.55

func HttpSuccess(data interface{}) *Response

请求成功

func HttpSuccessByCode added in v1.2.55

func HttpSuccessByCode(code int64, data interface{}) *Response

func HttpUnauthorized added in v1.2.55

func HttpUnauthorized(msg string) *Response

用户没有访问权限,需要进行身份认证

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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