apiutil

package
v0.33.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// BearerPrefix represents the token prefix for Bearer authentication scheme.
	BearerPrefix = "Bearer "
	// ThingKeyPrefixInternal represents the key prefix for Thing authentication based on an internal key.
	ThingKeyPrefixInternal = "Thing "
	// ThingKeyPrefixExternal represents the key prefix for Thing authentication based on an externaly defined key.
	ThingKeyPrefixExternal = "External "
)
View Source
const (
	OffsetKey              = "offset"
	LimitKey               = "limit"
	NameKey                = "name"
	OrderKey               = "order"
	DirKey                 = "dir"
	MetadataKey            = "metadata"
	IDKey                  = "id"
	SerialKey              = "serial"
	EmailKey               = "email"
	PayloadKey             = "payload"
	SubtopicKey            = "subtopic"
	ProtocolKey            = "protocol"
	ValueKey               = "v"
	StringValueKey         = "vs"
	DataValueKey           = "vd"
	BoolValueKey           = "vb"
	ComparatorKey          = "comparator"
	FromKey                = "from"
	ToKey                  = "to"
	NameOrder              = "name"
	IDOrder                = "id"
	AscDir                 = "asc"
	DescDir                = "desc"
	ContentTypeJSON        = "application/json"
	ContentTypeCSV         = "text/csv"
	ContentTypeOctetStream = "application/octet-stream"
	DefOffset              = 0
	DefLimit               = 10
)

Variables

View Source
var (
	// ErrBearerToken indicates missing or invalid bearer user token.
	ErrBearerToken = errors.New("missing or invalid bearer user token")

	// ErrBearerKey indicates missing or invalid bearer entity key.
	ErrBearerKey = errors.New("missing or invalid bearer entity key")

	// ErrMissingGroupID indicates missing group ID.
	ErrMissingGroupID = errors.New("missing group id")

	// ErrMissingOrgID indicates missing org ID.
	ErrMissingOrgID = errors.New("missing org id")

	// ErrMissingThingID indicates missing thing ID.
	ErrMissingThingID = errors.New("missing thing id")

	// ErrMissingProfileID indicates missing profile ID.
	ErrMissingProfileID = errors.New("missing profile id")

	// ErrMissingMemberID indicates missing member ID.
	ErrMissingMemberID = errors.New("missing member id")

	// ErrMissingNotifierID indicates missing notifier ID.
	ErrMissingNotifierID = errors.New("missing notifier id")

	// ErrMissingAlarmID indicates missing alarm ID.
	ErrMissingAlarmID = errors.New("missing alarm id")

	// ErrMissingRuleID indicates missing rule ID.
	ErrMissingRuleID = errors.New("missing rule id")

	// ErrMissingUserID indicates missing user ID.
	ErrMissingUserID = errors.New("missing user id")

	// ErrMissingRole indicates missing role.
	ErrMissingRole = errors.New("missing role")

	// ErrMissingObject indicates missing object.
	ErrMissingObject = errors.New("missing object")

	// ErrMissingKeyID indicates missing ID of key.
	ErrMissingKeyID = errors.New("missing key ID")

	// ErrMissingExternalThingKey indicates missing external thing key
	ErrMissingExternalThingKey = errors.New("missing external thing key")

	// ErrMissingInviteID incidates missing ID of Invite.
	ErrMissingInviteID = errors.New("missing invite ID")

	// ErrInvalidSubject indicates invalid subject.
	ErrInvalidSubject = errors.New("invalid subject")

	// ErrInvalidAction indicates invalid action.
	ErrInvalidAction = errors.New("invalid action")

	// ErrInvalidAuthKey indicates invalid auth key.
	ErrInvalidAuthKey = errors.New("invalid auth key")

	// ErrInvalidIDFormat indicates an invalid ID format.
	ErrInvalidIDFormat = errors.New("invalid id format provided")

	// ErrNameSize indicates that name size exceeds the max.
	ErrNameSize = errors.New("invalid name size")

	// ErrEmailSize indicates that email size exceeds the max.
	ErrEmailSize = errors.New("invalid email size")

	// ErrInvalidStatus indicates an invalid user account status.
	ErrInvalidStatus = errors.New("invalid user account status")

	// ErrLimitSize indicates that an invalid limit.
	ErrLimitSize = errors.New("invalid limit size")

	// ErrOffsetSize indicates an invalid offset.
	ErrOffsetSize = errors.New("invalid offset size")

	// ErrInvalidOrder indicates an invalid list order.
	ErrInvalidOrder = errors.New("invalid list order provided")

	// ErrInvalidDirection indicates an invalid list direction.
	ErrInvalidDirection = errors.New("invalid list direction provided")

	// ErrEmptyList indicates that entity data is empty.
	ErrEmptyList = errors.New("empty list provided")

	// ErrMissingSerial indicates missing serial.
	ErrMissingSerial = errors.New("missing serial")

	// ErrMissingCertData indicates missing cert data (ttl, key_type or key_bits).
	ErrMissingCertData = errors.New("missing certificate data")

	// ErrInvalidContact indicates an invalid contact.
	ErrInvalidContact = errors.New("invalid contact")

	// ErrMissingEmail indicates missing email.
	ErrMissingEmail = errors.New("missing email")

	// ErrMissingEmailToken indicates a missing e-mail verification token.
	ErrMissingEmailToken = errors.New("missing e-mail verification token")

	// ErrMissingRedirectPath indicates missing endpoint path from the frontend.
	ErrMissingRedirectPath = errors.New("missing redirect path")

	// ErrMissingPass indicates missing password.
	ErrMissingPass = errors.New("missing password")

	// ErrMissingConfPass indicates missing conf password.
	ErrMissingConfPass = errors.New("missing conf password")

	// ErrInvalidResetPass indicates an invalid reset password.
	ErrInvalidResetPass = errors.New("invalid reset password")

	// ErrInvalidComparator indicates an invalid comparator.
	ErrInvalidComparator = errors.New("invalid comparator")

	// ErrInvalidAPIKey indicates an invalid API key type.
	ErrInvalidAPIKey = errors.New("invalid api key type")

	// ErrUnsupportedContentType indicates unacceptable or lack of Content-Type
	ErrUnsupportedContentType = errors.New("unsupported content type")

	// ErrInvalidQueryParams indicates invalid query parameters
	ErrInvalidQueryParams = errors.New("invalid query parameters")

	// ErrInvalidAggType indicates invalid aggregation type
	ErrInvalidAggType = errors.New("invalid aggregation type")

	// ErrInvalidAggInterval indicates invalid aggregation interval
	ErrInvalidAggInterval = errors.New("invalid aggregation interval")

	// ErrNotFoundParam indicates that the parameter was not found in the query
	ErrNotFoundParam = errors.New("parameter not found in the query")

	// ErrMalformedEntity indicates a malformed entity specification.
	ErrMalformedEntity = errors.New("malformed entity specification")

	// ErrInvalidRole indicates an invalid role.
	ErrInvalidRole = errors.New("invalid role")

	// ErrMissingConditionField indicates a missing condition field
	ErrMissingConditionField = errors.New("missing condition field")

	// ErrMissingConditionComparator indicates a missing condition operator
	ErrMissingConditionComparator = errors.New("missing condition comparator")

	// ErrMissingConditionThreshold indicates a missing condition threshold
	ErrMissingConditionThreshold = errors.New("missing condition threshold")

	// ErrInvalidActionType indicates an invalid action type
	ErrInvalidActionType = errors.New("missing or invalid action type")

	// ErrMissingActionID indicates a missing action id
	ErrMissingActionID = errors.New("missing action id")

	// ErrInvalidOperator indicates an invalid logical operator
	ErrInvalidOperator = errors.New("missing or invalid logical operator")

	// ErrInviteExpired indicates that an invite has expired
	ErrInviteExpired = errors.New("invite expired")

	// ErrInviteExpired indicates that an invite is in an invalid state for a certain action to be performed on it
	ErrInvalidInviteState = errors.New("invalid invite state")

	// ErrUserAlreadyInvited indicates that the invitee already has a pending invitation to join the same Org
	ErrUserAlreadyInvited = errors.New("user already has pending invite to org")

	// ErrInvalidThingKeyType indicates an invalid or missing type of thing authentication key
	ErrInvalidThingKeyType = errors.New("invalid thing key type")
)

Errors defined in this file are used by the LoggingErrorEncoder decorator to distinguish and log API request validation errors and avoid that service errors are logged twice.

Functions

func EncodeError added in v0.26.0

func EncodeError(err error, w http.ResponseWriter)

func EncodeFileResponse added in v0.29.0

func EncodeFileResponse(_ context.Context, w http.ResponseWriter, response interface{}) (err error)

func EncodeGRPCError added in v0.31.0

func EncodeGRPCError(st *status.Status, w http.ResponseWriter)

Map a gRPC-error Status code to an HTTP status code and write it to w

func ExtractBearerToken

func ExtractBearerToken(r *http.Request) string

ExtractBearerToken returns value of the bearer token. If there is no bearer token - an empty value is returned.

func LoggingErrorEncoder

func LoggingErrorEncoder(logger logger.Logger, enc kithttp.ErrorEncoder) kithttp.ErrorEncoder

LoggingErrorEncoder is a go-kit error encoder logging decorator.

func ReadBoolQuery

func ReadBoolQuery(r *http.Request, key string, def bool) (bool, error)

ReadBoolQuery reads boolean query parameters in a given http request

func ReadFloatQuery

func ReadFloatQuery(r *http.Request, key string, def float64) (float64, error)

ReadFloatQuery reads the value of float64 http query parameters for a given key

func ReadIntQuery

func ReadIntQuery(r *http.Request, key string, def int64) (int64, error)

func ReadLimitQuery

func ReadLimitQuery(r *http.Request, key string, def uint64) (uint64, error)

ReadLimitQuery reads the value of limit http query parameters

func ReadMetadataQuery

func ReadMetadataQuery(r *http.Request, key string, def map[string]interface{}) (map[string]interface{}, error)

ReadMetadataQuery reads the value of json http query parameters for a given key

func ReadStringQuery

func ReadStringQuery(r *http.Request, key string, def string) (string, error)

ReadStringQuery reads the value of string http query parameters for a given key

func ReadUintQuery

func ReadUintQuery(r *http.Request, key string, def uint64) (uint64, error)

ReadUintQuery reads the value of uint64 http query parameters for a given key

func ValidatePageMetadata added in v0.26.0

func ValidatePageMetadata(pm PageMetadata, maxLimitSize, maxNameSize int) error

func ValidateUUID added in v0.26.0

func ValidateUUID(extID string) (err error)

func WriteErrorResponse added in v0.26.0

func WriteErrorResponse(err error, w http.ResponseWriter)

Types

type ErrorRes

type ErrorRes struct {
	Err string `json:"error"`
}

ErrorRes represents the HTTP error response body.

type PageMetadata added in v0.26.0

type PageMetadata struct {
	Total    uint64
	Offset   uint64                 `json:"offset,omitempty"`
	Limit    uint64                 `json:"limit,omitempty"`
	Name     string                 `json:"name,omitempty"`
	Order    string                 `json:"order,omitempty"`
	Dir      string                 `json:"dir,omitempty"`
	Metadata map[string]interface{} `json:"metadata,omitempty"`
	Email    string                 `json:"email,omitempty"`
	Payload  map[string]interface{} `json:"payload,omitempty"`
}

PageMetadata contains page metadata that helps navigation.

func BuildPageMetadata added in v0.26.0

func BuildPageMetadata(r *http.Request) (PageMetadata, error)

func BuildPageMetadataFromBody added in v0.29.0

func BuildPageMetadataFromBody(r *http.Request) (PageMetadata, error)

type Response

type Response interface {
	// Code returns HTTP response code.
	Code() int

	// Headers returns map of HTTP headers with their values.
	Headers() map[string]string

	// Empty indicates if HTTP response has content.
	Empty() bool
}

Response contains HTTP response specific methods.

type ViewFileRes added in v0.29.0

type ViewFileRes struct {
	File     []byte
	FileName string
}

func (ViewFileRes) Code added in v0.29.0

func (res ViewFileRes) Code() int

func (ViewFileRes) Empty added in v0.29.0

func (res ViewFileRes) Empty() bool

func (ViewFileRes) Headers added in v0.29.0

func (res ViewFileRes) Headers() map[string]string

Jump to

Keyboard shortcuts

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