Giter Club home page Giter Club logo

inet's Introduction

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 -u github.com/mjwhitta/inet

Usage

Minimal example:

package main

import (
    "bytes"
    "crypto/tls"
    "flag"
    "io"
    "net/http"
    "net/http/cookiejar"
    "net/url"
    "strings"

    "github.com/mjwhitta/inet"
)

var (
    debug bool
    dst   string = "https://icanhazip.com"
)

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

    println("### Advanced usage ###")

    // 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
    }

    // Enable or disable debug functionality
    c.Debug(debug) // Disabled by default

    // Set cookiejar
    c.SetJar(jar)

    if e = injectCookiesForTesting(c.Jar()); e != nil {
        return e
    }

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

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

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

    if !debug {
        println(strings.TrimSpace(string(b)))
    }

    return nil
}

func init() {
    flag.BoolVar(&debug, "debug", false, "Debug output")
    flag.Parse()

    // 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 injectCookiesForTesting(jar http.CookieJar) error {
    var cookies []*http.Cookie = []*http.Cookie{
        &http.Cookie{Name: "chocolatechip", Value: "yum"},
        &http.Cookie{Name: "oatmealraisin", Value: "gross"},
        &http.Cookie{Name: "snickerdoodle", Value: "best"},
    }
    var e error
    var uri *url.URL

    if uri, e = url.Parse(dst); e != nil {
        return e
    }

    jar.SetCookies(uri, cookies)
    return nil
}

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

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

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

    println("### Basic usage ###")

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

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

    println(strings.TrimSpace(string(b)))

    return nil
}

Links

inet's People

Contributors

mjwhitta avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.