apicache

package module
v0.0.0-...-063ce97 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2014 License: BSD-2-Clause Imports: 18 Imported by: 2

Documentation

Overview

EVE Online API downloader/cacher.

Begin by initializing the default client.

NewClient(NilCache)

Create a new request for the API page needed.

req := NewRequest("eve/ConquerableStationList.xml.aspx")

Set any options you may need, including keyid/vcode.

req.Set("keyid", "1234")
req.Set("vcode", "abcd")
req.Set("charactername", "innominate")
req.Set("characterid", fmt.Sprintf("%d", 123))

Get your response.

resp, err := req.Do()
xml := resp.Data

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCannotConnect = fmt.Errorf("Error connecting to API.")
	ErrNetwork       = fmt.Errorf("Network error.")
	ErrHTTP          = fmt.Errorf("HTTP error.")
	ErrForbidden     = fmt.Errorf("HTTP Forbidden, invalid API provided.")
	ErrUnknown       = fmt.Errorf("Unknown Error.")
	ErrXML           = fmt.Errorf("Malformed XML Detected")
	ErrTime          = fmt.Errorf("Malformed cache directive.")
)
View Source
var DebugLog = log.New(ioutil.Discard, "apicache", log.Ldate|log.Ltime)
View Source
var DefaultBaseURL = "https://api.eveonline.com/"

Default BaseURL for new clients, change as necessary. Must contain trailing slash or bad things happen.

View Source
var DefaultUserAgent = "go apicache by Innominate(github.com/inominate/apicache)"
View Source
var NilCache = new(nilCache)

Fake Cacher, does nothing but can serve as a stand-in for testing.

Functions

func MakeID

func MakeID() string

func SQLCacher

func SQLCacher(db *sql.DB) (*sqlCache, error)

SQL Database Cacher Must be passed an existing database handle, returns a cacher which can be used with NewClient(). Will create its own table if necessary.

func SetMaxIdleConns

func SetMaxIdleConns(maxIdleConns int)

Set max idle conns for the default client

func SetTimeout

func SetTimeout(timeout time.Duration)

Set timeout for default client.

func SynthesizeAPIError

func SynthesizeAPIError(code int, text string, expiresIn time.Duration) []byte

Types

type APIError

type APIError struct {
	ErrorCode int    `xml:"code,attr"`
	ErrorText string `xml:",innerxml"`
}

API Error Type, hopefully contains the error code and some useful information. This is CCP so nothing is guaranteed.

func (APIError) Error

func (e APIError) Error() string

Error String Generator

type CacheEntry

type CacheEntry struct {
	HTTPCode int
	Expires  time.Time
}

type Cacher

type Cacher interface {
	Store(cacheTag string, httpCode int, data []byte, expires time.Time) error
	Get(cacheTag string) (int, []byte, time.Time, error)
}

Cachers MUST be safe for concurrent use.

type Client

type Client struct {
	// Base URL, defaults to CCPs api but can be changed to a proxy
	BaseURL string

	//Alternate user agent to use.
	UserAgent string

	// Default three retries, can be changed at will.
	Retries int

	sync.RWMutex
	// contains filtered or unexported fields
}

API Client structure. Must be created using NewClient() function.

func GetDefaultClient

func GetDefaultClient() *Client

Return the default client for

func NewClient

func NewClient(cacher Cacher) *Client

Create a new API Client. The first time this is called will become the default client. Requires a cacher.

func (*Client) Do

func (c *Client) Do(r *Request) (retresp *Response, reterr error)

Perform a request, usually called by the request itself. User friendly error is enclosed in the response, returned error should be for internal use only.

func (*Client) GetCached

func (c *Client) GetCached(r *Request) (retresp *Response, reterr error)

func (*Client) NewRequest

func (c *Client) NewRequest(url string) *Request

Create a new request.

func (*Client) SetMaxIdleConns

func (c *Client) SetMaxIdleConns(maxIdleConns int)

func (*Client) SetTimeout

func (c *Client) SetTimeout(timeout time.Duration)

Set timeout for each API request.

type DiskCache

type DiskCache struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewDiskCache

func NewDiskCache(rootDir string, clearCache bool) *DiskCache

func (*DiskCache) Close

func (d *DiskCache) Close()

func (*DiskCache) Get

func (d *DiskCache) Get(cacheTag string) (int, []byte, time.Time, error)

func (*DiskCache) LogStats

func (d *DiskCache) LogStats()

func (*DiskCache) Store

func (d *DiskCache) Store(cacheTag string, HTTPCode int, data []byte, Expires time.Time) error

type Request

type Request struct {

	// Override the CCP requested expiration time, will not reduce existing cache
	// duration, see Force below. Zero value indicates this field should be
	// ignored
	// Intended use is to force longer cache timers to align dependent calls.
	// e.g. Pull starbase and locations APIs in sync with assets.
	Expires time.Time

	// Force pull despite cache, use at own risk.
	Force bool

	// Do not cache this request, use at own risk.
	NoCache bool
	// contains filtered or unexported fields
}

API Request structure for making requests.

func NewRequest

func NewRequest(url string) *Request

Create a new request using default client.

func (*Request) Do

func (r *Request) Do() (*Response, error)

Perform a request. User friendly error is enclosed in the response, returned error should be for internal use only.

func (*Request) GetCached

func (r *Request) GetCached() (*Response, error)

Get the cached request, or return an error if not cached. User friendly error is enclosed in the response, returned error should be for internal use only.

func (*Request) Set

func (r *Request) Set(key, value string)

Set parameters for the API call. KeyID/vCode can be set here and will override the client's settings.

type Response

type Response struct {
	// Raw XML data
	Data []byte

	// Signals if we used the cache or not
	FromCache bool

	// Data expiration time
	Expires time.Time

	// true if an error occured due to invalid API rather than server problems
	Invalidate bool

	// Contains API Error if one occured
	Error APIError

	//Pass on CCP's HTTP code because why not?
	HTTPCode int
}

Data structure returned from the API.

Jump to

Keyboard shortcuts

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