Skip to content

Commit e2f1156

Browse files
committed
trial-1
1 parent 1e7004b commit e2f1156

File tree

1 file changed

+105
-0
lines changed
  • packages/templates/clients/websocket/python/template

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { File, Text } from '@asyncapi/generator-react-sdk';
2+
import { getClientName, getServer, getServerUrl, getInfo, getTitle } from '@asyncapi/generator-helpers';
3+
import { AvailableOperations } from '../components/AvailableOperations';
4+
5+
export default function({ asyncapi, params }) {
6+
const server = getServer(asyncapi.servers(), params.server);
7+
const info = getInfo(asyncapi);
8+
const clientName = getClientName(asyncapi, params.appendClientSuffix, params.customClientName);
9+
const title = getTitle(asyncapi);
10+
const serverUrl = getServerUrl(server);
11+
12+
const operations = asyncapi.operations().all();
13+
14+
return (
15+
<File name="README.md">
16+
<Text newLines={2}>
17+
{`# ${title} (Python Client)
18+
19+
## Overview
20+
21+
${info.description() || `A WebSocket client for ${title} written in Python.`}
22+
23+
- **Version:** ${info.version()}
24+
- **URL:** ${serverUrl}
25+
26+
## Installation
27+
28+
Install dependencies:
29+
30+
\`\`\`bash
31+
pip install -r requirements.txt
32+
\`\`\`
33+
34+
35+
## Client API Reference
36+
37+
\`\`\`python
38+
from ${params.clientFileName.replace('.py', '')} import ${clientName}
39+
ws_client = ${clientName}()
40+
\`\`\`
41+
42+
Here, ws_client is an instance of the \`${clientName}\` class.
43+
### Core Methods
44+
45+
#### \`connect()\`
46+
Establishes a WebSocket connection to the server.
47+
48+
#### \`register_message_handler(handler_function)\`
49+
Registers a callback to handle incoming messages.
50+
- **Parameter:** \`handler_function\` - This function takes a parameter \`message\` (string).
51+
52+
#### \`register_error_handler(handler_function)\`
53+
Registers a callback to handle WebSocket errors.
54+
- **Parameter:** \`handler_function\` - This function takes a parameter \`error\` (Exception or object).
55+
56+
#### \`close()\`
57+
Closes the WebSocket connection.`}
58+
</Text>
59+
<AvailableOperations operations={operations} />
60+
<Text newLines={2}>
61+
{`## Testing the client
62+
63+
\`\`\`python
64+
from ${params.clientFileName.replace('.py', '')} import ${clientName}
65+
ws_client = ${clientName}()
66+
67+
# Example of custom message handler
68+
69+
def my_handler(message):
70+
print('====================')
71+
print('Just proving I got the message in my_handler:', message)
72+
print('====================')
73+
74+
# Example of custom error handler
75+
76+
def my_error_handler(error):
77+
print('Errors from Websocket:', getattr(error, 'message', str(error)))
78+
79+
async def main():
80+
ws_client.register_message_handler(my_handler)
81+
ws_client.register_error_handler(my_error_handler)
82+
try:
83+
await ws_client.connect()
84+
# Loop to send messages every 5 seconds
85+
import asyncio
86+
interval = 5 # seconds
87+
message = 'Hello, Echo!'
88+
while True:
89+
try:
90+
await ws_client.send_echo_message(message)
91+
except Exception as error:
92+
print('Error while sending message:', error)
93+
await asyncio.sleep(interval)
94+
except Exception as error:
95+
print('Failed to connect to WebSocket:', getattr(error, 'message', str(error)))
96+
97+
import asyncio
98+
asyncio.run(main())
99+
\`\`\`
100+
101+
`}
102+
</Text>
103+
</File>
104+
);
105+
}

0 commit comments

Comments
 (0)