client

package
v0.0.19 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package client provides a HTTP client implementation. This client is opinionated and comes with good defaults.

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
}

Client is a http.Client that has some good defaults. It also logs requests and responses using log.Logger

Use either Safe or Unsafe to get a valid client.

Clients should be reused instead of created as needed. Clients are safe for concurrent use by multiple goroutines.

see http.Client

func Safe added in v0.0.8

func Safe(l log.Logger) *Client

Safe creates a client that is safe from the server-side request forgery (SSRF) security vulnerability.

func Unsafe added in v0.0.8

func Unsafe(l log.Logger) *Client

Unsafe creates a client that is NOT safe from the server-side request forgery (SSRF) security vulnerability.

func (*Client) CloseIdleConnections

func (c *Client) CloseIdleConnections()

CloseIdleConnections closes any connections on its Transport which were previously connected from previous requests but are now sitting idle in a "keep-alive" state.

It does not interrupt any connections currently in use.

see http.Client.CloseIdleConnections

func (*Client) Do

func (c *Client) Do(ctx context.Context, req *http.Request) (resp *http.Response, err error)

Do sends an HTTP request and returns an HTTP response, following policy (such as redirects, cookies, auth) as configured on the client.

see http.Client.Do

func (*Client) Get

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

Get issues a GET to the specified URL.

see http.Client.Get

Example
package main

import (
	"context"
	"os"

	"github.com/komuw/ong/client"
	"github.com/komuw/ong/log"
)

func main() {
	l := log.New(os.Stdout, 7)

	cli := client.Safe(l)
	// This is the AWS metadata url.
	url := "http://169.254.169.254/latest/meta-data"
	_, _ = cli.Get(context.Background(), url)

	// This will log something like:
	// {"level":"info","logID":"Z5X7qXm8HkT8kZ83xQyrrQ","method":"GET","msg":"http_client","pid":11776,"process":"request","timestamp":"2022-10-09T12:03:33.851543383Z","url":"http://169.254.169.254/latest/meta-data"}
	// {"durationMS":0,"err":"Get \"http://169.254.169.254/latest/meta-data\": dial tcp 169.254.169.254:80: ong/client: address 169.254.169.254 IsLinkLocalUnicast","level":"error","logID":"Z5X7qXm8HkT8kZ83xQyrrQ","method":"GET","msg":"http_client","pid":11776,"process":"response","timestamp":"2022-10-09T12:03:33.851889217Z","url":"http://169.254.169.254/latest/meta-data"}
}
Output:

func (*Client) Head

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

Head issues a HEAD to the specified URL.

see http.Client.Head

func (*Client) Post

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

Post issues a POST to the specified URL.

see http.Client.Post

func (*Client) PostForm

func (c *Client) PostForm(ctx context.Context, url string, data url.Values) (resp *http.Response, err error)

PostForm issues a POST to the specified URL, with data's keys and values URL-encoded as the request body.

see http.Client.PostForm

Jump to

Keyboard shortcuts

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