client

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package client provides HTTP client implementation. The client provided in here 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 SafeClient or UnsafeClient 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 SafeClient

func SafeClient(l log.Logger) *Client

SafeClient creates a client that is safe from Server-side request forgery (SSRF) security vulnerability.

func UnsafeClient

func UnsafeClient(l log.Logger) *Client

UnsafeClient creates a client that is NOT safe from 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() {
	ctx := context.Background()
	l := log.New(ctx, os.Stdout, 7)

	cli := client.SafeClient(l)
	_, _ = cli.Get(ctx, "https://ajmsmsYnns-bad-domain.com")

	// This will log:
	// {"level":"info","logID":"D2MH3e3BqZmRgm3WK8yK7Q","method":"GET","msg":"http_client","pid":2102616,"process":"request","timestamp":"2022-09-16T09:39:55.423743309Z","url":"https://ajmsmsYnns-bad-domain.com"}
	// {"durationMS":339,"err":"Get \"https://ajmsmsYnns-bad-domain.com\": dial tcp: lookup ajmsmsYnns-bad-domain.com on 127.0.0.53:53: no such host","level":"error","logID":"D2MH3e3BqZmRgm3WK8yK7Q","method":"GET","msg":"http_client","pid":2102616,"process":"response","timestamp":"2022-09-16T09:39:55.762989726Z","url":"https://ajmsmsYnns-bad-domain.com"}
}
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