batch

package module
v0.0.0-...-e6b2e4d Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2019 License: MIT Imports: 11 Imported by: 0

README

Build Status GoDoc Go Report Card

Batch

Batch is a simple Go library for handling multipart batch requests. It adheres to the HTTP Multipart Batched Request Format draft spec.

Note

The draft spec is not well specified when it comes to whether or not upstream requests use http or https. The http.Client expects either when it makes a request.

By default all requests made by Batch use http. However, individual requests can set the x-use-https header in the Multipart/Batch message to use https.

Here's a complete example of a Batch request where the batch message uses the x-use-https header.

POST / HTTP/1.1
Host: example.org
Content-Type: multipart/batch; type="application/http;version=1.1" boundary=batch
Mime-Version: 1.0

--batch
Content-Type: application/http;version=1.1
Content-Transfer-Encoding: binary
Content-ID: <df536860-34f9-11de-b418-0800200c9a66@example.org>
x-use-https: true

POST /example/application HTTP/1.1
Host: example.org
Content-Type: text/plain
Content-Length: 3

Foo
--batch--

Examples

Simple

Batch adheres to the http.Handler interface and it can be provided directly to http.ListenAndServe.

package main

import (
	"net/http"

	"github.com/iamatypeofwalrus/batch"
)

func main() {
	b := batch.New()
	http.ListenAndServe(":8080", b)
}
Custom

You can customize the HTTP Client that Batch uses to perform a single request by providing it with any struct that adheres to the batch.HTTPClient interface. The http.Client adheres to this interface.

You can provide Batch with a batch.Logger in order to capture any error messages that occur when processing requests. log.Logger adheres to this interface.

package main

import (
	"log"
	"net/http"
	"os"

	"github.com/iamatypeofwalrus/batch"
)

func main() {
	l := log.New(os.Stdout, "", log.LstdFlags)
	b := &batch.Batch{
		Log:    l,
		Client: http.DefaultClient,
	}

	http.HandleFunc("/batch", b.ServeHTTP)

	log.Println("listening on :8080")
	http.ListenAndServe(":8080", nil)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Batch

type Batch struct {
	Log    Logger
	Client HTTPClient
}

Batch conforms to the http.Handler interface and can be used natively or with the HandlerFunc methods to assign the ServeHTTP method to a particular route.

func New

func New() *Batch

New returns an initialized Batch struct that uses the http.DefaultClient

func (*Batch) ServeHTTP

func (b *Batch) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP accepts and performs batch HTTP requests per the following specification:

https://tools.ietf.org/id/draft-snell-http-batch-00.html.

type HTTPClient

type HTTPClient interface {
	Do(*http.Request) (*http.Response, error)
}

HTTPClient is any interface that can perform an HTTP request and return a response and an error6t

type Logger

type Logger interface {
	Print(v ...interface{})
}

Logger is a logging interface that Batch will use to communicate internal errors

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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