sdk

package
v0.25.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

README

Mainflux Go SDK

Go SDK, a Go driver for Mainflux HTTP API.

Does both system administration (provisioning) and messaging.

Installation

Import "github.com/MainfluxLabs/mainflux/sdk/go" in your Go package.

import "github.com/MainfluxLabs/mainflux/pkg/sdk/go"```

Then call SDK Go functions to interact with the system.

## API Reference

```go
FUNCTIONS

func NewSDK(conf Config) mfSDK

func (sdk mfSDK) CreateUser(token string, user User) (string, error)
    CreateUser - create user

func (sdk mfSDK) User(token, id string) (User, error)
    User - gets user

func (sdk mfSDK) Users(token string, pm PageMetadata) (UsersPage, error)
    Users - gets list of users.
    
func (sdk mfSDK) CreateToken(user User) (string, error)
    CreateToken - create user token

func (sdk mfSDK) RegisterUser(user User) (string, error)
    RegisterUser - registers mainflux user
    
func (sdk mfSDK) UpdateUser(user User, token string) error
    UpdateUser - update user

func (sdk mfSDK) UpdatePassword(oldPass, newPass, token string) error
    UpdatePassword - update user password

func (sdk mfSDK) CreateThing(thing Thing, groupID, token string) (string, error)
    CreateThing - creates new thing and generates thing UUID

func (sdk mfSDK) CreateThings(things []Thing, groupID, token string) ([]Thing, error)
    CreateThings - registers new things 
	
func (sdk mfSDK) Things(token string, pm PageMetadata) (ThingsPage, error)
     Things - gets all things
	
func (sdk mfSDK) ThingsByProfile(token, profileID string, offset, limit uint64) (ThingsPage, error)
    ThingsByProfile - gets things by profile
	
func (sdk mfSDK) Thing(id, token string) (Thing, error)
    Thing - gets thing by id.
    
func (sdk mfSDK) MetadataByKey(thingKey string) (Metadata, error)
    MetadataByKey - gets thing metadata by key.

func (sdk mfSDK) UpdateThing(thing Thing, token string) error
    UpdateThing - updates existing thing
    
func (sdk mfSDK) DeleteThing(id, token string) error
    DeleteThing - removes thing

func (sdk mfSDK) DeleteThings(ids []string, token string) error
    DeleteThings - removes existing things
	
func (sdk mfSDK) IdentifyThing(key string) (string, error)
     IdentifyThing - validates thing's key and returns its ID
	
func (sdk mfSDK) CreateGroup(group Group, orgID, token string) (string, error)
    CreateGroup - creates new group
	
func (sdk mfSDK) DeleteGroup(id, token string) error
    DeleteGroup - deletes users group

func (sdk mfSDK) DeleteGroups(ids []string, token string) error
	DeleteGroups - delete users groups
	
func (sdk mfSDK) Groups(meta PageMetadata, token string) (GroupsPage, error)
    Groups - returns page of groups
	
func (sdk mfSDK) Group(id, token string) (Group, error)
    Group - returns users group by id
	 
func (sdk mfSDK) ListThingsByGroup(groupID, token string, offset, limit uint64) (ThingsPage, error)
    ListThingsByGroup - lists things by group

func (sdk mfSDK) ViewGroupByThing(thingID, token string) (Group, error)
    ViewGroupByThing - retrieves a group by thing
    
func (sdk mfSDK) UpdateGroup(group Group, token string) error
    UpdateGroup - updates existing group
    
func (sdk mfSDK) CreateProfile(profile Profile, groupID, token string) (string, error)
    CreateProfile - creates new profile and generates UUID

func (sdk mfSDK) CreateProfiles(profiles []Profile, groupID, token string) ([]Profile, error)
    CreateProfiles - registers new profiles
    
func (sdk mfSDK) Profiles(token string) ([]things.Profile, error)
    Profiles - gets all profiles

func (sdk mfSDK) ViewProfileByThing(token, thingID string) (Profile, error)
    ViewProfileByThing - returns profile by thing
    
func (sdk mfSDK) Profile(id, token string) (things.Profile, error)
    Profile - gets profile by ID

func (sdk mfSDK) UpdateProfile(profile Profile, token string) error
    UpdateProfile - updates existing profile
    
func (sdk mfSDK) DeleteProfile(id, token string) error
    DeleteProfile - removes profile

func (sdk mfSDK) DeleteProfiles(ids []string, token string) error
    DeleteProfiles - removes existing profiles
    
func (sdk mfSDK) ListProfilesByGroup(groupID, token string, offset, limit uint64) (ProfilesPage, error)
    ListProfilesByGroup - lists profiles by group
    
func (sdk mfSDK) ViewGroupByProfile(profileID, token string) (Group, error)
    ViewGroupByProfile retrieves a group by profile
    
func (sdk mfSDK) CreateRolesByGroup(roles []GroupMember, groupID, token string) error
    CreateRolesByGroup - creates new roles by group
    
func (sdk mfSDK) UpdateRolesByGroup(roles []GroupMember, groupID, token string) error
    UpdateRolesByGroup - updates existing group roles.
	
func (sdk mfSDK) RemoveRolesByGroup(ids []string, groupID, token string) error
    RemoveRolesByGroup - removes existing group roles
	
func (sdk mfSDK) ListRolesByGroup(groupID, token string, offset, limit uint64) (GroupMembersPage, error)
    ListRolesByGroup - lists roles by group
 
func (sdk mfSDK) CreateOrg(org Org, token string) error
    CreateOrg - registers new org
	
func (sdk mfSDK) Org(id, token string) (Org, error)
    Org - returns org data by id
	
func (sdk mfSDK) UpdateOrg(o Org, token string) error
    UpdateOrg - updates existing org

func (sdk mfSDK) DeleteOrg(id, token string) error
    DeleteOrg - removes existing org

func (sdk mfSDK) Orgs(meta PageMetadata, token string) (OrgsPage, error)
    Orgs - returns page of orgs

func (sdk mfSDK) ViewMember(orgID, memberID, token string) (Member, error)
    ViewMember - retrieves a member belonging to the specified org
	
func (sdk mfSDK) AssignMembers(om []OrgMember, orgID, token string) error
    AssignMembers - assigns a members to the org
	
func (sdk mfSDK) UnassignMembers(token, orgID string, memberIDs ...string) error
    UnassignMembers - unassigns a members from the specified org
    
func (sdk mfSDK) UpdateMember(member OrgMember, token string) error
    UpdateMember - updates existing member

func (sdk mfSDK) ListMembersByOrg(orgID, token string, offset, limit uint64) (MembersPage, error)
    ListMembersByOrg - lists members by org
	
func (sdk mfSDK) ListOrgsByMember(memberID, token string, offset, limit uint64) (OrgsPage, error)
    ListOrgsByMember - lists orgs by member
	
func (sdk mfSDK) CreateWebhooks(whs []Webhook, groupID, token string) ([]Webhook, error)
    CreateWebhooks - creates new webhooks
	
func (sdk mfSDK) ListWebhooksByGroup(groupID, token string) (Webhooks, error)
    ListWebhooksByGroup - lists webhooks by group
	
func (sdk mfSDK) Webhook(webhookID, token string) (Webhook, error)
    Webhook - returns webhook by id
	
func (sdk mfSDK) UpdateWebhook(wh Webhook, token string) error
    UpdateWebhook - updates existing webhook
	
func (sdk mfSDK) DeleteWebhooks(ids []string, groupID, token string) error
    DeleteWebhooks - removes existing webhooks
    
func (sdk mfSDK) SendMessage(profileID, msg, token string) error
    SendMessage - send message on Mainflux Profile

func (sdk mfSDK) ReadMessages(profileID, token string) (MessagesPage, error)
    ReadMessages - read messages of specified profile

func (sdk mfSDK) SetContentType(ct ContentType) error
    SetContentType - set message content type. Available options are SenML JSON, custom JSON and custom binary (octet-stream).

func (sdk mfSDK) Health() (mainflux.HealthInfo, error)
    Health - things service health check

func (sdk mfSDK) IssueCert(thingID string, keyBits int, keyType, valid, token string) (Cert, error)
    IssueCert - issues a certificate for a thing required for mtls

func (sdk mfSDK) RemoveCert(id, token string) error
    RemoveCert - removes a certificate

func (sdk mfSDK) RevokeCert(thingID, certID, token string) error
    RevokeCert - revokes certificate with certID for thing with thingID

func (sdk mfSDK) Issue(token string, duration time.Duration) (KeyRes, error)
    Issue - issues a new key, returning its token value alongside
	
func (sdk mfSDK) Revoke(token, id string) error
    Revoke - removes the key with the provided ID 
    
func (sdk mfSDK) RetrieveKey(token, id string) (retrieveKeyRes, error)
	RetrieveKey - retrieves data for the key identified by the provided ID

Documentation

Index

Constants

View Source
const (
	// LoginKey is temporary User key received on successfull login.
	LoginKey uint32 = iota
	// RecoveryKey represents a key for resseting password.
	RecoveryKey
	// APIKey enables the one to act on behalf of the user.
	APIKey
)

Variables

View Source
var (
	// ErrFailedCreation indicates that entity creation failed.
	ErrFailedCreation = errors.New("failed to create entity")

	// ErrFailedUpdate indicates that entity update failed.
	ErrFailedUpdate = errors.New("failed to update entity")

	// ErrFailedFetch indicates that fetching of entity data failed.
	ErrFailedFetch = errors.New("failed to fetch entity")

	// ErrFailedRemoval indicates that entity removal failed.
	ErrFailedRemoval = errors.New("failed to remove entity")

	// ErrFailedPublish indicates that publishing message failed.
	ErrFailedPublish = errors.New("failed to publish message")

	// ErrFailedRead indicates that read messages failed.
	ErrFailedRead = errors.New("failed to read messages")

	// ErrInvalidContentType indicates that non-existent message content type
	// was passed.
	ErrInvalidContentType = errors.New("Unknown Content Type")

	// ErrFetchHealth indicates that fetching of health check failed.
	ErrFetchHealth = errors.New("failed to fetch health check")

	// ErrCerts indicates error fetching certificates.
	ErrCerts = errors.New("failed to fetch certs data")

	// ErrCertsRemove indicates failure while cleaning up from the Certs service.
	ErrCertsRemove = errors.New("failed to remove certificate")

	// ErrMemberAdd failed to add member to a group.
	ErrMemberAdd = errors.New("failed to add member to group")
)

Functions

This section is empty.

Types

type Cert

type Cert struct {
	CACert     string `json:"issuing_ca,omitempty"`
	ClientKey  string `json:"client_key,omitempty"`
	ClientCert string `json:"client_cert,omitempty"`
}

Cert represents certs data.

type Config

type Config struct {
	AuthURL        string
	BootstrapURL   string
	CertsURL       string
	HTTPAdapterURL string
	ReaderURL      string
	ThingsURL      string
	WebhooksURL    string
	UsersURL       string

	MsgContentType  ContentType
	TLSVerification bool
}

Config contains sdk configuration parameters.

type ContentType

type ContentType string

ContentType represents all possible content types.

const (
	// CTJSON represents JSON content type.
	CTJSON ContentType = "application/json"

	// CTJSONSenML represents JSON SenML content type.
	CTJSONSenML ContentType = "application/senml+json"

	// CTBinary represents binary content type.
	CTBinary ContentType = "application/octet-stream"
)

type Group

type Group struct {
	ID          string                 `json:"id,omitempty"`
	Name        string                 `json:"name,omitempty"`
	OwnerID     string                 `json:"owner_id,omitempty"`
	OrgID       string                 `json:"org_id,omitempty"`
	Description string                 `json:"description,omitempty"`
	Metadata    map[string]interface{} `json:"metadata,omitempty"`
	CreatedAt   time.Time              `json:"created_at,omitempty"`
	UpdatedAt   time.Time              `json:"updated_at,omitempty"`
}

Group represents mainflux users group.

type GroupMember

type GroupMember struct {
	ID    string `json:"id,omitempty"`
	Role  string `json:"role,omitempty"`
	Email string `json:"email,omitempty"`
}

GroupMember represents mainflux Group Member.

type GroupMembersPage added in v0.24.0

type GroupMembersPage struct {
	GroupMembers []GroupMember `json:"group_members"`
	// contains filtered or unexported fields
}

GroupMembersPage contains a list of roles for a certain group with proper metadata.

type GroupsPage

type GroupsPage struct {
	Groups []Group `json:"groups"`
	// contains filtered or unexported fields
}

GroupsPage contains list of groups in a page with proper metadata.

type Key

type Key struct {
	ID        string
	Type      uint32
	IssuerID  string
	Subject   string
	IssuedAt  time.Time
	ExpiresAt time.Time
}

type KeyRes

type KeyRes struct {
	ID        string     `json:"id,omitempty"`
	Value     string     `json:"value,omitempty"`
	IssuedAt  time.Time  `json:"issued_at,omitempty"`
	ExpiresAt *time.Time `json:"expires_at,omitempty"`
}

func (KeyRes) Code

func (res KeyRes) Code() int

func (KeyRes) Empty

func (res KeyRes) Empty() bool

func (KeyRes) Headers

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

type Member

type Member struct {
	ID    string `json:"id"`
	Email string `json:"email"`
	Role  string `json:"role"`
}

type MembersPage

type MembersPage struct {
	Members []Member `json:"members"`
	// contains filtered or unexported fields
}

MembersPage contains list of members in a page with proper metadata.

type MessagesPage

type MessagesPage struct {
	Messages []senml.Message `json:"messages,omitempty"`
	// contains filtered or unexported fields
}

MessagesPage contains list of messages in a page with proper metadata.

type Metadata added in v0.25.0

type Metadata map[string]interface{}

type Org

type Org struct {
	ID          string                 `json:"id,omitempty"`
	OwnerID     string                 `json:"owner_id,omitempty"`
	Name        string                 `json:"name,omitempty"`
	Description string                 `json:"description,omitempty"`
	Metadata    map[string]interface{} `json:"metadata,omitempty"`
	CreatedAt   time.Time              `json:"created_at,omitempty"`
	UpdatedAt   time.Time              `json:"updated_at,omitempty"`
}

Org represents mainflux org.

type OrgMember

type OrgMember struct {
	MemberID  string    `json:"member_id,omitempty"`
	OrgID     string    `json:"org_id,omitempty"`
	Role      string    `json:"role,omitempty"`
	CreatedAt time.Time `json:"created_at,omitempty"`
	UpdatedAt time.Time `json:"updated_at,omitempty"`
	Email     string    `json:"email,omitempty"`
}

OrgMember represents mainflux Org Member.

type OrgsPage

type OrgsPage struct {
	Orgs []Org `json:"orgs"`
	// contains filtered or unexported fields
}

OrgsPage contains list of orgs in a page with proper metadata.

type PageMetadata

type PageMetadata struct {
	Total    uint64                 `json:"total"`
	Offset   uint64                 `json:"offset"`
	Limit    uint64                 `json:"limit"`
	Subtopic string                 `json:"subtopic,omitempty"`
	Format   string                 `json:"format,omitempty"`
	Level    uint64                 `json:"level,omitempty"`
	Email    string                 `json:"email,omitempty"`
	Name     string                 `json:"name,omitempty"`
	Type     string                 `json:"type,omitempty"`
	Metadata map[string]interface{} `json:"metadata,omitempty"`
}

type Profile added in v0.24.0

type Profile struct {
	ID       string                 `json:"id,omitempty"`
	GroupID  string                 `json:"group_id,omitempty"`
	Name     string                 `json:"name,omitempty"`
	Config   map[string]interface{} `json:"config,omitempty"`
	Metadata map[string]interface{} `json:"metadata,omitempty"`
}

Profile represents mainflux profile.

type ProfilesPage added in v0.24.0

type ProfilesPage struct {
	Profiles []Profile `json:"profiles"`
	// contains filtered or unexported fields
}

ProfilesPage contains list of profiles in a page with proper metadata.

type SDK

type SDK interface {
	// CreateUser creates mainflux user.
	CreateUser(token string, user User) (string, error)

	// User returns user object by id.
	User(token, id string) (User, error)

	// Users returns list of users.
	Users(token string, pm PageMetadata) (UsersPage, error)

	// CreateToken receives credentials and returns user token.
	CreateToken(user User) (string, error)

	// RegisterUser registers mainflux user.
	RegisterUser(user User) (string, error)

	// UpdateUser updates existing user.
	UpdateUser(user User, token string) error

	// UpdatePassword updates user password.
	UpdatePassword(oldPass, newPass, token string) error

	// CreateThing registers new thing and returns its id.
	CreateThing(thing Thing, groupID, token string) (string, error)

	// CreateThings registers new things and returns their ids.
	CreateThings(things []Thing, groupID, token string) ([]Thing, error)

	// Things returns page of things.
	Things(token string, pm PageMetadata) (ThingsPage, error)

	// ThingsByProfile returns page of things assigned to the specified profile.
	ThingsByProfile(profileID, token string, offset, limit uint64) (ThingsPage, error)

	// Thing returns thing object by id.
	Thing(id, token string) (Thing, error)

	// MetadataByKey retrieves metadata about the thing identified by the given key.
	MetadataByKey(thingKey string) (Metadata, error)

	// UpdateThing updates existing thing.
	UpdateThing(thing Thing, thingID, token string) error

	// DeleteThing removes existing thing.
	DeleteThing(id, token string) error

	// DeleteThings removes existing things.
	DeleteThings(ids []string, token string) error

	// IdentifyThing validates thing's key and returns its ID
	IdentifyThing(key string) (string, error)

	// CreateGroup creates new group and returns its id.
	CreateGroup(group Group, orgID, token string) (string, error)

	// DeleteGroup deletes users group.
	DeleteGroup(id, token string) error

	// DeleteGroups delete users groups.
	DeleteGroups(ids []string, token string) error

	// Groups returns page of groups.
	Groups(meta PageMetadata, token string) (GroupsPage, error)

	// Group returns users group object by id.
	Group(id, token string) (Group, error)

	// ListThingsByGroup lists things that are members of specified group.
	ListThingsByGroup(groupID, token string, offset, limit uint64) (ThingsPage, error)

	// ViewGroupByThing retrieves a group that the specified thing is a member of.
	ViewGroupByThing(thingID, token string) (Group, error)

	// UpdateGroup updates existing group.
	UpdateGroup(group Group, groupID, token string) error

	// CreateProfile creates new profile and returns its id.
	CreateProfile(profile Profile, groupID, token string) (string, error)

	// CreateProfiles registers new profiles and returns their ids.
	CreateProfiles(profiles []Profile, groupID, token string) ([]Profile, error)

	// Profiles returns page of profiles.
	Profiles(token string, pm PageMetadata) (ProfilesPage, error)

	// ViewProfileByThing returns profile that are assigned to specified thing.
	ViewProfileByThing(thingID, token string) (Profile, error)

	// Profile returns profile data by id.
	Profile(id, token string) (Profile, error)

	// UpdateProfile updates existing profile.
	UpdateProfile(profile Profile, profileID, token string) error

	// DeleteProfile removes existing profile.
	DeleteProfile(id, token string) error

	// DeleteProfiles removes existing profile.
	DeleteProfiles(ids []string, token string) error

	// ListProfilesByGroup lists profiles that are members of specified group.
	ListProfilesByGroup(groupID, token string, offset, limit uint64) (ProfilesPage, error)

	// ViewGroupByProfile retrieves a group that the specified profile is a member of.
	ViewGroupByProfile(profileID, token string) (Group, error)

	// CreateRolesByGroup creates new roles by group.
	CreateRolesByGroup(roles []GroupMember, groupID, token string) error

	// UpdateRolesByGroup updates existing group roles.
	UpdateRolesByGroup(roles []GroupMember, groupID, token string) error

	// RemoveRolesByGroup removes existing group roles.
	RemoveRolesByGroup(ids []string, groupID, token string) error

	// ListRolesByGroup lists roles that are specified for a certain group.
	ListRolesByGroup(groupID, token string, offset, limit uint64) (GroupMembersPage, error)

	// CreateOrg registers new org.
	CreateOrg(org Org, token string) error

	// Org returns org data by id.
	Org(id, token string) (Org, error)

	// UpdateOrg updates existing org.
	UpdateOrg(o Org, orgID, token string) error

	// DeleteOrg removes existing org.
	DeleteOrg(id, token string) error

	// Orgs returns page of orgs.
	Orgs(meta PageMetadata, token string) (OrgsPage, error)

	// ViewMember retrieves a member belonging to the specified org.
	ViewMember(orgID, memberID, token string) (Member, error)

	// AssignMembers assigns a members to the specified org.
	AssignMembers(om []OrgMember, orgID, token string) error

	// UnassignMembers unassigns a members from the specified org.
	UnassignMembers(token, orgID string, memberIDs ...string) error

	// UpdateMembers updates existing member.
	UpdateMembers(members []OrgMember, orgID, token string) error

	// ListMembersByOrg lists members who belong to a specified org.
	ListMembersByOrg(orgID, token string, offset, limit uint64) (MembersPage, error)

	// ListOrgsByMember lists orgs to which the specified member belongs.
	ListOrgsByMember(memberID, token string, offset, limit uint64) (OrgsPage, error)

	// CreateWebhooks creates new webhooks.
	CreateWebhooks(whs []Webhook, groupID, token string) ([]Webhook, error)

	// ListWebhooksByGroup lists webhooks who belong to a specified group.
	ListWebhooksByGroup(groupID, token string) (Webhooks, error)

	// Webhook returns webhook data by id.
	Webhook(webhookID, token string) (Webhook, error)

	// UpdateWebhook updates existing webhook.
	UpdateWebhook(wh Webhook, webhookID, token string) error

	// DeleteWebhooks removes existing webhooks.
	DeleteWebhooks(ids []string, token string) error

	// SendMessage send message.
	SendMessage(subtopic, msg, token string) error

	// ReadMessages read messages.
	ReadMessages(pm PageMetadata, token string) (map[string]interface{}, error)

	// SetContentType sets message content type.
	SetContentType(ct ContentType) error

	// Health returns things service health check.
	Health() (mainflux.HealthInfo, error)

	// IssueCert issues a certificate for a thing required for mtls.
	IssueCert(thingID string, keyBits int, keyType, valid, token string) (Cert, error)

	// RemoveCert removes a certificate
	RemoveCert(id, token string) error

	// RevokeCert revokes certificate with certID for thing with thingID
	RevokeCert(thingID, certID, token string) error

	// Issue issues a new key, returning its token value alongside.
	Issue(token string, duration time.Duration) (KeyRes, error)

	// Revoke removes the key with the provided ID that is issued by the user identified by the provided key.
	Revoke(token, id string) error

	// RetrieveKey retrieves data for the key identified by the provided ID, that is issued by the user identified by the provided key.
	RetrieveKey(token, id string) (retrieveKeyRes, error)
}

SDK contains Mainflux API.

func NewSDK

func NewSDK(conf Config) SDK

NewSDK returns new mainflux SDK instance.

type Thing

type Thing struct {
	ID        string                 `json:"id,omitempty"`
	GroupID   string                 `json:"group_id,omitempty"`
	ProfileID string                 `json:"profile_id,omitempty"`
	Name      string                 `json:"name,omitempty"`
	Key       string                 `json:"key,omitempty"`
	Metadata  map[string]interface{} `json:"metadata,omitempty"`
}

Thing represents mainflux thing.

type ThingsPage

type ThingsPage struct {
	Things []Thing `json:"things"`
	// contains filtered or unexported fields
}

ThingsPage contains list of things in a page with proper metadata.

type User

type User struct {
	ID       string                 `json:"id,omitempty"`
	Email    string                 `json:"email,omitempty"`
	Groups   []string               `json:"groups,omitempty"`
	Password string                 `json:"password,omitempty"`
	Metadata map[string]interface{} `json:"metadata,omitempty"`
}

User represents mainflux user its credentials.

type UserPasswordReq

type UserPasswordReq struct {
	OldPassword string `json:"old_password,omitempty"`
	Password    string `json:"password,omitempty"`
}

UserPasswordReq contains old and new passwords

type UsersPage

type UsersPage struct {
	Users []User `json:"users"`
	// contains filtered or unexported fields
}

UsersPage contains list of users in a page with proper metadata.

type Webhook

type Webhook struct {
	ID      string            `json:"id"`
	GroupID string            `json:"group_id"`
	Name    string            `json:"name"`
	Url     string            `json:"url"`
	Headers map[string]string `json:"headers"`
}

Webhook represents mainflux Webhook.

type Webhooks

type Webhooks struct {
	Webhooks []Webhook `json:"webhooks"`
}

Jump to

Keyboard shortcuts

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