client

package
v0.0.0-...-f012150 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package client provides a wrapper of Go standard http module with recommended defaults values and context.

Get and Post requests

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := client.New()
resp, err := c.Get(ctx, "http://example.com")
...

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := client.Client(WithMaxIdleConns(5 * time.Second))
resp, err := c.Post(ctx, "http://example.com", "text/plain", &buf)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

A Client represents HTTP client to send request to server.

func New

func New(opts ...Option) *Client

New returns a new instances of Client. It sets the necessary defaults if configuration is nil. It will also set any missing fields with defaults variables.

func (*Client) Do

func (c *Client) Do(req *http.Request) (*http.Response, error)

func (*Client) Get

func (c *Client) Get(ctx context.Context, url string, headers ...Header) (resp *http.Response, err error)

Get issues a GET request to the URL.

Example
package main

import (
	"context"
	"fmt"
	"io"
	"log"

	"github.com/miguel250/kuma/http/client"
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	c := client.New()
	resp, err := c.Get(ctx, "http://www.google.com/robots.txt")
	if err != nil {
		log.Fatal(err)
	}

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}

	defer resp.Body.Close()

	if resp.StatusCode == 200 {
		log.Fatalf("request failed with status code %d and\nbody: %s\n", resp.StatusCode, body)
	}

	fmt.Printf("%s", body)
}
Output:

func (*Client) Post

func (c *Client) Post(ctx context.Context, url string, body io.Reader, headers ...Header) (resp *http.Response, err error)

Post issues a POST request to the URL and sends a body to server.

type Header struct {
	Key   string
	Value string
}

Header represents a HTTP header

func WithHeader

func WithHeader(key, value string) Header

type Option

type Option func(*config)

func WithDefaultHeader

func WithDefaultHeader(key, value string) Option

WithDefaultHeader specifies header to be used by default for all requests. This function can be called multiple times.

func WithEnableHTTP2

func WithEnableHTTP2(b bool) Option

WithEnableHTTP2 controls whether to use HTTP2

func WithExpectContinueTimeout

func WithExpectContinueTimeout(d time.Duration) Option

WithExpectContinueTimeout is the maximum amount of time to wait for a server first response.

func WithIdleConnTimeout

func WithIdleConnTimeout(d time.Duration) Option

WithIdleConnTimeout is the maximum amount of time a keep-alive connection is kept open.

func WithKeepAlive

func WithKeepAlive(d time.Duration) Option

WithKeepAlive specifies the interval between keep-alive probes.

func WithMaxIdleConns

func WithMaxIdleConns(max int) Option

WithMaxIdleConns controls maximum of idle connections using keep-alive.

func WithTLSConfig

func WithTLSConfig(tlsConf *tls.Config) Option

WithTLSConfig specifies necessary TLS configuration.

func WithTLSHandshakeTimeout

func WithTLSHandshakeTimeout(d time.Duration) Option

WithTLSHandshakeTimeout specifies the time limit to wait for a TLS handshake.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout specifies the time limit for a request including getting a response from server.

Jump to

Keyboard shortcuts

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