MCPcopy
hub / github.com/encode/starlette / decode

Method decode

starlette/endpoints.py:91–117  ·  starlette/endpoints.py::WebSocketEndpoint.decode
(self, websocket: WebSocket, message: Message)

Source from the content-addressed store, hash-verified

89 await self.on_disconnect(websocket, close_code)
90
91 async def decode(self, websocket: WebSocket, message: Message) -> Any:
92 if self.encoding == class="st">"text":
93 if class="st">"text" not in message:
94 await websocket.close(code=status.WS_1003_UNSUPPORTED_DATA)
95 raise RuntimeError(class="st">"Expected text websocket messages, but got bytes")
96 return message[class="st">"text"]
97
98 elif self.encoding == class="st">"bytes":
99 if class="st">"bytes" not in message:
100 await websocket.close(code=status.WS_1003_UNSUPPORTED_DATA)
101 raise RuntimeError(class="st">"Expected bytes websocket messages, but got text")
102 return message[class="st">"bytes"]
103
104 elif self.encoding == class="st">"json":
105 if message.get(class="st">"text") is not None:
106 text = message[class="st">"text"]
107 else:
108 text = message[class="st">"bytes"].decode(class="st">"utf-8")
109
110 try:
111 return json.loads(text)
112 except json.decoder.JSONDecodeError:
113 await websocket.close(code=status.WS_1003_UNSUPPORTED_DATA)
114 raise RuntimeError(class="st">"Malformed JSON data received.")
115
116 assert self.encoding is None, fclass="st">"Unsupported &class="cm">#x27;encoding' attribute {self.encoding}"
117 return message[class="st">"text"] if message.get(class="st">"text") else message[class="st">"bytes"]
118
119 async def on_connect(self, websocket: WebSocket) -> None:
120 class="st">""class="st">"Override to handle an incoming websocket connection"class="st">""

Callers 15

dispatchMethod · 0.95
receive_jsonMethod · 0.80
handle_requestMethod · 0.80
sendMethod · 0.80
_user_safe_decodeFunction · 0.80
parseMethod · 0.80
parseMethod · 0.80
receive_jsonMethod · 0.80
__init__Method · 0.80
__init__Method · 0.80
keysMethod · 0.80
valuesMethod · 0.80

Calls 2

closeMethod · 0.45
getMethod · 0.45

Tested by 10

receive_jsonMethod · 0.64
handle_requestMethod · 0.64
sendMethod · 0.64
appFunction · 0.64
authenticateMethod · 0.64
handler_that_reads_bodyFunction · 0.64
appFunction · 0.64
multi_items_appFunction · 0.64
app_with_headersFunction · 0.64