Home

Awesome

<div align="center"> <img src="docs/assets/images/logo.png" alt="Logo fast-shot" width="320"/> <hr /> <p>A Fluent Go REST Client Library</p> <p> <a href="https://goreportcard.com/report/opus-domini/fast-shot"><img src="https://goreportcard.com/badge/opus-domini/fast-shot" alt="Go Report Badge"></a> <a href="https://godoc.org/github.com/opus-domini/fast-shot"><img src="https://godoc.org/github.com/opus-domini/fast-shot?status.svg" alt="Go Doc Badge"></a> <a href="https://github.com/opus-domini/fast-shot/actions/workflows/coverage.yml"><img src="https://github.com/opus-domini/fast-shot/actions/workflows/coverage.yml/badge.svg" alt="Converage Actions Badge"></a> <a href="https://codecov.io/gh/opus-domini/fast-shot"><img src="https://codecov.io/gh/opus-domini/fast-shot/graph/badge.svg?token=C80QDL5W7T" alt="Codecov Badge"/></a> <a href="https://github.com/opus-domini/fast-shot/blob/main/LICENSE"><img src="https://img.shields.io/github/license/opus-domini/fast-shot.svg" alt="License Badge"></a> <a href="https://github.com/avelino/awesome-go"><img src="https://awesome.re/mentioned-badge.svg" alt="Mentioned in Awesome Go"></a> </p> </div>

Fast Shot: A Fluent Go REST Client Library

Fast Shot is a robust, feature-rich, and highly configurable HTTP client for Go. Crafted with modern Go practices in mind, it offers a fluent, chainable API that allows for clean, idiomatic code.

Table of Contents

Why Fast Shot?

Features 🌟

Installation 🔧

To install Fast Shot, run the following command:

go get github.com/opus-domini/fast-shot

Quick Start 🚀

Here's how you can make a simple POST using Fast Shot:

package main

import (
    "fmt"
    fastshot "github.com/opus-domini/fast-shot"
    "github.com/opus-domini/fast-shot/constant/mime"
)

func main() {
    client := fastshot.NewClient("https://api.example.com").
        Auth().BearerToken("your_token_here").
        Build()

    payload := map[string]interface{}{
        "key1": "value1",
        "key2": "value2",
    }

    response, err := client.POST("/endpoint").
        Header().AddAccept(mime.JSON).
        Body().AsJSON(payload).
        Send()
	
    // Process response...
}

Advanced Usage 🛠️

Fluent API

Easily chain multiple settings in a single line:

client := fastshot.NewClient("https://api.example.com").
    Auth().BearerToken("your-bearer-token").
    Header().Add("My-Header", "My-Value").
    Config().SetTimeout(30 * time.Second).
    Build()

Advanced Retry Mechanism

Handle transient failures with customizable backoff strategies:

client.POST("/resource").
    Retry().SetExponentialBackoff(2 * time.Second, 5, 2.0).
    Send()

This new retry feature supports:

Out-of-the-Box Support for Client Load Balancing

Effortlessly manage multiple endpoints:

client := fastshot.NewClientLoadBalancer([]string{
    "https://api1.example.com",
    "https://api2.example.com",
    "https://api3.example.com",
    }).
    Config().SetTimeout(time.Second * 10).
    Build()

This feature allows you to distribute network traffic across several servers, enhancing the performance and reliability of your applications.

Authentication

Fast Shot supports various types of authentication:

// Bearer Token
builder.Auth().BearerToken("your-bearer-token")

// Basic Authentication
builder.Auth().BasicAuth("username", "password")

// Custom Authentication
builder.Auth().Set("custom-authentication-header")

Custom Headers and Cookies

Add your own headers and cookies effortlessly:

// Add Custom Header
builder.Header().
    Add("header", "value")

// Add Multiple Custom Headers
builder.Header().
    AddAll(map[string]string{
        "key1": "value1",
        "key2": "value2",
        "key3": "value3",
    })

// Add Custom Cookie
builder.Cookie().
    Add(&http.Cookie{Name: "session_id", Value: "id"})

Advanced Configurations

Control every aspect of the HTTP client:

// Set Timeout
builder.Config().
    SetTimeout(time.Second * 30)

// Set Follow Redirect
builder.Config().
    SetFollowRedirects(false)

// Set Custom Transport
builder.Config().
    SetCustomTransport(myCustomTransport)

// Set Proxy
builder.Config().
    SetProxy("http://my-proxy-server:port")

Contributing 🤝

We welcome contributions to Fast Shot! Here's how you can contribute:

  1. Fork the repository
  2. Create a new branch for your feature or bug fix
  3. Write your code and tests
  4. Ensure all tests pass
  5. Submit a pull request

Please make sure to update tests as appropriate and adhere to the existing coding style.

For more detailed information, check out our CONTRIBUTING.md file.