gooff

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2019 License: Apache-2.0 Imports: 8 Imported by: 0

README

Gooff [Go(lang) Offline]

Cache all your internet requests when developing

While travelling on and off planes I wanted to play around with external APIs to find the data I was looking for (specifically github's API) and this meant I could hit all the end points I needed while in the airport and continue developing on the plane.

Another great benefit of this library is that it can enable you to have consistent demos when interacting with third party APIs. Try once, demo consistently.

When's Good To Use

  • Having consistent demos when hitting an external API
  • Going on and offline periodically for example when travelling on a plane
  • Saving internet costs when on mobile
  • Demoing in an area with no internet
  • API takes a long time to return
  • Decrease request count to external API
  • When exploring a third party API

When Not To Use

  • To provide use-cases for unit tests. Just no.
  • In production, I imagine...

Install

go get github.com/Rambatino/gooff

## Usage

Only support the side-effects use case for quick switching on and off

Add this to your main.go

import _ "github.com/Rambatino/gooff"

And that's it!

Some notes on the package

It will cache all your (http.Status == 200) requests and use the cached value when making repeated requests.

It supports all requests GET, POST, PUT...

As this is only for personal use, initialising in init() using package side effects seems appropriate.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GoOffline

func GoOffline(dbPath string, preferDatabase, only200 bool)

GoOffline returns the default transport, including whether to prefer db And sets up the database

Types

type Transport

type Transport struct {
	Transport http.RoundTripper
	// contains filtered or unexported fields
}

Transport implements http.RoundTripper. When set as Transport of http.Client, it will store online requests for offline usage

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip is the core part of this module and implements http.RoundTripper.

If prefer database is true, always try and return value of request from database

If there is data, return it If there is no data, then send the request anyway

Store the result, linking it to the request If the request errors, then attempt to pull from database, otherwise return as is

Jump to

Keyboard shortcuts

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