xhttp

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: May 31, 2023 License: Apache-2.0 Imports: 19 Imported by: 1

README

xhttp

http

    const Timeout = 6 * time.Second
    const ApiFindUserById = Root + "/twirp/user.v1.User/FindUserById"

    params := struct {
		Id int64 `json:"id"`
	}{
		Id: id,
	}
	b, _ := json.Marshal(params)

	req, _ := http.NewRequest(http.MethodPost, frame.ApiFindUserById, bytes.NewReader(b))

	resp, err := httpD.NewClient(frame.Timeout).Do(ctx, req)
	fmt.Println(resp)
	if err != nil {
		return
	}
	if resp.StatusCode != 200 {
		return u, errors.New("请求失败")
	}

	respBody, _ := ioutil.ReadAll(resp.Body)
	_ = json.Unmarshal(respBody, &u)

grpc

支持 grpc unary 请求

grpc 的应用日益广泛,但 grpc 又过于复杂。就我们观察,绝大多数 grpc 接口 都是 unary 类型的。此类请求跟传统的 http 请求很类似,比较简单。

基于此,我们引入一个简单的 unary client 实现,争取用 20% 的代码解决 80% 的问题。

Documentation

Overview

Package xhttp 提供基础 http 客户端组件 内置以下功能: - logging - opentracing - prometheus

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	// Do 发送单个 http 请求
	Do(ctx context.Context, req *http.Request) (*http.Response, error)
}

Client http 客户端接口

func NewClient

func NewClient(timeout time.Duration) Client

NewClient 创建 Client 实例

func NewInsecureClient added in v1.3.0

func NewInsecureClient(timeout time.Duration) Client

NewInsecureClient 创建不校验证书的 Client 实例

type GrpcClient added in v1.3.0

type GrpcClient interface {
	DoUnary(ctx context.Context, api string, req, resp proto.Message) (h2resp *http.Response, err error)
}

func NewGrpcClient added in v1.3.0

func NewGrpcClient(timeout time.Duration) GrpcClient

Jump to

Keyboard shortcuts

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