Giter Club home page Giter Club logo

goat's Introduction

goat

Goat, is an HTTP client built on top of a standard Go http package, that is extremely easy to configure; no googling required. The idea is similar to https://github.com/vspaz/pyclient it supports out of the box:

  • TLS,
  • basic auth
  • delayed retries on specific errors
  • timeouts (connection, read, idle etc.)
  • keep-alive connections
  • extra helpers
  • logging
  • etc.

still, if you don't wish any configuration, you can you use it as is with default parameters. Please, see the examples below:

Local installation

go get -u github.com/vspaz/goat/pkg/[email protected]

Building a client

package main

import (
	"github.com/vspaz/goat/pkg/ghttp"
	"log"
)

func main() {
	client := ghttp.NewClientBuilder().
		WithHost("https://httpbin.org"). // optional 
		WithAuth("user", "user-password"). // optional 
		WithTls("cert.pem", "cert.pem", "ca.crt"). // optional 
		WithTlsHandshakeTimeout(10.0). // optional
		WithUserAgent("goat"). // optional
		WithRetry(3, []int{500, 503}). // optional 
		WithDelay(0.5). // optional
		WithResponseTimeout(10.0). // optional
		WithConnectionTimeout(2.0). // optional 
		WithHeadersReadTimeout(2.0). // optional
		WithLogLevel("info"). // optional 
		Build()

	resp, err := client.DoGet("/get", nil, nil)
	if err != nil {
		log.Fatal(err)
	}
	log.Println(resp.IsOk())

	// or just
	client = ghttp.NewClientBuilder().Build()
	resp, err = client.DoGet("https://httpbin.org/get", nil, nil)
	if err != nil {
		log.Fatal(err)
	}
	log.Println(resp.IsOk())
}

/GET

package main

import (
	"github.com/vspaz/goat/pkg/ghttp"
	"log"
)

type HttpBinGetResponse struct {
	Args struct {
	} `json:"args"`
	Headers struct {
		AcceptEncoding string `json:"Accept-Encoding"`
		Authorization  string `json:"Authorization"`
		Host           string `json:"Host"`
		UserAgent      string `json:"User-Agent"`
		XAmznTraceId   string `json:"X-Amzn-Trace-Id"`
	} `json:"headers"`
	Origin string `json:"origin"`
	Url    string `json:"url"`
}

func main() {
	client := ghttp.NewClientBuilder().Build()
	resp, err := client.DoGet("https://httpbin.org/get", nil, nil)
	if err != nil {
		log.Fatal(err)
	}
	log.Println(resp.IsOk())
	log.Println(resp.ToString())
}

/POST

package main

import (
	"github.com/vspaz/goat/pkg/ghttp"
	"log"
)

func main() {
	client := ghttp.NewClientBuilder().Build()
	resp, err := client.DoPost(
		"https://httpbin.org/post",
		map[string]string{"Content-Type": "application/json"},
		map[string]string{"param": "value"},
		map[string]string{"foo": "bar"})
	if err != nil {
		log.Fatal(err)
	}
	log.Println(resp.IsOk())
	log.Println(resp.ToString())
}

goat's People

Contributors

vspaz avatar

Stargazers

 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.