playerdatapy 1.8.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,294 @@
1
+ # Generated by ariadne-codegen
2
+
3
+ from .async_base_client import AsyncBaseClient
4
+ from .base_model import BaseModel, Upload
5
+ from .enums import (
6
+ AggFuncEnum,
7
+ AppAuthenticationFlow,
8
+ AppMessageTypeEnum,
9
+ ChartDataTypeEnum,
10
+ ChartTypeEnum,
11
+ ClubSport,
12
+ ConfiguredMetricListTypeEnum,
13
+ CreatorTypeEnum,
14
+ CustomMaxMetricEnum,
15
+ DatafileFormat,
16
+ DatasetStatusEnum,
17
+ DecryptionArea,
18
+ DetectedMatchEventState,
19
+ DeviceOwnerType,
20
+ DeviceSyncTypeEnum,
21
+ DeviceTypeEnum,
22
+ DiagnosticWarningErrorTypeEnum,
23
+ DisplayUnitEnum,
24
+ EdgeOwnerType,
25
+ FeatureNameEnum,
26
+ FirmwareBoardName,
27
+ FirmwareBuildProfile,
28
+ FirmwareFeatureVariant,
29
+ FirmwareProject,
30
+ FirmwareVariant,
31
+ MatchEventClassEnum,
32
+ MatchEventTeam,
33
+ MatchSessionResult,
34
+ OperatingMode,
35
+ OrderDirectionEnum,
36
+ OwnerEnum,
37
+ PathmapPathType,
38
+ PermissionAction,
39
+ PermissionSubject,
40
+ Platform,
41
+ ProcessingWarning,
42
+ QuickActionStateEnum,
43
+ RatingEnum,
44
+ ReportType,
45
+ ReportTypeEnum,
46
+ ResponderEnum,
47
+ SeriesChartTypeEnum,
48
+ SessionParticipationWarningCodes,
49
+ SessionTypeEnum,
50
+ SessionWarningCodes,
51
+ SignupFlow,
52
+ SortField,
53
+ StripeSubscriptionStatus,
54
+ TaggableTypeEnum,
55
+ TaggerTypeEnum,
56
+ UnitSystem,
57
+ )
58
+ from .exceptions import (
59
+ GraphQLClientError,
60
+ GraphQLClientGraphQLError,
61
+ GraphQLClientGraphQLMultiError,
62
+ GraphQLClientHttpError,
63
+ GraphQLClientInvalidResponseError,
64
+ )
65
+ from .gqlclient import Client
66
+ from .input_types import (
67
+ AccelzoneLowerBoundsInput,
68
+ AthleteAccelzoneAttributes,
69
+ AthleteClippedTimesInput,
70
+ AthleteDecelzoneAttributes,
71
+ AthleteGroupAttributes,
72
+ AthleteHeartRateBoundsAttributes,
73
+ AthleteRelativeAccelzoneAttributes,
74
+ AthleteRelativeDecelzoneAttributes,
75
+ AthleteSpeedzoneAttributes,
76
+ BulkUpdateMatchEventAttributes,
77
+ ClaimPersonAttributes,
78
+ ClubContextAttributes,
79
+ ClubPersonFilter,
80
+ CoachContextAttributesInput,
81
+ CreateDeviceSyncInput,
82
+ CreateFlexibleReportChartAttributes,
83
+ CustomQuestionDefinitionAttributes,
84
+ DatasetAttributes,
85
+ DecelzoneLowerBoundsInput,
86
+ DeviceAttributes,
87
+ DuplicateSessionAttributes,
88
+ EdgeSessionFilter,
89
+ FlexibleReportAttributes,
90
+ GatewaySessionAttributes,
91
+ HeartRateLowerBoundsInput,
92
+ IntervalInput,
93
+ LiveDataGatewayOwnershipAvailableGatewaysFilter,
94
+ LiveDataGatewayOwnershipGatewaysCurrentlyOwnedFilter,
95
+ MatchEventAttributes,
96
+ MatchPeriodAttributes,
97
+ MutateSessionAttributes,
98
+ MutateSessionBlueprintAttributes,
99
+ NewPersonAttributes,
100
+ OrderInputObject,
101
+ PitchAttributes,
102
+ PitchCoordinateAttributes,
103
+ PitchCoordinateSetAttributes,
104
+ PositionAttributes,
105
+ RecurrenceScheduleInput,
106
+ RecurrenceScheduleWeeklyRuleInput,
107
+ RelativeSpeedzoneAttributes,
108
+ ReportAttributes,
109
+ SegmentAttributes,
110
+ SegmentParticipationPatch,
111
+ SegmentPatch,
112
+ SessionBlueprintSegmentAttributes,
113
+ SessionParticipationAttributes,
114
+ SessionPlanAttributesInput,
115
+ SessionPositionsAttributes,
116
+ SessionsSessionAggregateMetricsFilter,
117
+ SessionsSessionAggregatePersonMetricsFilter,
118
+ SessionsSessionBaseFilter,
119
+ SessionsSessionFilter,
120
+ SessionsSessionParticipationBaseFilter,
121
+ SessionTagDefinitionAttributes,
122
+ SpeedzoneLowerBoundsInput,
123
+ SurveyAnswerAttributes,
124
+ SurveyAttributes,
125
+ SurveyDistributionAttributes,
126
+ SurveysSurveyAssignmentBaseFilter,
127
+ SurveysSurveyDistributionBaseFilter,
128
+ SurveyTimerTriggerAttributes,
129
+ SurveyTimerTriggerCreateAttributes,
130
+ TagDefinitionAttributes,
131
+ TaggerAttributes,
132
+ TimeSpanAttributes,
133
+ UnitOptionsInput,
134
+ UpdateAthleteAttributes,
135
+ UpdateClubMemberAttributes,
136
+ UpdateClubSettingsAttributes,
137
+ UpdateDatasetAttributes,
138
+ UpdateFlexibleReportAttributes,
139
+ UpdateFlexibleReportChartAttributes,
140
+ UpdateGatewayOwnershipAttributes,
141
+ UpdatePersonAttributes,
142
+ UpdateSettingsAttributes,
143
+ UpdateStaffBillingAttributes,
144
+ UpdateUserPreferencesAttributes,
145
+ UpsertDataRecordingsAttributes,
146
+ VideoRecordingAttributes,
147
+ )
148
+
149
+ # Appended manually
150
+ from .playerdata_api import PlayerDataAPI
151
+
152
+ __all__ = [
153
+ "PlayerDataAPI",
154
+ "AccelzoneLowerBoundsInput",
155
+ "AggFuncEnum",
156
+ "AppAuthenticationFlow",
157
+ "AppMessageTypeEnum",
158
+ "AsyncBaseClient",
159
+ "AthleteAccelzoneAttributes",
160
+ "AthleteClippedTimesInput",
161
+ "AthleteDecelzoneAttributes",
162
+ "AthleteGroupAttributes",
163
+ "AthleteHeartRateBoundsAttributes",
164
+ "AthleteRelativeAccelzoneAttributes",
165
+ "AthleteRelativeDecelzoneAttributes",
166
+ "AthleteSpeedzoneAttributes",
167
+ "BaseModel",
168
+ "BulkUpdateMatchEventAttributes",
169
+ "ChartDataTypeEnum",
170
+ "ChartTypeEnum",
171
+ "ClaimPersonAttributes",
172
+ "Client",
173
+ "ClubContextAttributes",
174
+ "ClubPersonFilter",
175
+ "ClubSport",
176
+ "CoachContextAttributesInput",
177
+ "ConfiguredMetricListTypeEnum",
178
+ "CreateDeviceSyncInput",
179
+ "CreateFlexibleReportChartAttributes",
180
+ "CreatorTypeEnum",
181
+ "CustomMaxMetricEnum",
182
+ "CustomQuestionDefinitionAttributes",
183
+ "DatafileFormat",
184
+ "DatasetAttributes",
185
+ "DatasetStatusEnum",
186
+ "DecelzoneLowerBoundsInput",
187
+ "DecryptionArea",
188
+ "DetectedMatchEventState",
189
+ "DeviceAttributes",
190
+ "DeviceOwnerType",
191
+ "DeviceSyncTypeEnum",
192
+ "DeviceTypeEnum",
193
+ "DiagnosticWarningErrorTypeEnum",
194
+ "DisplayUnitEnum",
195
+ "DuplicateSessionAttributes",
196
+ "EdgeOwnerType",
197
+ "EdgeSessionFilter",
198
+ "FeatureNameEnum",
199
+ "FirmwareBoardName",
200
+ "FirmwareBuildProfile",
201
+ "FirmwareFeatureVariant",
202
+ "FirmwareProject",
203
+ "FirmwareVariant",
204
+ "FlexibleReportAttributes",
205
+ "GatewaySessionAttributes",
206
+ "GraphQLClientError",
207
+ "GraphQLClientGraphQLError",
208
+ "GraphQLClientGraphQLMultiError",
209
+ "GraphQLClientHttpError",
210
+ "GraphQLClientInvalidResponseError",
211
+ "HeartRateLowerBoundsInput",
212
+ "IntervalInput",
213
+ "LiveDataGatewayOwnershipAvailableGatewaysFilter",
214
+ "LiveDataGatewayOwnershipGatewaysCurrentlyOwnedFilter",
215
+ "MatchEventAttributes",
216
+ "MatchEventClassEnum",
217
+ "MatchEventTeam",
218
+ "MatchPeriodAttributes",
219
+ "MatchSessionResult",
220
+ "MutateSessionAttributes",
221
+ "MutateSessionBlueprintAttributes",
222
+ "NewPersonAttributes",
223
+ "OperatingMode",
224
+ "OrderDirectionEnum",
225
+ "OrderInputObject",
226
+ "OwnerEnum",
227
+ "PathmapPathType",
228
+ "PermissionAction",
229
+ "PermissionSubject",
230
+ "PitchAttributes",
231
+ "PitchCoordinateAttributes",
232
+ "PitchCoordinateSetAttributes",
233
+ "Platform",
234
+ "PositionAttributes",
235
+ "ProcessingWarning",
236
+ "QuickActionStateEnum",
237
+ "RatingEnum",
238
+ "RecurrenceScheduleInput",
239
+ "RecurrenceScheduleWeeklyRuleInput",
240
+ "RelativeSpeedzoneAttributes",
241
+ "ReportAttributes",
242
+ "ReportType",
243
+ "ReportTypeEnum",
244
+ "ResponderEnum",
245
+ "SegmentAttributes",
246
+ "SegmentParticipationPatch",
247
+ "SegmentPatch",
248
+ "SeriesChartTypeEnum",
249
+ "SessionBlueprintSegmentAttributes",
250
+ "SessionParticipationAttributes",
251
+ "SessionParticipationWarningCodes",
252
+ "SessionPlanAttributesInput",
253
+ "SessionPositionsAttributes",
254
+ "SessionTagDefinitionAttributes",
255
+ "SessionTypeEnum",
256
+ "SessionWarningCodes",
257
+ "SessionsSessionAggregateMetricsFilter",
258
+ "SessionsSessionAggregatePersonMetricsFilter",
259
+ "SessionsSessionBaseFilter",
260
+ "SessionsSessionFilter",
261
+ "SessionsSessionParticipationBaseFilter",
262
+ "SignupFlow",
263
+ "SortField",
264
+ "SpeedzoneLowerBoundsInput",
265
+ "StripeSubscriptionStatus",
266
+ "SurveyAnswerAttributes",
267
+ "SurveyAttributes",
268
+ "SurveyDistributionAttributes",
269
+ "SurveyTimerTriggerAttributes",
270
+ "SurveyTimerTriggerCreateAttributes",
271
+ "SurveysSurveyAssignmentBaseFilter",
272
+ "SurveysSurveyDistributionBaseFilter",
273
+ "TagDefinitionAttributes",
274
+ "TaggableTypeEnum",
275
+ "TaggerAttributes",
276
+ "TaggerTypeEnum",
277
+ "TimeSpanAttributes",
278
+ "UnitOptionsInput",
279
+ "UnitSystem",
280
+ "UpdateAthleteAttributes",
281
+ "UpdateClubMemberAttributes",
282
+ "UpdateClubSettingsAttributes",
283
+ "UpdateDatasetAttributes",
284
+ "UpdateFlexibleReportAttributes",
285
+ "UpdateFlexibleReportChartAttributes",
286
+ "UpdateGatewayOwnershipAttributes",
287
+ "UpdatePersonAttributes",
288
+ "UpdateSettingsAttributes",
289
+ "UpdateStaffBillingAttributes",
290
+ "UpdateUserPreferencesAttributes",
291
+ "Upload",
292
+ "UpsertDataRecordingsAttributes",
293
+ "VideoRecordingAttributes",
294
+ ]
@@ -0,0 +1,375 @@
1
+ # Generated by ariadne-codegen
2
+
3
+ import enum
4
+ import json
5
+ from collections.abc import AsyncIterator
6
+ from typing import IO, Any, Optional, TypeVar, cast
7
+ from uuid import uuid4
8
+
9
+ import httpx
10
+ from pydantic import BaseModel
11
+ from pydantic_core import to_jsonable_python
12
+
13
+ from .base_model import UNSET, Upload
14
+ from .exceptions import (
15
+ GraphQLClientGraphQLMultiError,
16
+ GraphQLClientHttpError,
17
+ GraphQLClientInvalidMessageFormat,
18
+ GraphQLClientInvalidResponseError,
19
+ )
20
+
21
+ try:
22
+ from websockets import ( # type: ignore[import-not-found,unused-ignore]
23
+ ClientConnection,
24
+ )
25
+ from websockets import ( # type: ignore[import-not-found,unused-ignore]
26
+ connect as ws_connect,
27
+ )
28
+ from websockets.typing import ( # type: ignore[import-not-found,unused-ignore]
29
+ Data,
30
+ Origin,
31
+ Subprotocol,
32
+ )
33
+ except ImportError:
34
+ from contextlib import asynccontextmanager
35
+
36
+ @asynccontextmanager # type: ignore
37
+ async def ws_connect(*args, **kwargs):
38
+ raise NotImplementedError("Subscriptions require 'websockets' package.")
39
+ yield
40
+
41
+ ClientConnection = Any # type: ignore[misc,assignment,unused-ignore]
42
+ Data = Any # type: ignore[misc,assignment,unused-ignore]
43
+ Origin = Any # type: ignore[misc,assignment,unused-ignore]
44
+
45
+ def Subprotocol(*args, **kwargs): # type: ignore # noqa: N802, N803
46
+ raise NotImplementedError("Subscriptions require 'websockets' package.")
47
+
48
+
49
+ Self = TypeVar("Self", bound="AsyncBaseClient")
50
+
51
+ GRAPHQL_TRANSPORT_WS = "graphql-transport-ws"
52
+
53
+
54
+ class GraphQLTransportWSMessageType(str, enum.Enum):
55
+ CONNECTION_INIT = "connection_init"
56
+ CONNECTION_ACK = "connection_ack"
57
+ PING = "ping"
58
+ PONG = "pong"
59
+ SUBSCRIBE = "subscribe"
60
+ NEXT = "next"
61
+ ERROR = "error"
62
+ COMPLETE = "complete"
63
+
64
+
65
+ class AsyncBaseClient:
66
+ def __init__(
67
+ self,
68
+ url: str = "",
69
+ headers: Optional[dict[str, str]] = None,
70
+ http_client: Optional[httpx.AsyncClient] = None,
71
+ ws_url: str = "",
72
+ ws_headers: Optional[dict[str, Any]] = None,
73
+ ws_origin: Optional[str] = None,
74
+ ws_connection_init_payload: Optional[dict[str, Any]] = None,
75
+ ) -> None:
76
+ self.url = url
77
+ self.headers = headers
78
+ self.http_client = (
79
+ http_client if http_client else httpx.AsyncClient(headers=headers)
80
+ )
81
+
82
+ self.ws_url = ws_url
83
+ self.ws_headers = ws_headers or {}
84
+ self.ws_origin = Origin(ws_origin) if ws_origin else None
85
+ self.ws_connection_init_payload = ws_connection_init_payload
86
+
87
+ async def __aenter__(self: Self) -> Self:
88
+ return self
89
+
90
+ async def __aexit__(
91
+ self,
92
+ exc_type: object,
93
+ exc_val: object,
94
+ exc_tb: object,
95
+ ) -> None:
96
+ await self.http_client.aclose()
97
+
98
+ async def execute(
99
+ self,
100
+ query: str,
101
+ operation_name: Optional[str] = None,
102
+ variables: Optional[dict[str, Any]] = None,
103
+ **kwargs: Any,
104
+ ) -> httpx.Response:
105
+ processed_variables, files, files_map = self._process_variables(variables)
106
+
107
+ if files and files_map:
108
+ return await self._execute_multipart(
109
+ query=query,
110
+ operation_name=operation_name,
111
+ variables=processed_variables,
112
+ files=files,
113
+ files_map=files_map,
114
+ **kwargs,
115
+ )
116
+
117
+ return await self._execute_json(
118
+ query=query,
119
+ operation_name=operation_name,
120
+ variables=processed_variables,
121
+ **kwargs,
122
+ )
123
+
124
+ def get_data(self, response: httpx.Response) -> dict[str, Any]:
125
+ if not response.is_success:
126
+ raise GraphQLClientHttpError(
127
+ status_code=response.status_code, response=response
128
+ )
129
+
130
+ try:
131
+ response_json = response.json()
132
+ except ValueError as exc:
133
+ raise GraphQLClientInvalidResponseError(response=response) from exc
134
+
135
+ if (not isinstance(response_json, dict)) or (
136
+ "data" not in response_json and "errors" not in response_json
137
+ ):
138
+ raise GraphQLClientInvalidResponseError(response=response)
139
+
140
+ data = response_json.get("data")
141
+ errors = response_json.get("errors")
142
+
143
+ if errors:
144
+ raise GraphQLClientGraphQLMultiError.from_errors_dicts(
145
+ errors_dicts=errors, data=data
146
+ )
147
+
148
+ return cast(dict[str, Any], data)
149
+
150
+ async def execute_ws(
151
+ self,
152
+ query: str,
153
+ operation_name: Optional[str] = None,
154
+ variables: Optional[dict[str, Any]] = None,
155
+ **kwargs: Any,
156
+ ) -> AsyncIterator[dict[str, Any]]:
157
+ headers = self.ws_headers.copy()
158
+ headers.update(kwargs.get("extra_headers", {}))
159
+
160
+ merged_kwargs: dict[str, Any] = {"origin": self.ws_origin}
161
+ merged_kwargs.update(kwargs)
162
+ merged_kwargs["extra_headers"] = headers
163
+
164
+ operation_id = str(uuid4())
165
+ async with ws_connect(
166
+ self.ws_url,
167
+ subprotocols=[Subprotocol(GRAPHQL_TRANSPORT_WS)],
168
+ **merged_kwargs,
169
+ ) as websocket:
170
+ await self._send_connection_init(websocket)
171
+ # wait for connection_ack from server
172
+ await self._handle_ws_message(
173
+ await websocket.recv(),
174
+ websocket,
175
+ expected_type=GraphQLTransportWSMessageType.CONNECTION_ACK,
176
+ )
177
+ await self._send_subscribe(
178
+ websocket,
179
+ operation_id=operation_id,
180
+ query=query,
181
+ operation_name=operation_name,
182
+ variables=variables,
183
+ )
184
+
185
+ async for message in websocket:
186
+ data = await self._handle_ws_message(message, websocket)
187
+ if data:
188
+ yield data
189
+
190
+ def _process_variables(
191
+ self, variables: Optional[dict[str, Any]]
192
+ ) -> tuple[
193
+ dict[str, Any], dict[str, tuple[str, IO[bytes], str]], dict[str, list[str]]
194
+ ]:
195
+ if not variables:
196
+ return {}, {}, {}
197
+
198
+ serializable_variables = self._convert_dict_to_json_serializable(variables)
199
+ return self._get_files_from_variables(serializable_variables)
200
+
201
+ def _convert_dict_to_json_serializable(
202
+ self, dict_: dict[str, Any]
203
+ ) -> dict[str, Any]:
204
+ return {
205
+ key: self._convert_value(value)
206
+ for key, value in dict_.items()
207
+ if value is not UNSET
208
+ }
209
+
210
+ def _convert_value(self, value: Any) -> Any:
211
+ if isinstance(value, BaseModel):
212
+ return value.model_dump(by_alias=True, exclude_unset=True)
213
+ if isinstance(value, list):
214
+ return [self._convert_value(item) for item in value]
215
+ return value
216
+
217
+ def _get_files_from_variables(
218
+ self, variables: dict[str, Any]
219
+ ) -> tuple[
220
+ dict[str, Any], dict[str, tuple[str, IO[bytes], str]], dict[str, list[str]]
221
+ ]:
222
+ files_map: dict[str, list[str]] = {}
223
+ files_list: list[Upload] = []
224
+
225
+ def separate_files(path: str, obj: Any) -> Any:
226
+ if isinstance(obj, list):
227
+ nulled_list = []
228
+ for index, value in enumerate(obj):
229
+ value = separate_files(f"{path}.{index}", value)
230
+ nulled_list.append(value)
231
+ return nulled_list
232
+
233
+ if isinstance(obj, dict):
234
+ nulled_dict = {}
235
+ for key, value in obj.items():
236
+ value = separate_files(f"{path}.{key}", value)
237
+ nulled_dict[key] = value
238
+ return nulled_dict
239
+
240
+ if isinstance(obj, Upload):
241
+ if obj in files_list:
242
+ file_index = files_list.index(obj)
243
+ files_map[str(file_index)].append(path)
244
+ else:
245
+ file_index = len(files_list)
246
+ files_list.append(obj)
247
+ files_map[str(file_index)] = [path]
248
+ return None
249
+
250
+ return obj
251
+
252
+ nulled_variables = separate_files("variables", variables)
253
+ files: dict[str, tuple[str, IO[bytes], str]] = {
254
+ str(i): (file_.filename, cast(IO[bytes], file_.content), file_.content_type)
255
+ for i, file_ in enumerate(files_list)
256
+ }
257
+ return nulled_variables, files, files_map
258
+
259
+ async def _execute_multipart(
260
+ self,
261
+ query: str,
262
+ operation_name: Optional[str],
263
+ variables: dict[str, Any],
264
+ files: dict[str, tuple[str, IO[bytes], str]],
265
+ files_map: dict[str, list[str]],
266
+ **kwargs: Any,
267
+ ) -> httpx.Response:
268
+ data = {
269
+ "operations": json.dumps(
270
+ {
271
+ "query": query,
272
+ "operationName": operation_name,
273
+ "variables": variables,
274
+ },
275
+ default=to_jsonable_python,
276
+ ),
277
+ "map": json.dumps(files_map, default=to_jsonable_python),
278
+ }
279
+
280
+ return await self.http_client.post(
281
+ url=self.url, data=data, files=files, **kwargs
282
+ )
283
+
284
+ async def _execute_json(
285
+ self,
286
+ query: str,
287
+ operation_name: Optional[str],
288
+ variables: dict[str, Any],
289
+ **kwargs: Any,
290
+ ) -> httpx.Response:
291
+ headers: dict[str, str] = {"Content-type": "application/json"}
292
+ headers.update(kwargs.get("headers", {}))
293
+
294
+ merged_kwargs: dict[str, Any] = kwargs.copy()
295
+ merged_kwargs["headers"] = headers
296
+
297
+ return await self.http_client.post(
298
+ url=self.url,
299
+ content=json.dumps(
300
+ {
301
+ "query": query,
302
+ "operationName": operation_name,
303
+ "variables": variables,
304
+ },
305
+ default=to_jsonable_python,
306
+ ),
307
+ **merged_kwargs,
308
+ )
309
+
310
+ async def _send_connection_init(self, websocket: ClientConnection) -> None:
311
+ payload: dict[str, Any] = {
312
+ "type": GraphQLTransportWSMessageType.CONNECTION_INIT.value
313
+ }
314
+ if self.ws_connection_init_payload:
315
+ payload["payload"] = self.ws_connection_init_payload
316
+ await websocket.send(json.dumps(payload))
317
+
318
+ async def _send_subscribe(
319
+ self,
320
+ websocket: ClientConnection,
321
+ operation_id: str,
322
+ query: str,
323
+ operation_name: Optional[str] = None,
324
+ variables: Optional[dict[str, Any]] = None,
325
+ ) -> None:
326
+ payload: dict[str, Any] = {
327
+ "id": operation_id,
328
+ "type": GraphQLTransportWSMessageType.SUBSCRIBE.value,
329
+ "payload": {"query": query, "operationName": operation_name},
330
+ }
331
+ if variables:
332
+ payload["payload"]["variables"] = self._convert_dict_to_json_serializable(
333
+ variables
334
+ )
335
+ await websocket.send(json.dumps(payload))
336
+
337
+ async def _handle_ws_message(
338
+ self,
339
+ message: Data,
340
+ websocket: ClientConnection,
341
+ expected_type: Optional[GraphQLTransportWSMessageType] = None,
342
+ ) -> Optional[dict[str, Any]]:
343
+ try:
344
+ message_dict = json.loads(message)
345
+ except json.JSONDecodeError as exc:
346
+ raise GraphQLClientInvalidMessageFormat(message=message) from exc
347
+
348
+ type_ = message_dict.get("type")
349
+ payload = message_dict.get("payload", {})
350
+
351
+ if not type_ or type_ not in {t.value for t in GraphQLTransportWSMessageType}:
352
+ raise GraphQLClientInvalidMessageFormat(message=message)
353
+
354
+ if expected_type and expected_type != type_:
355
+ raise GraphQLClientInvalidMessageFormat(
356
+ f"Invalid message received. Expected: {expected_type.value}"
357
+ )
358
+
359
+ if type_ == GraphQLTransportWSMessageType.NEXT:
360
+ if "data" not in payload:
361
+ raise GraphQLClientInvalidMessageFormat(message=message)
362
+ return cast(dict[str, Any], payload["data"])
363
+
364
+ if type_ == GraphQLTransportWSMessageType.COMPLETE:
365
+ await websocket.close()
366
+ elif type_ == GraphQLTransportWSMessageType.PING:
367
+ await websocket.send(
368
+ json.dumps({"type": GraphQLTransportWSMessageType.PONG.value})
369
+ )
370
+ elif type_ == GraphQLTransportWSMessageType.ERROR:
371
+ raise GraphQLClientGraphQLMultiError.from_errors_dicts(
372
+ errors_dicts=payload, data=message_dict
373
+ )
374
+
375
+ return None
@@ -0,0 +1,19 @@
1
+ from .authorisation_code_flow_base import AuthorisationCodeFlowBase
2
+
3
+
4
+ class AuthorisationCodeFlow(AuthorisationCodeFlowBase):
5
+ """Handles oauth2 authorisation code flow and token management."""
6
+
7
+ def __init__(
8
+ self, client_id: str, port: int, client_secret: str, token_file: str = ".token"
9
+ ):
10
+ super().__init__(client_id, port, token_file)
11
+ self.client_secret = client_secret
12
+
13
+ def _fetch_token(self, code: str) -> dict:
14
+ """Exchange authorization code for access token."""
15
+ return self.oauth_session.fetch_token(
16
+ f"{self.api_base_url}/oauth/token",
17
+ code=code,
18
+ client_secret=self.client_secret,
19
+ )