contexts.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. from typing import Annotated, Any, Literal, Optional, TypedDict, Union
  2. from ninja import Field, Schema
  3. from pydantic import RootModel
  4. class DeviceContext(Schema):
  5. type: Literal["device"] = "device"
  6. name: Optional[str] = None # Inconsistency documented as required
  7. family: Optional[str] = None # Recommended but optional
  8. model: Optional[str] = None # Recommended but optional
  9. model_id: Optional[str] = None
  10. arch: Optional[str] = None
  11. battery_level: Optional[float] = None
  12. orientation: Optional[str] = None
  13. manufacturer: Optional[str] = None
  14. brand: Optional[str] = None
  15. screen_resolution: Optional[str] = None
  16. screen_height_pixels: Optional[int] = None
  17. screen_width_pixels: Optional[int] = None
  18. screen_density: Optional[float] = None
  19. screen_dpi: Optional[float] = None
  20. online: Optional[bool] = None
  21. charging: Optional[bool] = None
  22. low_memory: Optional[bool] = None
  23. simulator: Optional[bool] = None
  24. memory_size: Optional[int] = None
  25. free_memory: Optional[int] = None
  26. usable_memory: Optional[int] = None
  27. storage_size: Optional[int] = None
  28. free_storage: Optional[int] = None
  29. external_storage_size: Optional[int] = None
  30. external_free_storage: Optional[int] = None
  31. boot_time: Optional[str] = None
  32. timezone: Optional[str] = None # Deprecated, use timezone of culture context
  33. language: Optional[str] = None # Deprecated, use locale of culture context
  34. processor_count: Optional[int] = None
  35. cpu_description: Optional[str] = None
  36. processor_frequency: Optional[float] = None
  37. device_type: Optional[str] = None
  38. battery_status: Optional[str] = None
  39. device_unique_identifier: Optional[str] = None
  40. supports_vibration: Optional[bool] = None
  41. supports_accelerometer: Optional[bool] = None
  42. supports_gyroscope: Optional[bool] = None
  43. supports_audio: Optional[bool] = None
  44. supports_location_service: Optional[bool] = None
  45. class Config(Schema.Config):
  46. protected_namespaces = ()
  47. class OSContext(Schema):
  48. type: Literal["os"] = "os"
  49. name: str
  50. version: Optional[str] = None
  51. build: Optional[str] = None
  52. kernel_version: Optional[str] = None
  53. rooted: Optional[bool] = None
  54. theme: Optional[str] = None
  55. raw_description: Optional[str] = None # Recommended but optional
  56. class RuntimeContext(Schema):
  57. type: Literal["runtime"] = "runtime"
  58. name: str # Recommended
  59. version: Optional[str] = None
  60. raw_description: Optional[str] = None
  61. class AppContext(Schema):
  62. type: Literal["app"] = "app"
  63. app_start_time: Optional[str] = None
  64. device_app_hash: Optional[str] = None
  65. build_type: Optional[str] = None
  66. app_identifier: Optional[str] = None
  67. app_name: Optional[str] = None
  68. app_version: Optional[str] = None
  69. app_build: Optional[str] = None
  70. app_memory: Optional[int] = None
  71. in_foreground: Optional[bool] = None
  72. class BrowserContext(Schema):
  73. type: Literal["browser"] = "browser"
  74. name: str
  75. version: Optional[str] = None
  76. class GPUContext(Schema):
  77. type: Literal["gpu"] = "gpu"
  78. name: str
  79. version: Optional[str] = None
  80. id: Optional[str] = None
  81. vendor_id: Optional[str] = None
  82. vendor_name: Optional[str] = None
  83. memory_size: Optional[int] = None
  84. api_type: Optional[str] = None
  85. multi_threaded_rendering: Optional[bool] = None
  86. npot_support: Optional[str] = None
  87. max_texture_size: Optional[int] = None
  88. graphics_shader_level: Optional[str] = None
  89. supports_draw_call_instancing: Optional[bool] = None
  90. supports_ray_tracing: Optional[bool] = None
  91. supports_compute_shaders: Optional[bool] = None
  92. supports_geometry_shaders: Optional[bool] = None
  93. class StateContext(Schema):
  94. type: Literal["state"] = "state"
  95. state: dict
  96. class CultureContext(Schema):
  97. type: Literal["culture"] = "culture"
  98. calendar: Optional[str] = None
  99. display_name: Optional[str] = None
  100. locale: Optional[str] = None
  101. is_24_hour_format: Optional[bool] = None
  102. timezone: Optional[str] = None
  103. class CloudResourceContext(Schema):
  104. type: Literal["cloud_resource"] = "cloud_resource"
  105. cloud: dict
  106. host: dict
  107. class TraceContext(Schema):
  108. type: Literal["trace"] = "trace"
  109. trace_id: str
  110. span_id: str
  111. parent_span_id: Optional[str] = None
  112. op: Optional[str] = None
  113. status: Optional[str] = None
  114. exclusive_time: Optional[float] = None
  115. client_sample_rate: Optional[float] = None
  116. tags: Optional[Union[dict, list]] = None
  117. dynamic_sampling_context: Optional[dict] = None
  118. origin: Optional[str] = None
  119. class ReplayContext(Schema):
  120. type: Literal["replay"] = "replay"
  121. replay_id: str
  122. class ResponseContext(Schema):
  123. type: Literal["response"] = "response"
  124. status_code: int
  125. class ContextsDict(TypedDict):
  126. device: DeviceContext
  127. os: OSContext
  128. runtime: RuntimeContext
  129. app: AppContext
  130. browser: BrowserContext
  131. gpu: GPUContext
  132. state: StateContext
  133. culture: CultureContext
  134. cloud_resource: CloudResourceContext
  135. trace: TraceContext
  136. replay: ReplayContext
  137. response: ResponseContext
  138. ContextsUnion = Union[
  139. DeviceContext,
  140. OSContext,
  141. RuntimeContext,
  142. AppContext,
  143. BrowserContext,
  144. GPUContext,
  145. StateContext,
  146. CultureContext,
  147. CloudResourceContext,
  148. TraceContext,
  149. ReplayContext,
  150. ResponseContext,
  151. ]
  152. Contexts = dict[str, Union[Annotated[ContextsUnion, Field(discriminator="type")], Any]]
  153. class ContextsSchema(RootModel):
  154. # TODO, add pre validation logic here to assign the type based on the key
  155. root: Contexts