client

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

README

Client

概述

HTTP Client 是一个用于发送 HTTP 请求的库,它支持如下功能:

Content-Type 自动识别,仅支持 application/json 和 application/x-www-form-urlencoded 两种格式 支持将 path 参数自动填充到 url 支持将结构体中 header 填充到 http 请求头

请求示例

GET、POST Form 请求

Get 和 Post form 请求的使用方式是一样的,只需要将结构体中的字段标记为 form 即可。

type Request struct {
    Node   string `path:"node"`
    ID     int    `form:"id"`
    Header string `header:"X-Header"`
}

var domain = flag.String("domain", "http://localhost:3333", "the domain to request")

func main() {
flag.Parse()

    req := Request{
        Node:   "foo",
        ID:     1024,
        Header: "foo-header",
    }
    resp, err := httpc.Do(context.Background(), http.MethodGet, *domain+"/nodes/:node", req)
    // resp, err := httpc.Do(context.Background(), http.MethodPost, *domain+"/nodes/:node", req)
    if err != nil {
        fmt.Println(err)
        return
    }

    io.Copy(os.Stdout, resp.Body)
}

以上内等同于如下 curl:

get
curl --location 'http://localhost:3333/nodes/foo?id=1024' \
--header 'X-Header: foo-header'

post
curl --location 'http://localhost:3333/nodes/foo' \
--header 'X-Header: foo-header' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'id=1024'
POST Json 请求

Post Json 请求的使用方式是一样的,只需要将结构体中的字段标记为 json 即可。


type Request struct {
    Node   string `path:"node"`
    Foo    string `json:"foo"`
    Bar    string `json:"bar"`
    Header string `header:"X-Header"`
}

var domain = flag.String("domain", "http://localhost:3333", "the domain to request")

func main() {
flag.Parse()

    req := Request{
        Node:   "foo",
        Header: "foo-header",
        Foo: "foo",
        Bar: "bar",
    }
    resp, err := httpc.Do(context.Background(), http.MethodPost, *domain+"/nodes/:node", req)
    if err != nil {
        fmt.Println(err)
        return
    }

    io.Copy(os.Stdout, resp.Body)
}

以上请求等同于如下 curl:

curl --location 'http://localhost:3333/nodes/foo' \
--header 'X-Header: foo-header' \
--header 'Content-Type: application/json' \
--data '{
"foo":"foo",
"bar":"bar"
}'

Documentation

Overview

Created by guoxin in 2023/12/8 14:00

Created by guoxin in 2023/12/8 15:46

Created by guoxin in 2023/12/8 18:00

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Addresses

type Addresses interface {
	GetAddresses() []string
	SetAddresses([]string)
}

type HttpClientRequest

type HttpClientRequest interface {
	// HttpMethod 指定请求方法
	HttpMethod
	// HttpPath 指定请求路径
	HttpPath
}

HttpClientRequest 使用client组件访问接口的请求需要实现这个接口

type HttpConn

type HttpConn struct {
	Addresses  []string
	ServerName string
	ClientName string
}

func NewHttpConn

func NewHttpConn(serverName, clientName string) HttpConn

func (*HttpConn) GetAddresses

func (h *HttpConn) GetAddresses() []string

func (*HttpConn) NewAddress

func (h *HttpConn) NewAddress(addresses []resolver.Address)

func (*HttpConn) NewServiceConfig

func (h *HttpConn) NewServiceConfig(serviceConfig string)

func (*HttpConn) ParseServiceConfig

func (h *HttpConn) ParseServiceConfig(serviceConfigJSON string) *serviceconfig.ParseResult

func (*HttpConn) ReportError

func (h *HttpConn) ReportError(err error)

func (*HttpConn) SetAddresses

func (h *HttpConn) SetAddresses(addresses []string)

func (*HttpConn) UpdateState

func (h *HttpConn) UpdateState(state resolver.State) error

type HttpMethod

type HttpMethod interface {
	GetMethod() string
}

HttpMethod 指定请求方法 可选参数如下 see: http.MethodGet see: http.MethodHead see: http.MethodPost see: http.MethodPut see: http.MethodPatch see: http.MethodDelete see: http.MethodConnect see: http.MethodOptions see: http.MethodTrace

type HttpPath

type HttpPath interface {
	GetPath() string
}

HttpPath 指定请求路径 eg: /path/to/api

type NacosHttpClient

type NacosHttpClient struct {
	// contains filtered or unexported fields
}

NacosHttpClient 基于Nacos的Http调用客户端

func NewNacosHttpClient

func NewNacosHttpClient(nacos *nacos.Nacos, clientConn HttpConn) (*NacosHttpClient, error)

NewNacosHttpClient 创建 基于Nacos的Http调用客户端

func (*NacosHttpClient) Call

func (c *NacosHttpClient) Call(ctx context.Context, req HttpClientRequest, res any) error

Call 基于Nacos的Http调用客户端

Jump to

Keyboard shortcuts

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