http

package module
v3.9.16 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2025 License: Apache-2.0 Imports: 25 Imported by: 10

README

HTTP Client

This plugin is a http client for micro.

Overview

The http client wraps net/http to provide a robust micro client with service discovery, load balancing and streaming. It complies with the micro.Client interface.

Usage

Use directly
import "go.unistack.org/micro-client-http/v3"

service := micro.NewService(
	micro.Name("my.service"),
	micro.Client(http.NewClient()),
)
Call Service

Assuming you have a http service "my.service" with path "/foo/bar"

// new client
client := http.NewClient()

// create request/response
request := client.NewRequest("my.service", "/foo/bar", protoRequest{})
response := new(protoResponse)

// call service
err := client.Call(context.TODO(), request, response)

or you can call any rest api or site and unmarshal to response struct

// new client
client := client.NewClientCallOptions(http.NewClient(), http.Address("https://api.github.com"))

req := client.NewRequest("github", "/users/vtolstov", nil)
rsp := make(map[string]interface{})

err := c.Call(context.TODO(), req, &rsp, mhttp.Method(http.MethodGet)) 

Look at http_test.go for detailed use.

Encoding

Default protobuf with content-type application/proto

client.NewRequest("service", "/path", protoRequest{})

Json with content-type application/json

client.NewJsonRequest("service", "/path", jsonRequest{})

Documentation

Overview

Package http provides a http client

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultPoolMaxStreams maximum streams on a connectioin
	// (20)
	DefaultPoolMaxStreams = 20

	// DefaultPoolMaxIdle maximum idle conns of a pool
	// (50)
	DefaultPoolMaxIdle = 50

	// DefaultMaxRecvMsgSize maximum message that client can receive
	// (4 MB).
	DefaultMaxRecvMsgSize = 1024 * 1024 * 4

	// DefaultMaxSendMsgSize maximum message that client can send
	// (4 MB).
	DefaultMaxSendMsgSize = 1024 * 1024 * 4
)
View Source
var DefaultContentType = "application/json"

Functions

func Body

func Body(b string) client.CallOption

Body specifies body option to client Call

func Cookie(cookies ...string) client.CallOption

Cookie pass cookie to client Call

func ErrorMap

func ErrorMap(m map[string]interface{}) client.CallOption

func GetError

func GetError(err error) interface{}

func HTTPClient

func HTTPClient(c *http.Client) client.Option

nolint: golint HTTPClient pass http.Client option to client Call

func HTTPDialer

func HTTPDialer(d *net.Dialer) client.Option

nolint: golint HTTPDialer pass net.Dialer option to client

func Header(headers ...string) client.CallOption

Header pass cookie to client Call

func MaxRecvMsgSize

func MaxRecvMsgSize(s int) client.Option

MaxRecvMsgSize set the maximum size of message that client can receive.

func MaxSendMsgSize

func MaxSendMsgSize(s int) client.Option

MaxSendMsgSize set the maximum size of message that client can send.

func Metadata

func Metadata(md metadata.Metadata) client.CallOption

Metadata pass metadata to client Call

func Method

func Method(m string) client.CallOption

Method pass method option to client Call

func NewClient

func NewClient(opts ...client.Option) *httpClient

func Path

func Path(p string) client.CallOption

Path spcecifies path option to client Call

func PoolMaxIdle

func PoolMaxIdle(d int) client.Option

PoolMaxIdle maximum idle conns of a pool

func PoolMaxStreams

func PoolMaxStreams(n int) client.Option

PoolMaxStreams maximum streams on a connectioin

func StructTags

func StructTags(tags []string) client.CallOption

StructTags pass tags slice option to client Call

Types

type Error

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

Error struct holds error

func (*Error) Error

func (err *Error) Error() string

Error func for error interface

Jump to

Keyboard shortcuts

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