catalog

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2017 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TypeDevices   = "devices"
	TypeResources = "resources"
	CtxPath       = "/ctx/rc.jsonld"
)
View Source
const (
	DNSSDServiceType          = "_linksmart-rc._tcp"
	MaxPerPage                = 100
	ApiVersion                = "1.0.0"
	ApiName                   = "ResourceCatalog"
	ApiDeviceCollectionType   = "Devices"
	ApiResourceCollectionType = "Resources"
	ApiDeviceType             = "Device"
	ApiResourceType           = "Resource"
	CatalogBackendMemory      = "memory"
	CatalogBackendLevelDB     = "leveldb"
	StaticLocation            = "/static"
)
View Source
const (
	GetParamPage    = "page"
	GetParamPerPage = "per_page"
)
View Source
const (
	FOpEquals   = "equals"
	FOpPrefix   = "prefix"
	FOpSuffix   = "suffix"
	FOpContains = "contains"
)

Variables

This section is empty.

Functions

func ErrorMsg

func ErrorMsg(res *http.Response) string

Returns the message field of a resource.Error response

func ErrorResponse

func ErrorResponse(w http.ResponseWriter, code int, msgs ...string)

ErrorResponse writes error to HTTP ResponseWriter

func GetPageOfSlice

func GetPageOfSlice(slice []string, page, perPage, maxPerPage int) ([]string, error)

Returns a 'slice' of the given slice based on the requested 'page'

func GetPagingAttr

func GetPagingAttr(total, page, perPage, maxPerPage int) (int, int, error)

Returns offset and limit representing a subset of the given slice total size

based on the requested 'page'

func HTTPDoAuth

func HTTPDoAuth(req *http.Request, ticket *obtainer.Client) (*http.Response, error)

Send an HTTP request with Authorization entity-header.

Ticket is renewed once in case of failure.

func HTTPRequest

func HTTPRequest(method string, url string, headers map[string][]string, body io.Reader,
	ticket *obtainer.Client) (*http.Response, error)

Constructs and submits an HTTP request and returns the response

func MatchObject

func MatchObject(object interface{}, path []string, op string, value string) (bool, error)

func NewStaticHandler

func NewStaticHandler(staticDir string) http.HandlerFunc

Serves static and all /static/ctx files as ld+json

func ParsePagingParams

func ParsePagingParams(page, perPage string, maxPerPage int) (int, int, error)

Parses string paging parameters to integers

func RegisterDevice

func RegisterDevice(client CatalogClient, d *Device) error

Registers device given a configured Catalog Client

func RegisterDeviceWithKeepalive

func RegisterDeviceWithKeepalive(endpoint string, discover bool, d Device, sigCh <-chan bool, wg *sync.WaitGroup,
	ticket *obtainer.Client)

Registers device in the remote catalog endpoint: catalog endpoint. If empty - will be discovered using DNS-SD d: device registration sigCh: channel for shutdown signalisation from upstream

func ValidatePagingParams

func ValidatePagingParams(page, perPage, maxPerPage int) error

Validates paging parameters

Types

type BadRequestError

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

Bad Request

func (*BadRequestError) Error

func (e *BadRequestError) Error() string

type CatalogClient

type CatalogClient interface {
	// CRUD
	Get(id string) (*SimpleDevice, error)
	Add(d *Device) (string, error)
	Update(id string, d *Device) error
	Delete(id string) error

	// Returns a slice of Devices given:
	// page - page in the collection
	// perPage - number of entries per page
	List(page, perPage int) ([]SimpleDevice, int, error)

	// Returns a slice of Devices given: path, operation, value, page, perPage
	Filter(path, op, value string, page, perPage int) ([]SimpleDevice, int, error)

	// Returns a single resource
	GetResource(id string) (*Resource, error)

	// Returns a slice of Resources given:
	// page - page in the collection
	// perPage - number of entries per page
	ListResources(page, perPage int) ([]Resource, int, error)

	// Returns a slice of Resources given: path, operation, value, page, perPage
	FilterResources(path, op, value string, page, perPage int) ([]Resource, int, error)
}

Catalog client

func NewLocalCatalogClient

func NewLocalCatalogClient(controller CatalogController) CatalogClient

func NewRemoteCatalogClient

func NewRemoteCatalogClient(serverEndpoint string, ticket *obtainer.Client) (CatalogClient, error)

type CatalogController

type CatalogController interface {
	Stop() error
	// contains filtered or unexported methods
}

Controller interface

func NewController

func NewController(storage CatalogStorage, apiLocation string) (CatalogController, error)

type CatalogStorage

type CatalogStorage interface {
	Close() error
	// contains filtered or unexported methods
}

Storage interface

func NewLevelDBStorage

func NewLevelDBStorage(dsn string, opts *opt.Options) (CatalogStorage, error)

type ConflictError

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

Conflict (non-unique id, assignment to read-only data)

func (*ConflictError) Error

func (e *ConflictError) Error() string

type Controller

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

func (*Controller) Stop

func (c *Controller) Stop() error

Stop the controller

type Device

type Device struct {
	Id          string                 `json:"id"`
	URL         string                 `json:"url"`
	Type        string                 `json:"type"`
	Name        string                 `json:"name,omitempty"`
	Meta        map[string]interface{} `json:"meta,omitempty"`
	Description string                 `json:"description,omitempty"`
	Ttl         uint                   `json:"ttl,omitempty"`
	Created     time.Time              `json:"created"`
	Updated     time.Time              `json:"updated"`
	Expires     *time.Time             `json:"expires,omitempty"`
	Resources   Resources              `json:"resources"`
}

Device

type DeviceCollection

type DeviceCollection struct {
	Context string         `json:"@context,omitempty"`
	Id      string         `json:"id"`
	Type    string         `json:"type"`
	Devices []SimpleDevice `json:"devices"`
	Page    int            `json:"page"`
	PerPage int            `json:"per_page"`
	Total   int            `json:"total"`
}

type Devices

type Devices []Device

type Error

type Error struct {
	// Code is the (http) code of the error
	Code int `json:"code"`
	// Message is the (human-readable) error message
	Message string `json:"message"`
}

Error describes an API error (serializable in JSON)

type JSONLDResource

type JSONLDResource struct {
	Context string `json:"@context"`
	*Resource
}

type JSONLDSimpleDevice

type JSONLDSimpleDevice struct {
	Context string `json:"@context"`
	*SimpleDevice
}

type LevelDBStorage

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

LevelDB storage

func (*LevelDBStorage) Close

func (s *LevelDBStorage) Close() error

type LocalCatalogClient

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

func (*LocalCatalogClient) Add

func (self *LocalCatalogClient) Add(r *Device) (string, error)

Adds a device and returns its id

func (*LocalCatalogClient) Delete

func (self *LocalCatalogClient) Delete(id string) error

func (*LocalCatalogClient) Filter

func (self *LocalCatalogClient) Filter(path, op, value string, page, perPage int) ([]SimpleDevice, int, error)

func (*LocalCatalogClient) FilterResources

func (self *LocalCatalogClient) FilterResources(path, op, value string, page, perPage int) ([]Resource, int, error)

func (*LocalCatalogClient) Get

func (self *LocalCatalogClient) Get(id string) (*SimpleDevice, error)

func (*LocalCatalogClient) GetResource

func (self *LocalCatalogClient) GetResource(id string) (*Resource, error)

func (*LocalCatalogClient) List

func (self *LocalCatalogClient) List(page int, perPage int) ([]SimpleDevice, int, error)

func (*LocalCatalogClient) ListResources

func (self *LocalCatalogClient) ListResources(page int, perPage int) ([]Resource, int, error)

func (*LocalCatalogClient) Update

func (self *LocalCatalogClient) Update(id string, r *Device) error

type Map

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

AVL Tree: sorted nodes according to keys

A node of the AVL Tree (go-avltree)

type MemoryStorage

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

In-memory storage

func NewMemoryStorage

func NewMemoryStorage() *MemoryStorage

func (*MemoryStorage) Close

func (s *MemoryStorage) Close() error

type NotFoundError

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

Not Found

func (*NotFoundError) Error

func (e *NotFoundError) Error() string

type Protocol

type Protocol struct {
	Type         string                 `json:"type"`
	Endpoint     map[string]interface{} `json:"endpoint"`
	Methods      []string               `json:"methods,omitempty"`
	ContentTypes []string               `json:"content-types,omitempty"`
}

Protocol describes the resource API

type ReadableCatalogAPI

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

Read-only catalog api

func NewReadableCatalogAPI

func NewReadableCatalogAPI(controller CatalogController, apiLocation, staticLocation, description string) *ReadableCatalogAPI

func (*ReadableCatalogAPI) Filter

func (a *ReadableCatalogAPI) Filter(w http.ResponseWriter, req *http.Request)

Lists filtered devices in a DeviceCollection

func (*ReadableCatalogAPI) FilterResources

func (a *ReadableCatalogAPI) FilterResources(w http.ResponseWriter, req *http.Request)

Lists filtered resources in a ResourceCollection

func (*ReadableCatalogAPI) Get

Gets a single Device

func (*ReadableCatalogAPI) GetResource

func (a *ReadableCatalogAPI) GetResource(w http.ResponseWriter, req *http.Request)

Gets a single Resource

func (*ReadableCatalogAPI) Index

Index of API

func (*ReadableCatalogAPI) List

Lists devices in a DeviceCollection

func (*ReadableCatalogAPI) ListResources

func (a *ReadableCatalogAPI) ListResources(w http.ResponseWriter, req *http.Request)

Lists resources in a ResourceCollection

type RemoteCatalogClient

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

func (*RemoteCatalogClient) Add

func (c *RemoteCatalogClient) Add(d *Device) (string, error)

Adds a device and returns its id

func (*RemoteCatalogClient) Delete

func (c *RemoteCatalogClient) Delete(id string) error

Deletes a device

func (*RemoteCatalogClient) Filter

func (c *RemoteCatalogClient) Filter(path, op, value string, page, perPage int) ([]SimpleDevice, int, error)

Filters devices

func (*RemoteCatalogClient) FilterResources

func (c *RemoteCatalogClient) FilterResources(path, op, value string, page, perPage int) ([]Resource, int, error)

Filter resources

func (*RemoteCatalogClient) Get

Retrieves a device

func (*RemoteCatalogClient) GetResource

func (c *RemoteCatalogClient) GetResource(id string) (*Resource, error)

Retrieves a resource

func (*RemoteCatalogClient) List

func (c *RemoteCatalogClient) List(page int, perPage int) ([]SimpleDevice, int, error)

Retrieves a page from the device collection

func (*RemoteCatalogClient) ListResources

func (c *RemoteCatalogClient) ListResources(page int, perPage int) ([]Resource, int, error)

Retrieves a page from the resource collection

func (*RemoteCatalogClient) Update

func (c *RemoteCatalogClient) Update(id string, d *Device) error

Updates a device

type Resource

type Resource struct {
	Id             string                 `json:"id"`
	URL            string                 `json:"url"`
	Type           string                 `json:"type"`
	Name           string                 `json:"name,omitempty"`
	Meta           map[string]interface{} `json:"meta,omitempty"`
	Protocols      []Protocol             `json:"protocols"`
	Representation map[string]interface{} `json:"representation,omitempty"`
	Device         string                 `json:"device"` // URL of device
}

Resource

type ResourceCollection

type ResourceCollection struct {
	Context   string     `json:"@context,omitempty"`
	Id        string     `json:"id"`
	Type      string     `json:"type"`
	Resources []Resource `json:"resources"`
	Page      int        `json:"page"`
	PerPage   int        `json:"per_page"`
	Total     int        `json:"total"`
}

type Resources

type Resources []Resource

func (Resources) Len

func (s Resources) Len() int

Sorting operators

func (Resources) Less

func (s Resources) Less(i, j int) bool

func (Resources) Swap

func (s Resources) Swap(i, j int)

type SimpleDevice

type SimpleDevice struct {
	Device
	Resources []string `json:"resources"`
}

Device with only IDs of resources

type WritableCatalogAPI

type WritableCatalogAPI struct {
	*ReadableCatalogAPI
}

Writable catalog api

func NewWritableCatalogAPI

func NewWritableCatalogAPI(controller CatalogController, apiLocation, staticLocation, description string) *WritableCatalogAPI

func (*WritableCatalogAPI) Delete

func (a *WritableCatalogAPI) Delete(w http.ResponseWriter, req *http.Request)

Deletes a device

func (*WritableCatalogAPI) Post

Adds a Device

func (*WritableCatalogAPI) Put

Updates an existing device (Response: StatusOK) If the device does not exist, a new one will be created with the given id (Response: StatusCreated)

Jump to

Keyboard shortcuts

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