Enhance Notification Management with New Endpoints and Response Structures
- Added new endpoints for retrieving notifications for both admins and users, improving the flexibility of the notification system. - Implemented query parameters for filtering notifications by status, method, event type, type, and recipient, enhancing usability. - Introduced new response structures for notifications, including pagination information, to provide better data handling in API responses. - Updated API documentation with Swagger comments for the new endpoints and response formats, ensuring clarity for API consumers. - Refactored notification handling logic to support the new features, promoting a more robust notification management system.
This commit is contained in:
@@ -197,6 +197,113 @@ func (c *Client) sendRequest(ctx context.Context, req *NotificationRequest) (*No
|
||||
return ¬ificationResp, nil
|
||||
}
|
||||
|
||||
// GetNotifications retrieves a list of notifications from the notification service
|
||||
func (c *Client) GetNotifications(ctx context.Context, req *GetNotificationsRequest) (*NotificationListResponse, error) {
|
||||
if c.config.EnableLogging && c.logger != nil {
|
||||
c.logger.Debug("Getting notifications", map[string]interface{}{
|
||||
"status": req.Status,
|
||||
"method": req.Method,
|
||||
"event_type": req.EventType,
|
||||
"type": req.Type,
|
||||
"user_id": req.UserID,
|
||||
"base_url": c.config.BaseURL,
|
||||
})
|
||||
}
|
||||
|
||||
// Build query parameters
|
||||
queryParams := c.buildQueryParams(req)
|
||||
|
||||
// Create HTTP request
|
||||
url := fmt.Sprintf("%s/api/v1/notifications?%s", c.config.BaseURL, queryParams)
|
||||
httpReq, err := http.NewRequestWithContext(ctx, "GET", 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 notificationsResp NotificationListResponse
|
||||
if err := json.Unmarshal(body, ¬ificationsResp); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
|
||||
}
|
||||
|
||||
if c.config.EnableLogging && c.logger != nil {
|
||||
c.logger.Info("Notifications retrieved successfully", map[string]interface{}{
|
||||
"count": len(notificationsResp.Data),
|
||||
"total": notificationsResp.Pagination.Total,
|
||||
"page": notificationsResp.Pagination.CurrentPage,
|
||||
"total_pages": notificationsResp.Pagination.TotalPages,
|
||||
})
|
||||
}
|
||||
|
||||
return ¬ificationsResp, nil
|
||||
}
|
||||
|
||||
// buildQueryParams builds query parameters from the request
|
||||
func (c *Client) buildQueryParams(req *GetNotificationsRequest) string {
|
||||
params := make([]string, 0)
|
||||
|
||||
if req.Status != "" {
|
||||
params = append(params, fmt.Sprintf("status=%s", req.Status))
|
||||
}
|
||||
if req.Method != "" {
|
||||
params = append(params, fmt.Sprintf("method=%s", req.Method))
|
||||
}
|
||||
if req.EventType != "" {
|
||||
params = append(params, fmt.Sprintf("event_type=%s", req.EventType))
|
||||
}
|
||||
if req.Type != "" {
|
||||
params = append(params, fmt.Sprintf("type=%s", req.Type))
|
||||
}
|
||||
if req.UserID != "" {
|
||||
params = append(params, fmt.Sprintf("user_id=%s", req.UserID))
|
||||
}
|
||||
if req.Page > 0 {
|
||||
params = append(params, fmt.Sprintf("page=%d", req.Page))
|
||||
}
|
||||
if req.PerPage > 0 {
|
||||
params = append(params, fmt.Sprintf("per_page=%d", req.PerPage))
|
||||
}
|
||||
|
||||
// Join parameters with &
|
||||
if len(params) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
result := params[0]
|
||||
for i := 1; i < len(params); i++ {
|
||||
result += "&" + params[i]
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// 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