Add MarkSeen Functionality to Notification System
- Implemented the MarkSeen method in the notification client to mark notifications as seen, enhancing user interaction with notifications. - Added MarkSeenRequest and MarkSeenResponse structs to facilitate the request and response handling for marking notifications. - Updated the NotificationService interface to include the new MarkSeen method, ensuring consistency across the service layer. - Enhanced the SDK with a QuickMarkSeen convenience method for easier usage in client applications. - Included logging for successful and failed attempts to mark notifications as seen, improving traceability and debugging capabilities.
This commit is contained in:
@@ -304,6 +304,65 @@ func (c *Client) buildQueryParams(req *GetNotificationsRequest) string {
|
||||
return result
|
||||
}
|
||||
|
||||
// MarkSeen marks a notification as seen
|
||||
func (c *Client) MarkSeen(ctx context.Context, notificationID string) (*MarkSeenResponse, error) {
|
||||
if c.config.EnableLogging && c.logger != nil {
|
||||
c.logger.Debug("Marking notification as seen", map[string]interface{}{
|
||||
"notification_id": notificationID,
|
||||
"base_url": c.config.BaseURL,
|
||||
})
|
||||
}
|
||||
|
||||
// Create HTTP request
|
||||
url := fmt.Sprintf("%s/api/v1/notifications/%s/seen", c.config.BaseURL, notificationID)
|
||||
httpReq, err := http.NewRequestWithContext(ctx, "PUT", url, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create HTTP request: %w", err)
|
||||
}
|
||||
|
||||
// Set headers
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("User-Agent", c.config.UserAgent)
|
||||
|
||||
// Send request
|
||||
resp, err := c.httpClient.Do(httpReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("HTTP request failed: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Read response body
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
// Handle non-2xx status codes
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
// Try to parse error response
|
||||
var errorResp NotificationErrorResponse
|
||||
if json.Unmarshal(body, &errorResp) == nil && errorResp.Error != "" {
|
||||
return nil, MapHTTPError(resp.StatusCode, errorResp.Error)
|
||||
}
|
||||
return nil, MapHTTPError(resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
// Parse success response
|
||||
var markSeenResp MarkSeenResponse
|
||||
if err := json.Unmarshal(body, &markSeenResp); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
|
||||
}
|
||||
|
||||
if c.config.EnableLogging && c.logger != nil {
|
||||
c.logger.Info("Notification marked as seen successfully", map[string]interface{}{
|
||||
"notification_id": notificationID,
|
||||
"status": markSeenResp.Status,
|
||||
})
|
||||
}
|
||||
|
||||
return &markSeenResp, nil
|
||||
}
|
||||
|
||||
// shouldRetry determines if a request should be retried based on the error
|
||||
func (c *Client) shouldRetry(err error) bool {
|
||||
switch e := err.(type) {
|
||||
|
||||
Reference in New Issue
Block a user