hateoas

package module
v0.0.0-...-d778dca Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2017 License: Apache-2.0 Imports: 9 Imported by: 8

README

hateoas

CircleCI Apache 2 License

HATEOAS as a concept is awesome. hateoas is Go package to populate HATEOAS responses for RESTful APIs with proper Swagger documentation.

Dependencies
Quickstart
import "github.com/byuoitav/hateoas"

In your main Go file (probably something like server.go), load your Swagger YAML file into the HATEOAS library:

err := hateoas.Load("https://raw.githubusercontent.com/byuoitav/av-api/master/swagger.yaml")
if err != nil {
	log.Fatalln("Could not load swagger.json file. Error: " + err.Error())
}
func main() {
	port := ":8000"
	router := echo.New()

	router.GET("/", echo.WrapHandler(http.HandlerFunc(hateoas.RootResponse)))

	router.Start(port)
}

In your to-be-returned structs, be sure to include a Links attribute similar to the following:

type Building struct {
	Links    []hateoas.Link `json:"links,omitempty"`
	Building string         `json:"building"`
}

In your code, before returning your structs, add the HATEOAS links:

links, err := hateoas.AddLinks(c, []string{allBuildings.Buildings[i].Building})
if err != nil {
	return c.JSON(http.StatusBadRequest, helpers.ReturnError(err))
}

allBuildings.Buildings[i].Links = links
Syntax

hateoas.AddLinks takes two arguments. The first is a string representing the current HTTP path (EG: /endpoint/:variable). The second is an array of strings specifying values for URL parameters used in the HTTP path. The strings will be used by hateoas to populate URLs for downstream endpoints.

func AddLinks(path string, parameters []string) ([]Link, error) {
	...
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EchoToSwagger

func EchoToSwagger(path string) string

EchoToSwagger converts paths from Echo syntax to Swagger syntax

func Load

func Load(fileLocation string) error

Load loads a swagger.json file from an external URL

func MergeSort

func MergeSort(first []string, second []string) string

MergeSort takes two string arrays and shuffles them together (there has to be a better way to do this)

func RootResponse

func RootResponse(writer http.ResponseWriter, request *http.Request)

RootResponse offers HATEOAS links from the root endpoint of the API

Types

type Info

type Info struct {
	Title       string `json:"title,omitempty"`
	Description string `json:"description,omitempty"`
	Version     string `json:"version,omitempty"`
}
type Link struct {
	Rel  string `json:"rel"`
	HREF string `json:"href"`
}
func AddLinks(path string, parameters []string) ([]Link, error)

AddLinks searches through given paths

type Method

type Method struct {
	Summary    string      `yaml:"summary,omitempty"`
	Parameters []Parameter `yaml:"parameters,omitempty"`
}

type Parameter

type Parameter struct {
	Name     string `yaml:"name,omitempty"`
	In       string `yaml:"in,omitempty"`
	Required bool   `yaml:"required,omitempty"`
}

type Path

type Path struct {
	Get    *Method `yaml:"get,omitempty"`
	Post   *Method `yaml:"post,omitempty"`
	Put    *Method `yaml:"put,omitempty"`
	Delete *Method `yaml:"delete,omitempty"`
}

type Root

type Root struct {
	Title       string `json:"title,omitempty"`
	Description string `json:"description,omitempty"`
	Version     string `json:"version,omitempty"`
	Links       []Link `json:"links,omitempty"`
}

Root is a generic struct utilized at the root of an API to provide initial HATEOAS links

func GetInfo

func GetInfo() Root

GetInfo returns general information about the API (mainly for the root path)

type Swagger

type Swagger struct {
	Info  Info            `json:"info,omitempty"`
	Paths map[string]Path `json:"paths"`
}

Jump to

Keyboard shortcuts

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