Documentation ¶
Overview ¶
Package unifi is a Golang SDK for UBNT APIs to interact with Unifi Controller.
Example ¶
package main import ( "context" "log" "time" "github.com/northbright/unifi" ) func main() { var err error unifiURL := "https://192.168.1.56:8443" userName := "admin" password := "admin" defer func() { if err != nil { log.Printf("error: %v", err) } }() // New an Unifi instance. u, err := unifi.New(unifiURL, userName, password) if err != nil { return } // Set debug mode to true to output debug messages(default is false). unifi.SetDebugMode(true) // Set timeout to 5 seconds. timeout := time.Duration(time.Second * 5) // Create context with timeout. ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() exit := make(chan error) go func() { // Login if err = u.Login(ctx); err != nil { exit <- err return } // Logout before return if login successfully. defer func() { exit <- u.Logout(ctx) }() mac := "aa:bb:cc:dd:ee:ff" // MAC address. min := 5 // time to authorize in minutes. // Authorize guest with MAC and time. if err = u.AuthorizeGuest(ctx, "default", mac, min); err != nil { exit <- err return } down := 2048 // download speed in KB. up := 512 // upload speed in KB. quota := 20 // Quota limit in MB. // Authorize guest with MAC, time, download speed, upload speed and quota. if err = u.AuthorizeGuestWithQos(ctx, "default", mac, min, down, up, quota); err != nil { exit <- err return } // Unauthorize guest with MAC. /*if err = u.UnAuthorizeGuest(ctx, "default", mac); err != nil { exit <- err return }*/ exit <- nil }() select { case err = <-exit: log.Printf("goroutine exited") return case <-ctx.Done(): err = ctx.Err() return } }
Output:
Index ¶
- func IsDebugMode() bool
- func ParseJSON(b []byte) (map[string]interface{}, bool, error)
- func SetDebugMode(f bool)
- type Unifi
- func (u *Unifi) AuthorizeGuest(ctx context.Context, site, mac string, min int) error
- func (u *Unifi) AuthorizeGuestWithQos(ctx context.Context, site, mac string, min, down, up, quota int) error
- func (u *Unifi) Do(r *http.Request, readRespBody bool) ([]byte, error)
- func (u *Unifi) Login(ctx context.Context) error
- func (u *Unifi) Logout(ctx context.Context) error
- func (u *Unifi) UnAuthorizeGuest(ctx context.Context, site, mac string) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Unifi ¶
type Unifi struct {
// contains filtered or unexported fields
}
Unifi provides functions to call Unifi APIs.
func New ¶
New creates a new Unifi.
Params:
unifiURL: Unifi Controller's URL. E.g. https://10.0.1.100:8443 userName: User name of Unifi Controller. password: Password of Unifi Controller.
func (*Unifi) AuthorizeGuest ¶
AuthorizeGuest authorizes guest by MAC, time.It's a wrapper of AuthorizeGuestWithQos.
Params:
ctx: Parent context. You may use context.Background() to create an empty context. See http://godoc.org/context for more info. site: Site name. It's **NOT** the "Site Name"(just description) in Unifi GUI. If you only have 1 site. Just use "default" or leave it empty. If you've created new sites, follow this to get the site name: https://github.com/northbright/Notes/blob/master/Software/unifi/use-compass-to-explore-mongodb-of-unifi/use-compass-to-explore-mongodb-of-unifi.md mac: MAC address of guest to be authorized. It's in "aa:bb:cc:dd:ee:ff" format. min: Timeout in minutes.
func (*Unifi) AuthorizeGuestWithQos ¶
func (u *Unifi) AuthorizeGuestWithQos(ctx context.Context, site, mac string, min, down, up, quota int) error
AuthorizeGuestWithQos authorizes guest by MAC, time and set qos.
Params:
ctx: Parent context. You may use context.Background() to create an empty context. See http://godoc.org/context for more info. site: Site name. It's **NOT** the "Site Name"(just description) in Unifi GUI. If you only have 1 site. Just use "default" or leave it empty. If you've created new sites, follow this to get the site name: https://github.com/northbright/Notes/blob/master/Software/unifi/use-compass-to-explore-mongodb-of-unifi/use-compass-to-explore-mongodb-of-unifi.md mac: MAC address of guest to be authorized. It's in "aa:bb:cc:dd:ee:ff" format. min: Timeout in minutes. down: Max download speed in KB. up: Max upload speed in KB. quota: Quota in MB.
func (*Unifi) Do ¶
Do does the HTTP request for Unifi API.
Params:
r: *http.Request. readRespBody: if read response body or not. it will return the response body if it's true or return nil if it's false.
Return:
response body if readRespBody is true.
func (*Unifi) Login ¶
Login logins Unifi Controller.
Params:
ctx: Parent context. You may use context.Background() to create an empty context. See http://godoc.org/context for more info.
func (*Unifi) Logout ¶
Logout logouts Unifi Controller.
Params:
ctx: Parent context. You may use context.Background() to create an empty context. See http://godoc.org/context for more info.
func (*Unifi) UnAuthorizeGuest ¶
UnAuthorizeGuest unauthorizes guest by MAC.
Params:
ctx: Parent context. You may use context.Background() to create an empty context. See http://godoc.org/context for more info. site: Site name. It's **NOT** the "Site Name"(just description) in Unifi GUI. If you only have 1 site. Just use "default" or leave it empty. If you've created new sites, follow this to get the site name: https://github.com/northbright/Notes/blob/master/Software/unifi/use-compass-to-explore-mongodb-of-unifi/use-compass-to-explore-mongodb-of-unifi.md mac: MAC address of guest to be unauthorized. It's in "aa:bb:cc:dd:ee:ff" format.
Click to show internal directories.
Click to hide internal directories.