inet

package module
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: May 7, 2024 License: GPL-3.0 Imports: 6 Imported by: 2

README

INet

Yum

Go Report Card License

What is this?

This module is a simple wrapper around net/http, winhttp, and wininet. It creates a minimal, cross-platform HTTP Client interface and handles choosing an appropriate backend. On Windows you can use any of the three supported backends (defaults to wininet). On Linux the only supported backend is net/http.

How to install

Open a terminal and run the following:

$ go get --ldflags "-s -w" --trimpath -u github.com/mjwhitta/inet

Usage

Minimal example:

package main

import (
    "bytes"
    "crypto/tls"
    "fmt"
    "io"
    "net/http"
    "net/http/cookiejar"

    "github.com/mjwhitta/inet"
)

var dst string = "https://icanhazip.com"

func configuredRequestAndDedicatedClientExample() error {
    var body io.Reader = bytes.NewReader([]byte("test"))
    var c inet.Client
    var e error
    var jar http.CookieJar
    var req *http.Request
    var res *http.Response

    // Create cookiejar
    if jar, e = cookiejar.New(nil); e != nil {
        panic(e)
    }

    // Create client
    if c, e = inet.NewClient("My custom UA"); e != nil {
        return e
    }

    // Set cookiejar
    c.Jar(jar)

    // Create request
    req, e = http.NewRequest(http.MethodPost, dst, body)
    if e != nil {
        return e
    }

    // Configure request
    req.AddCookie(&http.Cookie{Name: "chocolatechip", Value: "tasty"})
    req.AddCookie(&http.Cookie{Name: "oatmealraisin", Value: "gross"})
    req.AddCookie(&http.Cookie{Name: "snickerdoodle", Value: "yummy"})

    // Send request
    if res, e = c.Do(req); e != nil {
        return e
    }
    defer res.Body.Close()

    return output(res)
}

func init() {
    // Disable TLS verification (WARNING: insecure)
    if t, ok := http.DefaultTransport.(*http.Transport); ok {
        if t.TLSClientConfig == nil {
            t.TLSClientConfig = &tls.Config{}
        }

        t.TLSClientConfig.InsecureSkipVerify = true
    }
}

func main() {
    if e := simpleGetExample(); e != nil {
        panic(e)
    }

    if e := configuredRequestAndDedicatedClientExample(); e != nil {
        panic(e)
    }
}

func output(res *http.Response) error {
    var b []byte
    var e error

    if b, e = io.ReadAll(res.Body); e != nil {
        return e
    }

    fmt.Println(res.Status)

    for k := range res.Header {
        fmt.Printf("%s: %s\n", k, res.Header.Get(k))
    }

    for _, cookie := range res.Cookies() {
        fmt.Printf("%s = %s\n", cookie.Name, cookie.Value)
    }

    if len(b) > 0 {
        fmt.Println(string(b))
    }

    fmt.Println()

    return nil
}

func simpleGetExample() error {
    var e error
    var res *http.Response

    // Use simple helper function
    if res, e = inet.Get(dst); e != nil {
        return e
    }
    defer res.Body.Close()

    return output(res)
}

Documentation

Index

Constants

View Source
const (
	HTTPBackend = iota // Default on non-Windows
	WinHTTPBackend
	WinINetBackend // Default on Windows
)

Supported backends

View Source
const Version string = "0.4.4"

Version is the package version

Variables

This section is empty.

Functions

func Backend

func Backend(backend int) error

Backend is used to track the preferred backend HTTP client. Only net/http is supported for non-Windows OS.

func Get

func Get(url string) (*http.Response, error)

Get will make a GET request using the DefaultClient.

func Head(url string) (*http.Response, error)

Head will make a HEAD request using the DefaultClient.

func Post

func Post(
	url string, contentType string, body io.Reader,
) (*http.Response, error)

Post will make a POST request using the DefaultClient.

func PostForm

func PostForm(url string, data url.Values) (*http.Response, error)

PostForm will make a POST request using the DefaultClient.

Types

type Client

type Client interface {
	Debug(enable bool) Client
	Do(req *http.Request) (*http.Response, error)
	Get(url string) (*http.Response, error)
	Head(url string) (*http.Response, error)
	Jar(jar http.CookieJar) Client
	Post(
		url string, contentType string, body io.Reader,
	) (*http.Response, error)
	PostForm(url string, data url.Values) (*http.Response, error)
	Timeout(timeout time.Duration) Client
	Transport(trans *http.Transport) Client
}

Client is an interface defining required functions for an HTTP client.

var (
	// DefaultClient points to the default Client for the current
	// backend.
	DefaultClient Client
)

func NewClient

func NewClient(ua ...string) (Client, error)

NewClient will return a new Client for the current Backend. An optional User-Agent can be provided for Windows backends only. User-Agent will still need to be specified for requests.

type HTTPClient

type HTTPClient struct {
	http.Client
	// contains filtered or unexported fields
}

HTTPClient is a simple wrapper around net/http that adds the Jar(http.CookieJar) method for the Client interface.

func (*HTTPClient) Debug added in v0.4.0

func (c *HTTPClient) Debug(enable bool) Client

Debug will enable debugging/logging of Requests/Responses.

func (*HTTPClient) Do added in v0.4.0

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

Do is a wrapper around net/http.Client.Do which allows for debugging of Requests/Responses.

func (*HTTPClient) Jar

func (c *HTTPClient) Jar(jar http.CookieJar) Client

Jar will set the cookiejar for the underlying http.Client.

func (*HTTPClient) Timeout added in v0.1.2

func (c *HTTPClient) Timeout(timeout time.Duration) Client

Timeout will set the timeout for the underlying http.Client.

func (*HTTPClient) Transport added in v0.3.0

func (c *HTTPClient) Transport(trans *http.Transport) Client

Transport will set the transport for the underlying http.Client.

Jump to

Keyboard shortcuts

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