Skip to content

PluginApi

The PluginApi is the root interface of Flare Hotspot SDK. It provides access to methods used to manipulate system accounts, network devices, theme configuration, user sessions, payment system and more. Each plugin is provided with an instance of PluginApi.

When the plugin is first loaded into the system, the system looks for the Init function of the plugin's main package. The PluginApi object is then passed to the plugin's Init function. From here, you can start configuring the routes and components of your plugin. An example of a plugin's init function:

plugins/com.mydomain.myplugin/main.go
package main

import (
    sdkplugin "github.com/flarehotspot/sdk/api/plugin"
)

func main() {}

func Init(api sdkplugin.PluginApi) {
    // You can start using the SDK here.
    // You can configure your routes, define your plugin components
    // and register items in the portal and admin nav menu, and more.
}

PluginApi Methods

The following are the available methods in PluginApi.

Pkg

It returns the package field defined in plugin.json.

pkg := api.Pkg()
fmt.Println(pkg) // "com.mydomain.myplugin"

Name

It returns the name field defined in plugin.json.

name := api.Name()
fmt.Println(name) // "My Plugin"

Version

It returns the version field defined in plugin.json.

version := api.Version()
fmt.Println(version) // "1.0.0"

Description

It returns the description field defined in plugin.json.

description := api.Description()
fmt.Println(description) // "My plugin description"

Dir

It returns the absolute path of the plugin's installtion directory.

dir := api.Dir()
fmt.Println(dir) // "/path/to/com.mydomain.myplugin"

Translate

It is a utility function used to convert a message key into a translated string. Example usage:

msg := api.Translate("info", "payment_received", "amount", 1.00)
fmt.Println(msg) // "Payment received USD 1.0.0"

In this example, given that the application language is set to en, the system will look for the file resources/translations/en/info/payment_received.txt inside your plugin directory. If the file is found, the system will use the contents of the file as the translation template.

Sometimes we want to put variables inside the translation message. In this example, we want to pass the amount as a paramenter to the message. We can do that by passing the amount param as key-value pairs to the Translate method. Internally, the param pairs are converted into a type map[any]any. To use the amount param in the translation file, we'll enclose it with <% and %> delimiters (with dot prefix). Therefore the content of payment_received.txt should be:

Payment received: USD <% .amount %>

Resource

It returns the absolute path of the file under the plugin's resource directory.

resource := api.Resource("/my-resource.txt")
fmt.Println(resource) // "/path/to/com.mydomain.myplugin/resources/my-resource.txt"

SqlDb

It returns *sql.DB instance which is used to query, insert, update and delete database entities.

db := api.SqlDb()
fmt.Println(db) // *sql.DB

Acct

It returns the AccountsApi object which is used to access and modify the system admin accounts.

acct := api.Acct()
fmt.Println(acct) // AccountsApi

Http

It returns the HttpApi object which is used to configure routes and serve HTTP requests.

http := api.Http()
fmt.Println(http) // HttpApi

Config

It returns the ConfigApi object which is used to access and modify the system configuration.

config := api.Config()
fmt.Println(config) // ConfigApi

Payments

It return the PaymentsApi object which is used to create payment options or create system transactions.

payments := api.Payments()
fmt.Println(payments) // PaymentsApi

InAppPurchases

It returns the InAppPurchasesApi object which is used to create and manage in-app purchases.

inAppPurchases := api.InAppPurchases()
fmt.Println(inAppPurchases) // InAppPurchasesApi

Ads

It returns the AdsApi object which is used to create and manage ads.

ads := api.Ads()
fmt.Println(ads) // AdsApi

PluginsMgr

It returns the PluginsMgrApi object which is used to manage plugins.

pluginsMgr := api.PluginsMgr()
fmt.Println(pluginsMgr) // PluginsMgrApi

Network

It returns the NetworkApi object which is used to manage the network.

network := api.Network()
fmt.Println(network) // NetworkApi

DeviceHooks

It returns the DeviceHooksApi object which is used to manage device registration hooks.

deviceHooks := api.DeviceHooks()
fmt.Println(deviceHooks) // DeviceHooksApi

SessionsMgr

It returns the SessionsMgrApi object which is used to manage user sessions.

sessionsMgr := api.SessionsMgr()
fmt.Println(sessionsMgr) // SessionsMgrApi

Uci

It returns the UciApi object which is a wrapper to OpenWRT's UCI.

uci := api.Uci()
fmt.Println(uci) // UciApi

Themes

It returns the ThemesApi object which is used to manage system UI themes.

themes := api.Themes()
fmt.Println(themes) // ThemesApi

Features

Returns the implement features of the plugin.

features := api.Features()
fmt.Println(features) // []string{"theme:admin", "theme:portal"}

Below are the available features and their descriptions:

Feature Description
theme:admin Plugin provides an admin theme
theme:portal Plugin provides a portal theme