online

package
v0.0.35 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2021 License: LGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package online for remote interactions.

Index

Examples

Constants

View Source
const (

	// ReleaseAPI GitHub API v3 releases endpoint.
	// See: https://developer.github.com/v3/repos/releases/
	ReleaseAPI = "https://api.github.com/repos/bengarrett/retrotxtgo/releases/latest"
)

Variables

View Source
var (
	ErrJSON = errors.New("cannot understand the response body as the syntax is not json")
	ErrMash = errors.New("cannot unmarshal the json response body")
)

Functions

func Get

func Get(url, etag string) (resp *http.Response, body []byte, err error)

Get fetches a URL and returns both its response and body. If an etag is provided a "If-None-Match" header request will be included.

Example
_, d, _ := Get("https://demozoo.org/api/v1/productions/126496/", "")
_, g, _ := Get(ReleaseAPI, "")
fmt.Println("valid json api?", json.Valid(d))
fmt.Println("valid github api?", json.Valid(g))
Output:

valid json api? true
valid github api? true

func Ping

func Ping(url string) (ok bool, err error)

Ping requests a URL and determines if the status is ok.

Example
ok, _ := Ping("https://example.org")
bad, _ := Ping("https://example.com/this/url/does/not/exist")
fmt.Println(ok, bad)
Output:

true false

Types

type API

type API map[string]interface{}

API interface to store the JSON results from GitHub.

func Endpoint

func Endpoint(url, etag string) (useCache bool, data API, err error)

Endpoint request an API endpoint from the URL. A HTTP ETag can be provided to validate local data cache against the server. The useCache will return true with the etag value matches the server's ETag header.

Example
_, p, _ := Endpoint("https://demozoo.org/api/v1/productions/126496/", `W/"0708012ac3fb439a46dd5156195901b4"`)
fmt.Println("id:", p["id"])
Output:

id: 126496

Jump to

Keyboard shortcuts

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