refactor: improve media service client methods and clean up test files

- Introduced helper methods `getMediaEndpoint` and `getMediaSoapClient` in the media client for better code reuse and clarity.
- Updated various media service methods to utilize the new helper methods, enhancing maintainability.
- Cleaned up test files by standardizing formatting and removing unnecessary blank lines for improved readability.
This commit is contained in:
0x524a
2025-12-02 01:22:06 -05:00
parent 202218e24e
commit 2ea36220f7
9 changed files with 177 additions and 176 deletions
+18 -16
View File
@@ -11,6 +11,20 @@ import (
// Media service namespace // Media service namespace
const mediaNamespace = "http://www.onvif.org/ver10/media/wsdl" const mediaNamespace = "http://www.onvif.org/ver10/media/wsdl"
// getMediaEndpoint returns the media endpoint, falling back to the default endpoint if not set.
func (c *Client) getMediaEndpoint() string {
if c.mediaEndpoint != "" {
return c.mediaEndpoint
}
return c.endpoint
}
// getMediaSoapClient creates a new SOAP client for media operations.
func (c *Client) getMediaSoapClient() *soap.Client {
username, password := c.GetCredentials()
return soap.NewClient(c.httpClient, username, password)
}
// GetProfiles retrieves all media profiles // GetProfiles retrieves all media profiles
func (c *Client) GetProfiles(ctx context.Context) ([]*Profile, error) { func (c *Client) GetProfiles(ctx context.Context) ([]*Profile, error) {
endpoint := c.mediaEndpoint endpoint := c.mediaEndpoint
@@ -2046,13 +2060,8 @@ func (c *Client) GetMetadataConfigurationOptions(ctx context.Context, configurat
} }
// GetAudioOutputConfiguration retrieves audio output configuration // GetAudioOutputConfiguration retrieves audio output configuration
//
//nolint:dupl // Similar structure to GetAudioSourceConfiguration but different types and operations
func (c *Client) GetAudioOutputConfiguration(ctx context.Context, configurationToken string) (*AudioOutputConfiguration, error) { func (c *Client) GetAudioOutputConfiguration(ctx context.Context, configurationToken string) (*AudioOutputConfiguration, error) {
endpoint := c.mediaEndpoint endpoint := c.getMediaEndpoint()
if endpoint == "" {
endpoint = c.endpoint
}
type GetAudioOutputConfiguration struct { type GetAudioOutputConfiguration struct {
XMLName xml.Name `xml:"trt:GetAudioOutputConfiguration"` XMLName xml.Name `xml:"trt:GetAudioOutputConfiguration"`
@@ -2076,9 +2085,7 @@ func (c *Client) GetAudioOutputConfiguration(ctx context.Context, configurationT
} }
var resp GetAudioOutputConfigurationResponse var resp GetAudioOutputConfigurationResponse
soapClient := c.getMediaSoapClient()
username, password := c.GetCredentials()
soapClient := soap.NewClient(c.httpClient, username, password)
if err := soapClient.Call(ctx, endpoint, "", req, &resp); err != nil { if err := soapClient.Call(ctx, endpoint, "", req, &resp); err != nil {
return nil, fmt.Errorf("GetAudioOutputConfiguration failed: %w", err) return nil, fmt.Errorf("GetAudioOutputConfiguration failed: %w", err)
@@ -2703,10 +2710,7 @@ func (c *Client) GetVideoSourceConfiguration(ctx context.Context, configurationT
// GetAudioSourceConfiguration retrieves a specific audio source configuration // GetAudioSourceConfiguration retrieves a specific audio source configuration
func (c *Client) GetAudioSourceConfiguration(ctx context.Context, configurationToken string) (*AudioSourceConfiguration, error) { func (c *Client) GetAudioSourceConfiguration(ctx context.Context, configurationToken string) (*AudioSourceConfiguration, error) {
endpoint := c.mediaEndpoint endpoint := c.getMediaEndpoint()
if endpoint == "" {
endpoint = c.endpoint
}
type GetAudioSourceConfiguration struct { type GetAudioSourceConfiguration struct {
XMLName xml.Name `xml:"trt:GetAudioSourceConfiguration"` XMLName xml.Name `xml:"trt:GetAudioSourceConfiguration"`
@@ -2730,9 +2734,7 @@ func (c *Client) GetAudioSourceConfiguration(ctx context.Context, configurationT
} }
var resp GetAudioSourceConfigurationResponse var resp GetAudioSourceConfigurationResponse
soapClient := c.getMediaSoapClient()
username, password := c.GetCredentials()
soapClient := soap.NewClient(c.httpClient, username, password)
if err := soapClient.Call(ctx, endpoint, "", req, &resp); err != nil { if err := soapClient.Call(ctx, endpoint, "", req, &resp); err != nil {
return nil, fmt.Errorf("GetAudioSourceConfiguration failed: %w", err) return nil, fmt.Errorf("GetAudioSourceConfiguration failed: %w", err)
-1
View File
@@ -1487,4 +1487,3 @@ func TestGetOSDOptions(t *testing.T) {
t.Errorf("Expected MaximumNumberOfOSDs 10, got %d", options.MaximumNumberOfOSDs) t.Errorf("Expected MaximumNumberOfOSDs 10, got %d", options.MaximumNumberOfOSDs)
} }
} }