* feat: add support for logging * fix linting * chore: remove old APIError in favor of ServerError
3.5 KiB
3.5 KiB
Migrating from paultyng/go-unifi
This guide will help you migrate from paultyng/go-unifi to filipowm/go-unifi. The main differences are in how the client is initialized, while all client methods remain the same, with additional methods covering of the UniFi Controller API.
Client Initialization Changes
paultyng/go-unifi Style
In the upstream library, client initialization typically looks like this:
client := unifi.Client{}
client.SetBaseURL("https://unifi.localdomain")
// Optional: Configure TLS
if skipTLSVerify {
httpClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
jar, _ := cookiejar.New(nil)
httpClient.Jar = jar
client.SetHTTPClient(httpClient)
}
// Login is required
err := client.Login(ctx, username, password)
if err != nil {
return nil, err
}
filipowm/go-unifi Style
The new library provides a more structured and configurable approach:
// Using API Key (recommended, requires UniFi Controller 9.0.108+)
client, err := unifi.NewClient(&unifi.ClientConfig{
BaseURL: "https://unifi.localdomain",
APIKey: "your-api-key",
})
// OR using username/password
client, err := unifi.NewClient(&unifi.ClientConfig{
BaseURL: "https://unifi.localdomain",
Username: "your-username",
Password: "your-password",
})
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
Key Differences
-
Client Creation:
- Old: Manual client creation and configuration using setters. Client used is a struct.
- New: Builder pattern with
NewClientfunction andClientConfigstruct. Client used is an interface.
-
Authentication:
- Old: Only username/password authentication with explicit
Login()call - New: Supports both API Key (recommended) and username/password authentication
- New: Login is handled automatically during client creation
- Old: Only username/password authentication with explicit
-
HTTP Client Configuration:
- Old: Manual HTTP client configuration with
SetHTTPClientand manual creation ofhttp.Clientandcookiejar.Jar - New: Built-in configuration options through
ClientConfig:HttpTransportCustomizerfor transport-level customizationHttpRoundTripperProviderfor complete HTTP client control
- Old: Manual HTTP client configuration with
-
Removed unifi.APIError:
- Old:
unifi.APIErrorstruct for API errors - New: Standard
unifi.ServerErrorstruct for API errors
- Old:
-
Additional Features in filipowm/go-unifi:
- Validation modes (Soft, Hard, Disabled)
- Request/Response interceptors
- Custom error handling
- Comprehensive configuration options
Migration Steps
- Replace the import from
github.com/paultyng/go-unifitogithub.com/filipowm/go-unifi - Replace manual client creation with
NewClientand appropriateClientConfig - If using TLS skip verification, use the
VerifySSLoption inClientConfig:client, err := unifi.NewClient(&unifi.ClientConfig{ BaseURL: "https://unifi.localdomain", APIKey: "your-api-key", VerifySSL: false, }) - Remove explicit
Login()calls as they are now handled automatically, unless you use bare client initialization - Replace usage of
unifi.APIErrorwithunifi.ServerError
The rest of your code using the client methods should continue to work as before, as the API methods remain the same.
For details on configuring client, check client configuration.