jsonapi

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2023 License: ISC Imports: 7 Imported by: 0

Documentation

Overview

Package jsonapi provides a very basic JSON REST-like API client.

In any sane world (?!) we could rely entirely on the HTTP status code and return (only) that when an error occurs... but there are many APIs, SensorPush included, that return useful data in the body regardless of the status code. There are also cases where the API returns the wrong status code, and the body is needed for a full understanding. (For instance SensorPush returns 400 with an "access denied" in the body rather than just using 401.)

The cleanest way to handle this is to unmarshal into the response object *regardless* of the status code, so that it's available even if we end up returning an error. The caller can use an anonymous embedded struct for the error fields. (Alternatively, we can include the body bytes in the returned error, for separate unmarshaling?)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	DefaultHeader http.Header
	// contains filtered or unexported fields
}

Client represents a JSON REST API client.

func NewClient

func NewClient(baseURL string) (*Client, error)

NewClient creates a new JSONAPIClient.

func (*Client) Call

func (c *Client) Call(relURL string, body interface{}, response interface{}) error

Call makes a call!

type HTTPStatusError

type HTTPStatusError struct {
	StatusCode int
	Body       []byte
}

HTTPStatusError represents an error from the HTTP layer (4xx, 5xx)

func (*HTTPStatusError) Error

func (e *HTTPStatusError) Error() string

Jump to

Keyboard shortcuts

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