feat: use Client interface instead of client struct when interacting with UniFi SDK (#21)

* feat: use Client interface instead of client struct when interacting with UniFi SDK

Breaking change!

* chore: linting

* chore: linting
This commit is contained in:
Mateusz Filipowicz
2025-02-17 09:38:41 +01:00
committed by GitHub
parent fadc5ada8b
commit dca894e8e7
104 changed files with 1048 additions and 419 deletions
+5 -5
View File
@@ -65,7 +65,7 @@ func (dst *{{ $k }}) UnmarshalJSON(b []byte) error {
{{ end }}
{{ if not .IsSetting }}
func (c *Client) list{{ .StructName }}(ctx context.Context, site string) ([]{{ .StructName }}, error) {
func (c *client) list{{ .StructName }}(ctx context.Context, site string) ([]{{ .StructName }}, error) {
var respBody struct {
Meta Meta `json:"meta"`
Data []{{ .StructName }} `json:"data"`
@@ -80,7 +80,7 @@ func (c *Client) list{{ .StructName }}(ctx context.Context, site string) ([]{{ .
}
{{- end }}
func (c *Client) get{{ .StructName }}(ctx context.Context, site{{ if not .IsSetting }}, id{{ end }} string) (*{{ .StructName }}, error) {
func (c *client) get{{ .StructName }}(ctx context.Context, site{{ if not .IsSetting }}, id{{ end }} string) (*{{ .StructName }}, error) {
var respBody struct {
Meta Meta `json:"meta"`
Data []{{ .StructName }} `json:"data"`
@@ -103,7 +103,7 @@ func (c *Client) get{{ .StructName }}(ctx context.Context, site{{ if not .IsSett
}
{{ if not .IsSetting }}
func (c *Client) delete{{ .StructName }}(ctx context.Context, site, id string) error {
func (c *client) delete{{ .StructName }}(ctx context.Context, site, id string) error {
err := c.Delete(ctx, fmt.Sprintf("s/%s/rest/{{ .ResourcePath }}/%s", site, id), struct{}{}, nil)
if err != nil {
return err
@@ -111,7 +111,7 @@ func (c *Client) delete{{ .StructName }}(ctx context.Context, site, id string) e
return nil
}
func (c *Client) create{{ .StructName }}(ctx context.Context, site string, d *{{ .StructName }}) (*{{ .StructName }}, error) {
func (c *client) create{{ .StructName }}(ctx context.Context, site string, d *{{ .StructName }}) (*{{ .StructName }}, error) {
var respBody struct {
Meta Meta `json:"meta"`
Data []{{ .StructName }} `json:"data"`
@@ -132,7 +132,7 @@ func (c *Client) create{{ .StructName }}(ctx context.Context, site string, d *{{
}
{{- end }}
func (c *Client) update{{ .StructName }}(ctx context.Context, site string, d *{{ .StructName }}) (*{{ .StructName }}, error) {
func (c *client) update{{ .StructName }}(ctx context.Context, site string, d *{{ .StructName }}) (*{{ .StructName }}, error) {
var respBody struct {
Meta Meta `json:"meta"`
Data []{{ .StructName }} `json:"data"`
+4 -3
View File
@@ -143,17 +143,18 @@ func singlePointerParam(name string) []FunctionParam {
}
func (c *ClientInfoBuilder) AddResource(r *Resource) *ClientInfoBuilder {
c.AddFunction(&Comment{comment: fmt.Sprintf("client methods for %s resource", r.Name()), resourceName: r.Name()})
c.AddFunction(&Comment{comment: fmt.Sprintf("==== client methods for %s resource ====", r.Name()), resourceName: r.Name()})
if r.IsSetting() {
c.addResourceFunction("Get", r.Name(), "retrieves the settings for a resource", nil, singlePointerReturn(r.Name()))
c.addResourceFunction("Update", r.Name(), "updates a resource", singlePointerParam(r.Name()), singlePointerReturn(r.Name()))
return c
}
c.addResourceFunction("Get", r.Name(), "retrieves a resource", []FunctionParam{{"id", "string"}}, singlePointerReturn(r.Name()))
c.addResourceFunction("List", r.Name(), "lists the resources", nil, []string{"[]*" + r.Name()})
c.addResourceFunction("List", r.Name(), "lists the resources", nil, []string{"[]" + r.Name()})
c.addResourceFunction("Create", r.Name(), "creates a resource", singlePointerParam(r.Name()), singlePointerReturn(r.Name()))
c.addResourceFunction("Update", r.Name(), "updates a resource", singlePointerParam(r.Name()), singlePointerReturn(r.Name()))
c.addResourceFunction("Delete", r.Name(), "deletes a resource", []FunctionParam{{"id", "string"}}, nil)
c.AddFunction(&Comment{comment: fmt.Sprintf("==== end of client methods for %s resource ====", r.Name()), resourceName: r.Name() + "_end"})
return c
}
@@ -198,5 +199,5 @@ func (c *ClientInfo) GenerateCode() (string, error) {
// Name returns the name of the client.
func (c *ClientInfo) Name() string {
return "client"
return "Client"
}
+266
View File
@@ -2,7 +2,20 @@
customizations:
client:
excludeResources:
- "BroadcastGroup"
- "ChannelPlan"
- "Dashboard"
- "DHCPOption"
- "Dpi*"
- "HeatMap*"
- "Hotspot*"
- "Map"
- "MediaFile"
- "ScheduleTask"
- "Setting*" # Exclude all resources that start with "Setting"
- "SpatialRecord"
- "Tag"
- "VirtualDevice"
functions:
- name: "Login"
comment: "Login logs in to the controller. Useful only for user/password authentication."
@@ -16,6 +29,75 @@ customizations:
comment: "BaseURL returns the base URL of the controller."
returns:
- "string"
- name: "Do"
comment: "Do sends a request to the controller."
params:
- name: "ctx"
type: "context.Context"
- name: "method"
type: "string"
- name: "apiPath"
type: "string"
- name: "reqBody"
type: "interface{}"
- name: "respBody"
type: "interface{}"
returns:
- "error"
- name: "Get"
comment: "Get sends a GET request to the controller."
params:
- name: "ctx"
type: "context.Context"
- name: "apiPath"
type: "string"
- name: "reqBody"
type: "interface{}"
- name: "respBody"
type: "interface{}"
returns:
- "error"
- name: "Post"
comment: "Post sends a POST request to the controller."
params:
- name: "ctx"
type: "context.Context"
- name: "apiPath"
type: "string"
- name: "reqBody"
type: "interface{}"
- name: "respBody"
type: "interface{}"
returns:
- "error"
- name: "Put"
comment: "Put sends a PUT request to the controller."
params:
- name: "ctx"
type: "context.Context"
- name: "apiPath"
type: "string"
- name: "reqBody"
type: "interface{}"
- name: "respBody"
type: "interface{}"
returns:
- "error"
- name: "Delete"
comment: "Delete sends a DELETE request to the controller."
params:
- name: "ctx"
type: "context.Context"
- name: "apiPath"
type: "string"
- name: "reqBody"
type: "interface{}"
- name: "respBody"
type: "interface{}"
returns:
- "error"
- name: "AdoptDevice"
comment: "AdoptDevice adopts a device by MAC address."
resourceName: "Device"
@@ -28,6 +110,190 @@ customizations:
type: "string"
returns:
- "error"
- name: "ForgetDevice"
comment: "ForgetDevice forgets a device by MAC address."
resourceName: "Device"
params:
- name: "ctx"
type: "context.Context"
- name: "site"
type: "string"
- name: "mac"
type: "string"
returns:
- "error"
- name: "GetDeviceByMAC"
resourceName: "Device"
params:
- name: "ctx"
type: "context.Context"
- name: "site"
type: "string"
- name: "mac"
type: "string"
returns:
- "*Device"
- "error"
- name: "ReorderFirewallRules"
resourceName: "FirewallRule"
params:
- name: "ctx"
type: "context.Context"
- name: "site"
type: "string"
- name: "ruleset"
type: "string"
- name: "reorder"
type: "[]FirewallRuleIndexUpdate"
returns:
- "error"
- name: "GetSetting"
resourceName: "Setting"
params:
- name: "ctx"
type: "context.Context"
- name: "site"
type: "string"
- name: "key"
type: "string"
returns:
- "*Setting"
- "interface{}"
- "error"
- name: "ListSites"
resourceName: "Site"
params:
- name: "ctx"
type: "context.Context"
returns:
- "[]Site"
- "error"
- name: "GetSite"
resourceName: "Site"
params:
- name: "ctx"
type: "context.Context"
- name: "id"
type: "string"
returns:
- "*Site"
- "error"
- name: "CreateSite"
resourceName: "Site"
params:
- name: "ctx"
type: "context.Context"
- name: "description"
type: "string"
returns:
- "[]Site"
- "error"
- name: "DeleteSite"
resourceName: "Site"
params:
- name: "ctx"
type: "context.Context"
- name: "id"
type: "string"
returns:
- "[]Site"
- "error"
- name: "UpdateSite"
resourceName: "Site"
params:
- name: "ctx"
type: "context.Context"
- name: "name"
type: "string"
- name: "description"
type: "string"
returns:
- "[]Site"
- "error"
- name: "GetSystemInfo"
resourceName: "SysInfo"
params:
- name: "ctx"
type: "context.Context"
- name: "id"
type: "string"
returns:
- "*SysInfo"
- "error"
- name: "GetSystemInformation"
resourceName: "SysInfo"
returns:
- "*SysInfo"
- "error"
- name: "GetUserByMAC"
resourceName: "User"
params:
- name: "ctx"
type: "context.Context"
- name: "site"
type: "string"
- name: "mac"
type: "string"
returns:
- "*User"
- "error"
- name: "BlockUserByMAC"
resourceName: "User"
params:
- name: "ctx"
type: "context.Context"
- name: "site"
type: "string"
- name: "mac"
type: "string"
returns:
- "error"
- name: "UnblockUserByMAC"
resourceName: "User"
params:
- name: "ctx"
type: "context.Context"
- name: "site"
type: "string"
- name: "mac"
type: "string"
returns:
- "error"
- name: "DeleteUserByMAC"
resourceName: "User"
params:
- name: "ctx"
type: "context.Context"
- name: "site"
type: "string"
- name: "mac"
type: "string"
returns:
- "error"
- name: "KickUserByMAC"
resourceName: "User"
params:
- name: "ctx"
type: "context.Context"
- name: "site"
type: "string"
- name: "mac"
type: "string"
returns:
- "error"
- name: "OverrideUserFingerprint"
resourceName: "User"
params:
- name: "ctx"
type: "context.Context"
- name: "site"
type: "string"
- name: "mac"
type: "string"
- name: "devIdOverride"
type: "int"
returns:
- "error"
resources:
Account:
fields: