|
@@ -8,24 +8,7 @@ from rest_framework.response import Response
|
|
|
|
|
|
from sentry.api.bases.organization import OrganizationEndpoint
|
|
|
from sentry.utils import json, redis
|
|
|
-
|
|
|
-STATE_CATEGORIES = {
|
|
|
- "onboarding": {
|
|
|
- "ttl": 30 * 24 * 60 * 60, # the time in seconds that the state will be persisted
|
|
|
- "scope": "org", # Can be "org" or "member"
|
|
|
- "max_payload_size": 1024,
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-def get_client_state_key(organization, category, user):
|
|
|
- if category not in STATE_CATEGORIES:
|
|
|
- raise NotFound(detail="Category not found")
|
|
|
- scope = STATE_CATEGORIES[category]["scope"]
|
|
|
- if scope == "member":
|
|
|
- return f"client-state:{category}:{organization}:{user.id}"
|
|
|
- elif scope == "org":
|
|
|
- return f"client-state:{category}:{organization}"
|
|
|
+from sentry.utils.client_state import STATE_CATEGORIES, get_client_state_key, get_redis_client
|
|
|
|
|
|
|
|
|
class ClientStateListEndpoint(OrganizationEndpoint):
|
|
@@ -50,12 +33,21 @@ class ClientStateEndpoint(OrganizationEndpoint):
|
|
|
private = True
|
|
|
|
|
|
def __init__(self, **options) -> None:
|
|
|
- cluster_key = getattr(settings, "SENTRY_CLIENT_STATE_REDIS_CLUSTER", "default")
|
|
|
- self.client = redis.redis_clusters.get(cluster_key)
|
|
|
+ self.client = get_redis_client()
|
|
|
super().__init__(**options)
|
|
|
|
|
|
- def get(self, request: Request, organization, category) -> Response:
|
|
|
+ def convert_args(self, request: Request, organization_slug, *args, **kwargs):
|
|
|
+ (args, kwargs) = super().convert_args(request, organization_slug, *args, **kwargs)
|
|
|
+ organization = kwargs["organization"]
|
|
|
+ category = kwargs["category"]
|
|
|
key = get_client_state_key(organization.slug, category, request.user)
|
|
|
+ if not key:
|
|
|
+ raise NotFound(detail="Category not found")
|
|
|
+
|
|
|
+ kwargs["key"] = key
|
|
|
+ return (args, kwargs)
|
|
|
+
|
|
|
+ def get(self, request: Request, organization, category, key) -> Response:
|
|
|
value = self.client.get(key)
|
|
|
if value:
|
|
|
response = HttpResponse(value)
|
|
@@ -64,15 +56,13 @@ class ClientStateEndpoint(OrganizationEndpoint):
|
|
|
else:
|
|
|
return Response({})
|
|
|
|
|
|
- def put(self, request: Request, organization, category):
|
|
|
- key = get_client_state_key(organization.slug, category, request.user)
|
|
|
+ def put(self, request: Request, organization, category, key):
|
|
|
data_to_write = json.dumps(request.data)
|
|
|
if len(data_to_write) > STATE_CATEGORIES[category]["max_payload_size"]:
|
|
|
return Response(status=413)
|
|
|
self.client.setex(key, STATE_CATEGORIES[category]["ttl"], data_to_write)
|
|
|
return Response(status=201)
|
|
|
|
|
|
- def delete(self, request: Request, organization, category):
|
|
|
- key = get_client_state_key(organization.slug, category, request.user)
|
|
|
+ def delete(self, request: Request, organization, category, key):
|
|
|
self.client.delete(key)
|
|
|
return Response(status=204)
|