api.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. from typing import Optional
  2. from allauth.socialaccount.models import SocialApp
  3. from allauth.socialaccount.providers.openid_connect.views import OpenIDConnectAdapter
  4. from django.conf import settings
  5. from django.http import HttpRequest
  6. from ninja import ModelSchema, NinjaAPI, Schema
  7. from glitchtip.constants import SOCIAL_ADAPTER_MAP
  8. from users.utils import ais_user_registration_open
  9. from .exceptions import ThrottleException
  10. from .parsers import EnvelopeParser
  11. from .schema import CamelSchema
  12. try:
  13. from djstripe.settings import djstripe_settings
  14. except ImportError:
  15. pass
  16. api = NinjaAPI(parser=EnvelopeParser(), title="GlitchTip API", urls_namespace="api")
  17. api.add_router("", "glitchtip.event_ingest.api.router")
  18. @api.exception_handler(ThrottleException)
  19. def throttled(request: HttpRequest, exc: ThrottleException):
  20. response = api.create_response(
  21. request,
  22. {"message": "Please retry later"},
  23. status=429,
  24. )
  25. if retry_after := exc.retry_after:
  26. if isinstance(retry_after, int):
  27. response["Retry-After"] = retry_after
  28. else:
  29. response["Retry-After"] = retry_after.strftime("%a, %d %b %Y %H:%M:%S GMT")
  30. return response
  31. class SocialAppSchema(ModelSchema):
  32. scopes: list[str]
  33. authorize_url: Optional[str]
  34. class Config:
  35. model = SocialApp
  36. model_fields = ["name", "client_id", "provider"]
  37. class SettingsOut(CamelSchema):
  38. social_apps: list[SocialAppSchema]
  39. billing_enabled: bool
  40. i_paid_for_glitchtip: bool
  41. enable_user_registration: bool
  42. enable_organization_creation: bool
  43. stripe_public_key: Optional[str]
  44. plausible_url: Optional[str]
  45. plausible_domain: Optional[str]
  46. chatwoot_website_token: Optional[str]
  47. sentryDSN: Optional[str]
  48. sentry_traces_sample_rate: Optional[float]
  49. environment: Optional[str]
  50. version: str
  51. server_time_zone: str
  52. @api.get("settings/", response=SettingsOut, by_alias=True)
  53. async def get_settings(request: HttpRequest):
  54. social_apps: list[SocialApp] = []
  55. async for social_app in SocialApp.objects.order_by("name"):
  56. provider = social_app.get_provider(request)
  57. social_app.scopes = provider.get_scope(request)
  58. adapter_cls = SOCIAL_ADAPTER_MAP.get(social_app.provider)
  59. if adapter_cls == OpenIDConnectAdapter:
  60. adapter = adapter_cls(request, social_app.provider_id)
  61. elif adapter_cls:
  62. adapter = adapter_cls(request)
  63. else:
  64. adapter = None
  65. if adapter:
  66. social_app.authorize_url = adapter.authorize_url
  67. social_app.provider = social_app.provider_id or social_app.provider
  68. social_apps.append(social_app)
  69. billing_enabled = settings.BILLING_ENABLED
  70. return {
  71. "social_apps": social_apps,
  72. "billing_enabled": billing_enabled,
  73. "i_paid_for_glitchtip": settings.I_PAID_FOR_GLITCHTIP,
  74. "enable_user_registration": await ais_user_registration_open(),
  75. "enable_organization_creation": settings.ENABLE_ORGANIZATION_CREATION,
  76. "stripe_public_key": djstripe_settings.STRIPE_PUBLIC_KEY
  77. if billing_enabled
  78. else None,
  79. "plausible_url": settings.PLAUSIBLE_URL,
  80. "plausible_domain": settings.PLAUSIBLE_DOMAIN,
  81. "chatwoot_website_token": settings.CHATWOOT_WEBSITE_TOKEN,
  82. "sentryDSN": settings.SENTRY_FRONTEND_DSN,
  83. "sentry_traces_sample_rate": settings.SENTRY_TRACES_SAMPLE_RATE,
  84. "environment": settings.ENVIRONMENT,
  85. "version": settings.GLITCHTIP_VERSION,
  86. "server_time_zone": settings.TIME_ZONE,
  87. }