diff --git a/app/api/v1/endpoints/counters.py b/app/api/v1/endpoints/counters.py index f5068a1..0d953d3 100644 --- a/app/api/v1/endpoints/counters.py +++ b/app/api/v1/endpoints/counters.py @@ -203,4 +203,47 @@ async def create_custom_goals_for_counter( detail="Could not create any of the requested goals. Check logs for details." ) - return GoalCreateResponse(created_goals=created_goals_list) \ No newline at end of file + return GoalCreateResponse(created_goals=created_goals_list) + +# --- НОВЫЙ ЭНДПОИНТ: Удаление цели --- +@router.delete( + "/{counter_id}/goals/{goal_id}", + status_code=status.HTTP_204_NO_CONTENT, + summary="Удаление цели в счетчике" +) +async def delete_goal_in_counter( + counter_id: int, + goal_id: int, + credentials: Annotated[HTTPAuthorizationCredentials, Depends(bearer_scheme)] +): + """ + Удаляет указанную цель в указанном счетчике. + В случае успеха возвращает пустой ответ со статусом 204. + """ + token = credentials.credentials + logger.info(f"Attempting to delete goal ID: {goal_id} from counter ID: {counter_id}") + + url = f"{settings.YANDEX_METRIKA_API_URL}/management/v1/counter/{counter_id}/goal/{goal_id}" + headers = {'Authorization': f'OAuth {token}'} + + try: + async with httpx.AsyncClient() as client: + response = await client.delete(url, headers=headers) + response.raise_for_status() + logger.success(f"Successfully deleted goal ID: {goal_id} from counter {counter_id}.") + # При успехе стандартно возвращается пустой ответ + return + + except httpx.HTTPStatusError as e: + error_details = e.response.json() + logger.error(f"Yandex Metrika API error during goal deletion: {e.response.status_code} - {error_details}") + raise HTTPException( + status_code=e.response.status_code, + detail=f"Yandex API Error: {error_details.get('message', 'Unknown error')}" + ) + except Exception as e: + logger.opt(exception=True).error("An unexpected error occurred during goal deletion.") + raise HTTPException( + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + detail="An internal server error occurred." + ) \ No newline at end of file