api

package
v0.0.0-...-6858956 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package api exposes a low-level interface to the MyRadio API.

It exposes the Requester interface for types that represent connections to the API, methods for constructing Requesters, and functions and methods for using Requesters to make requests.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoMYRADIOKEY is the error thrown when MYRADIOKEY is not present in
	// the environment.
	ErrNoMYRADIOKEY = errors.New("MYRADIOKEY not in environment")
	// ErrNoKeyFile is the error thrown when there
	// is no myradio.key file.
	ErrNoKeyFile = errors.New("couldn't find any API key file")
)

Functions

func GetAPIKey

func GetAPIKey() (apikey string, err error)

GetAPIKey tries to get an API key from all possible sources. This tries the following paths for a file containing one line (the API key):

  1. Whichever path is set in the environment variable `MYRADIOKEYFILE`;
  2. `.myradio.key`, in the current directory;
  3. `.myradio.key`, in the user's home directory;
  4. `/etc/myradio.key`;
  5. `/usr/local/etc/myradio.key`.`

Types

type HTTPMethod

type HTTPMethod int

HTTPMethod guards against incorrect methods being specified through strings

const (
	//GetReq corresponds to GET
	GetReq HTTPMethod = iota
	//PostReq corresponds to POST
	PostReq
	//PutReq corresponds to PUT
	PutReq
)

func (HTTPMethod) String

func (m HTTPMethod) String() (string, error)

String converts a HTTPMethod object into a usable request method string

type Request

type Request struct {
	// The endpoint, as a suffix of the API root URL.
	Endpoint string
	// The set of mixins to use.
	Mixins []string
	// The map of parameters to use.
	Params map[string][]string
	// The type of request (i.e. GET/POST etc.)
	ReqType HTTPMethod
	// The body of the request
	Body bytes.Buffer
}

Request represents an API request being built.

func NewRequest

func NewRequest(endpoint string) *Request

NewRequest constructs a new request for the given endpoint.

func NewRequestf

func NewRequestf(format string, params ...interface{}) *Request

NewRequestf constructs a new request for the endpoint constructed by the given format string and parameters.

type Requester

type Requester interface {
	// Do fulfils an API request.
	Do(r *Request) *Response
}

Requester is the type of anything that can handle an API request.

func MockRequester

func MockRequester(message *json.RawMessage) Requester

MockRequester creates a new mocked requester.

func NewRequester

func NewRequester(apikey string, url url.URL) Requester

NewRequester creates a new 'live' requester.

type Response

type Response struct {
	// contains filtered or unexported fields
}

Response represents the result of an API request.

func (*Response) Into

func (r *Response) Into(in interface{}) error

Into unmarshals the response r into in.

func (*Response) IsEmpty

func (r *Response) IsEmpty() bool

IsEmpty checks whether the response payload is present, but empty.

func (*Response) JSON

func (r *Response) JSON() (*json.RawMessage, error)

JSON returns r as raw JSON.

Jump to

Keyboard shortcuts

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