format
This commit is contained in:
+27
-27
@@ -59,10 +59,10 @@ type SocketTicketResponse struct {
|
|||||||
// SessionResponse repesents the response from the session endpoint
|
// SessionResponse repesents the response from the session endpoint
|
||||||
type SessionResponse struct {
|
type SessionResponse struct {
|
||||||
Profile struct {
|
Profile struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
FirstName string `json:"first_name"`
|
FirstName string `json:"first_name"`
|
||||||
LastName string `json:"last_name"`
|
LastName string `json:"last_name"`
|
||||||
} `json:"profile"`
|
} `json:"profile"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,9 +82,9 @@ type RingRestClient struct {
|
|||||||
session *SessionResponse
|
session *SessionResponse
|
||||||
sessionExpiry time.Time
|
sessionExpiry time.Time
|
||||||
sessionMutex sync.Mutex
|
sessionMutex sync.Mutex
|
||||||
|
|
||||||
// Cache-Schlüssel für diese Instanz
|
// Cache-Schlüssel für diese Instanz
|
||||||
cacheKey string
|
cacheKey string
|
||||||
}
|
}
|
||||||
|
|
||||||
// CameraKind represents the different types of Ring cameras
|
// CameraKind represents the different types of Ring cameras
|
||||||
@@ -92,11 +92,11 @@ type CameraKind string
|
|||||||
|
|
||||||
// CameraData contains common fields for all camera types
|
// CameraData contains common fields for all camera types
|
||||||
type CameraData struct {
|
type CameraData struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
DeviceID string `json:"device_id"`
|
DeviceID string `json:"device_id"`
|
||||||
Kind string `json:"kind"`
|
Kind string `json:"kind"`
|
||||||
LocationID string `json:"location_id"`
|
LocationID string `json:"location_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// RingDeviceType represents different types of Ring devices
|
// RingDeviceType represents different types of Ring devices
|
||||||
@@ -167,7 +167,7 @@ const (
|
|||||||
// NewRingRestClient creates a new Ring client instance with caching
|
// NewRingRestClient creates a new Ring client instance with caching
|
||||||
func NewRingRestClient(auth interface{}, onTokenRefresh func(string)) (*RingRestClient, error) {
|
func NewRingRestClient(auth interface{}, onTokenRefresh func(string)) (*RingRestClient, error) {
|
||||||
var cacheKey string
|
var cacheKey string
|
||||||
|
|
||||||
// Create cache key based on auth data
|
// Create cache key based on auth data
|
||||||
switch a := auth.(type) {
|
switch a := auth.(type) {
|
||||||
case RefreshTokenAuth:
|
case RefreshTokenAuth:
|
||||||
@@ -183,10 +183,10 @@ func NewRingRestClient(auth interface{}, onTokenRefresh func(string)) (*RingRest
|
|||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("invalid auth type")
|
return nil, fmt.Errorf("invalid auth type")
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheMutex.Lock()
|
cacheMutex.Lock()
|
||||||
defer cacheMutex.Unlock()
|
defer cacheMutex.Unlock()
|
||||||
|
|
||||||
if cachedClient, ok := clientCache[cacheKey]; ok {
|
if cachedClient, ok := clientCache[cacheKey]; ok {
|
||||||
// Check if token is not nil and not expired
|
// Check if token is not nil and not expired
|
||||||
if cachedClient.authToken != nil && time.Now().Before(cachedClient.tokenExpiry) {
|
if cachedClient.authToken != nil && time.Now().Before(cachedClient.tokenExpiry) {
|
||||||
@@ -194,7 +194,7 @@ func NewRingRestClient(auth interface{}, onTokenRefresh func(string)) (*RingRest
|
|||||||
return cachedClient, nil
|
return cachedClient, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
client := &RingRestClient{
|
client := &RingRestClient{
|
||||||
httpClient: &http.Client{Timeout: defaultTimeout},
|
httpClient: &http.Client{Timeout: defaultTimeout},
|
||||||
onTokenRefresh: onTokenRefresh,
|
onTokenRefresh: onTokenRefresh,
|
||||||
@@ -216,7 +216,7 @@ func NewRingRestClient(auth interface{}, onTokenRefresh func(string)) (*RingRest
|
|||||||
}
|
}
|
||||||
|
|
||||||
clientCache[cacheKey] = client
|
clientCache[cacheKey] = client
|
||||||
|
|
||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -276,21 +276,21 @@ func (c *RingRestClient) Request(method, url string, body interface{}) ([]byte,
|
|||||||
c.authToken = nil
|
c.authToken = nil
|
||||||
c.tokenExpiry = time.Time{} // Reset token expiry
|
c.tokenExpiry = time.Time{} // Reset token expiry
|
||||||
c.authMutex.Unlock()
|
c.authMutex.Unlock()
|
||||||
|
|
||||||
if attempt == maxRetries {
|
if attempt == maxRetries {
|
||||||
return nil, fmt.Errorf("authentication failed after %d retries", maxRetries)
|
return nil, fmt.Errorf("authentication failed after %d retries", maxRetries)
|
||||||
}
|
}
|
||||||
|
|
||||||
// By 401 with Auth AND Session start over
|
// By 401 with Auth AND Session start over
|
||||||
c.sessionMutex.Lock()
|
c.sessionMutex.Lock()
|
||||||
c.session = nil
|
c.session = nil
|
||||||
c.sessionExpiry = time.Time{} // Reset session expiry
|
c.sessionExpiry = time.Time{} // Reset session expiry
|
||||||
c.sessionMutex.Unlock()
|
c.sessionMutex.Unlock()
|
||||||
|
|
||||||
if err := c.ensureSession(); err != nil {
|
if err := c.ensureSession(); err != nil {
|
||||||
return nil, fmt.Errorf("failed to refresh session: %w", err)
|
return nil, fmt.Errorf("failed to refresh session: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Authorization", "Bearer "+c.authToken.AccessToken)
|
req.Header.Set("Authorization", "Bearer "+c.authToken.AccessToken)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -305,15 +305,15 @@ func (c *RingRestClient) Request(method, url string, body interface{}) ([]byte,
|
|||||||
c.session = nil
|
c.session = nil
|
||||||
c.sessionExpiry = time.Time{} // Reset session expiry
|
c.sessionExpiry = time.Time{} // Reset session expiry
|
||||||
c.sessionMutex.Unlock()
|
c.sessionMutex.Unlock()
|
||||||
|
|
||||||
if attempt == maxRetries {
|
if attempt == maxRetries {
|
||||||
return nil, fmt.Errorf("session refresh failed after %d retries", maxRetries)
|
return nil, fmt.Errorf("session refresh failed after %d retries", maxRetries)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.ensureSession(); err != nil {
|
if err := c.ensureSession(); err != nil {
|
||||||
return nil, fmt.Errorf("failed to refresh session: %w", err)
|
return nil, fmt.Errorf("failed to refresh session: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -461,7 +461,7 @@ func (c *RingRestClient) ensureAuth() error {
|
|||||||
RT: authResp.RefreshToken,
|
RT: authResp.RefreshToken,
|
||||||
HID: c.hardwareID,
|
HID: c.hardwareID,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set token expiry (1 minute before actual expiry)
|
// Set token expiry (1 minute before actual expiry)
|
||||||
expiresIn := time.Duration(authResp.ExpiresIn-60) * time.Second
|
expiresIn := time.Duration(authResp.ExpiresIn-60) * time.Second
|
||||||
c.tokenExpiry = time.Now().Add(expiresIn)
|
c.tokenExpiry = time.Now().Add(expiresIn)
|
||||||
@@ -471,7 +471,7 @@ func (c *RingRestClient) ensureAuth() error {
|
|||||||
newRefreshToken := encodeAuthConfig(c.authConfig)
|
newRefreshToken := encodeAuthConfig(c.authConfig)
|
||||||
c.onTokenRefresh(newRefreshToken)
|
c.onTokenRefresh(newRefreshToken)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Refreshn the token in the client
|
// Refreshn the token in the client
|
||||||
c.RefreshToken = encodeAuthConfig(c.authConfig)
|
c.RefreshToken = encodeAuthConfig(c.authConfig)
|
||||||
|
|
||||||
@@ -585,7 +585,7 @@ func (c *RingRestClient) GetAuth(twoFactorAuthCode string) (*AuthTokenResponse,
|
|||||||
if c.onTokenRefresh != nil {
|
if c.onTokenRefresh != nil {
|
||||||
c.onTokenRefresh(c.RefreshToken)
|
c.onTokenRefresh(c.RefreshToken)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Refresh the cached client
|
// Refresh the cached client
|
||||||
cacheMutex.Lock()
|
cacheMutex.Lock()
|
||||||
clientCache[c.cacheKey] = c
|
clientCache[c.cacheKey] = c
|
||||||
@@ -718,4 +718,4 @@ func interfaceSlice(slice interface{}) []CameraData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
type SnapshotProducer struct {
|
type SnapshotProducer struct {
|
||||||
core.Connection
|
core.Connection
|
||||||
|
|
||||||
client *RingRestClient
|
client *RingRestClient
|
||||||
cameraID string
|
cameraID string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,7 +35,7 @@ func NewSnapshotProducer(client *RingRestClient, cameraID string) *SnapshotProdu
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
client: client,
|
client: client,
|
||||||
cameraID: cameraID,
|
cameraID: cameraID,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user