1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-29 13:49:28 +08:00
Aleksandar Novaković ad5c66fad2 NOISSUE - Refactor SDK and things service (#420)
* Refactor Mainflux go SDK

Add structures instead of string parameters. Add offset and limit
parameters to things and channels methods. Add better configuration
support.

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Add new public errors with better error handling

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update SDK to use uint instread of string id

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update cli to use new SDK API

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Remove TLS termination from nginx configuration

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update SDK documentation and structures

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Refactor things service

Decouple HTTP layer from business logic. Remove ID number validation
check. Remove models from HTTP requests and responses.

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Reformat tests for things service

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Increase test coverage for things service

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2018-10-24 10:21:03 +01:00

73 lines
1.8 KiB
Go

//
// Copyright (c) 2018
// Mainflux
//
// SPDX-License-Identifier: Apache-2.0
//
package things
import "strings"
// Thing represents a Mainflux thing. Each thing is owned by one user, and
// it is assigned with the unique identifier and (temporary) access key.
type Thing struct {
ID uint64
Owner string
Type string
Name string
Key string
Metadata string
}
var thingTypes = map[string]bool{
"app": true,
"device": true,
}
// Validate returns an error if thing representation is invalid.
func (c *Thing) Validate() error {
if c.Type = strings.ToLower(c.Type); !thingTypes[c.Type] {
return ErrMalformedEntity
}
return nil
}
// ThingRepository specifies a thing persistence API.
type ThingRepository interface {
// Save persists the thing. Successful operation is indicated by non-nil
// error response.
Save(Thing) (uint64, error)
// Update performs an update to the existing thing. A non-nil error is
// returned to indicate operation failure.
Update(Thing) error
// RetrieveByID retrieves the thing having the provided identifier, that is owned
// by the specified user.
RetrieveByID(string, uint64) (Thing, error)
// RetrieveByKey returns thing ID for given thing key.
RetrieveByKey(string) (uint64, error)
// RetrieveAll retrieves the subset of things owned by the specified user.
RetrieveAll(string, uint64, uint64) []Thing
// Remove removes the thing having the provided identifier, that is owned
// by the specified user.
Remove(string, uint64) error
}
// ThingCache contains thing caching interface.
type ThingCache interface {
// Save stores pair thing key, thing id.
Save(string, uint64) error
// ID returns thing ID for given key.
ID(string) (uint64, error)
// Removes thing from cache.
Remove(uint64) error
}