Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/components/CoreMethods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Text } from '@asyncapi/generator-react-sdk';

export function CoreMethods({ language }) {
const msgHandler =
language === 'python'
? 'register_message_handler(handler_function)'
: 'registerMessageHandler(handlerFunction)';

const errHandler =
language === 'python'
? 'register_error_handler(handler_function)'
: 'registerErrorHandler(handlerFunction)';

return (
<Text newLines={2}>
{`## API

### \`connect()\`
Establishes a WebSocket connection.

### \`${msgHandler}\`
Registers a callback for incoming messages.

### \`${errHandler}\`
Registers a callback for connection errors.

### \`close()\`
Closes the WebSocket connection.
`}
</Text>
);
}
11 changes: 11 additions & 0 deletions packages/components/Info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { getClientName, getServer, getServerUrl, getInfo, getTitle } from '@asyncapi/generator-helpers';

export function BaseInfo(asyncapi, params) {
const server = getServer(asyncapi.servers(), params.server);
const info = getInfo(asyncapi);
const clientName = getClientName(asyncapi, params.appendClientSuffix, params.customClientName);
const title = getTitle(asyncapi);
const serverUrl = getServerUrl(server);

return { server, info, clientName, title, serverUrl };
}
16 changes: 16 additions & 0 deletions packages/components/Installation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Text } from '@asyncapi/generator-react-sdk';

export function Installation() {
return (
<Text newLines={2}>
{`## Installation

Install dependencies:

\`\`\`bash
pip install -r requirements.txt
\`\`\`
`}
</Text>
);
}
16 changes: 16 additions & 0 deletions packages/components/Overview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Text } from '@asyncapi/generator-react-sdk';

export function Overview({ info, title, serverUrl }) {
return (
<Text newLines={2}>
{`## Overview

${info.description() || `A WebSocket client for ${title}.`}

- **Version:** ${info.version()}
- **Server URL:** ${serverUrl}
`}
</Text>
);
}

46 changes: 46 additions & 0 deletions packages/components/Usage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Text } from '@asyncapi/generator-react-sdk';

export function Usage({ clientName, clientFileName, language }) {
let snippet = '';

if (language === 'python') {
snippet = `
from ${clientFileName.replace('.py', '')} import ${clientName}

ws_client = ${clientName}()

async def main():
await ws_client.connect()
# use ws_client to send/receive messages
await ws_client.close()
`;
} else if (language === 'javascript') {
snippet = `
const ${clientName} = require('./${clientFileName.replace('.js', '')}');
const wsClient = new ${clientName}();

async function main() {
try {
await wsClient.connect();
// use wsClient to send/receive messages
await wsClient.close();
} catch (error) {
console.error('Failed to connect:', error);
}
}

main();
`;
Comment on lines +6 to +33
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Python usage example never actually runs.

Line [12] defines async def main() but nothing ever drives the event loop—users copying the snippet get no connection attempt. Add the missing asyncio import plus asyncio.run(main()) (or equivalent) so the example is executable.

-    snippet = `
-from ${clientFileName.replace('.py', '')} import ${clientName}
+    snippet = `
+import asyncio
+from ${clientFileName.replace('.py', '')} import ${clientName}
 
 ws_client = ${clientName}()
 
 async def main():
     await ws_client.connect()
     # use ws_client to send/receive messages
     await ws_client.close()
+
+if __name__ == "__main__":
+    asyncio.run(main())
 `;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (language === 'python') {
snippet = `
from ${clientFileName.replace('.py', '')} import ${clientName}
ws_client = ${clientName}()
async def main():
await ws_client.connect()
# use ws_client to send/receive messages
await ws_client.close()
`;
} else if (language === 'javascript') {
snippet = `
const ${clientName} = require('./${clientFileName.replace('.js', '')}');
const wsClient = new ${clientName}();
async function main() {
try {
await wsClient.connect();
// use wsClient to send/receive messages
await wsClient.close();
} catch (error) {
console.error('Failed to connect:', error);
}
}
main();
`;
if (language === 'python') {
snippet = `
import asyncio
from ${clientFileName.replace('.py', '')} import ${clientName}
ws_client = ${clientName}()
async def main():
await ws_client.connect()
# use ws_client to send/receive messages
await ws_client.close()
if __name__ == "__main__":
asyncio.run(main())
`;
} else if (language === 'javascript') {
snippet = `
const ${clientName} = require('./${clientFileName.replace('.js', '')}');
const wsClient = new ${clientName}();
async function main() {
try {
await wsClient.connect();
// use wsClient to send/receive messages
await wsClient.close();
} catch (error) {
console.error('Failed to connect:', error);
}
}
main();
`;
🤖 Prompt for AI Agents
In packages/components/Usage.js around lines 6 to 33, the Python example defines
async def main() but never runs it; update the snippet to import asyncio at the
top of the Python block and add a call to asyncio.run(main()) (or equivalent) at
the end so the example actually drives the event loop and performs the
connect/close sequence when copied by users.

}

return (
<Text newLines={2}>
{`## Usage

\`\`\`${language}
${snippet.trim()}
\`\`\`
`}
</Text>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export function AvailableOperations({ operations }) {
return (
<>
<Text newLines={2}>### Available Operations</Text>
{operations.map((operation) => (
<Text newLines={2}>
{operations.map((operation, index) => (
<Text key={index} newLines={2}>
<OperationHeader operation={operation} />
<MessageExamples operation={operation} />
</Text>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,106 +1,30 @@
import { File, Text } from '@asyncapi/generator-react-sdk';
import { getClientName, getServer, getServerUrl, getInfo, getTitle } from '@asyncapi/generator-helpers';
import { BaseInfo } from '../../../../../components/Info';
import { Overview } from '../../../../../components/Overview';
import { CoreMethods } from '../../../../../components/CoreMethods';
import { Usage } from '../../../../../components/Usage';
import { AvailableOperations } from '../components/AvailableOperations';

export default function({ asyncapi, params }) {
const server = getServer(asyncapi.servers(), params.server);
const info = getInfo(asyncapi);
const clientName = getClientName(asyncapi, params.appendClientSuffix, params.customClientName);
const title = getTitle(asyncapi);
const serverUrl = getServerUrl(server);
const { info, clientName, title, serverUrl } = BaseInfo(asyncapi, params);

const operations = asyncapi.operations().all();

return (
<File name="README.md">
<Text newLines={2}>
{`# ${title}

## Overview

${info.description() || `A WebSocket client for ${title}.`}

- **Version:** ${info.version()}
- **URL:** ${serverUrl}


## Client API Reference

\`\`\`javascript
const ${clientName} = require('./${params.clientFileName.replace('.js', '')}');
const wsClient = new ${clientName}();
\`\`\`

Here the wsClient is an instance of the \`${clientName}\` class.
### Core Methods

#### \`connect()\`
Establishes a WebSocket connection to the server.

#### \`registerMessageHandler(handlerFunction)\`
Registers a callback to handle incoming messages.
- **Parameter:** \`handlerFunction\` - This Function takes a parameter \`message\` which is a string.

#### \`registerErrorHandler(handlerFunction)\`
Registers a callback to handle WebSocket errors.
- **Parameter:** \`handlerFunction\` - This Function takes a parameter \`error\` which is an object

#### \`close()\`
Closes the WebSocket connection.`}
</Text>
<AvailableOperations operations={operations} />
<Text newLines={2}>
{`## Testing the client

\`\`\`javascript
const ${clientName} = require('./${params.clientFileName.replace('.js', '')}');
const wsClient = new ${clientName}();


// Example of how custom message handler that operates on incoming messages can look like

function myHandler(message) {
console.log('====================');
console.log('Just proving I got the message in myHandler:', message);
console.log('====================');
}

// Example of custom error handler

function myErrorHandler(error) {
console.error('Errors from Websocket:', error.message);
}

async function main() {
wsClient.registerMessageHandler(myHandler);
wsClient.registerErrorHandler(myErrorHandler);

try {
await wsClient.connect();

// Loop to send messages every 5 seconds
const interval = 5000; // 5 seconds
const message = 'Hello, Echo!';

while (true) {
try {
await wsClient.sendEchoMessage(message);
} catch (error) {
console.error('Error while sending message:', error);
}
// Wait for the interval before sending the next message
await new Promise(resolve => setTimeout(resolve, interval));
}
} catch (error) {
console.error('Failed to connect to WebSocket:', error.message);
}
}

main();
\`\`\`

`}
</Text>
<>
<Text newLines={2}>
{`# ${title}`}
</Text>
<Overview info={info} title={title} serverUrl={serverUrl} />
<Usage
clientName={clientName}
clientFileName={params.clientFileName}
language="javascript"
/>
<CoreMethods language="javascript" />
<AvailableOperations operations={operations} />
</>
</File>
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { File, Text } from '@asyncapi/generator-react-sdk';
import { BaseInfo } from '../../../../../components/Info';
import { Overview } from '../../../../../components/Overview';
import { Installation } from '../../../../../components/Installation';
import { Usage } from '../../../../../components/Usage';
import { CoreMethods} from '../../../../../components/CoreMethods';

export default function({ asyncapi, params }) {
const { info, clientName, title, serverUrl } = BaseInfo(asyncapi, params);

return (
<File name="README.md">
<Text newLines={2}>{`# ${title}`}</Text>
<Overview info={info} title={title} serverUrl={serverUrl} />
<Installation />
<Usage clientName={clientName} clientFileName={params.clientFileName} language="python" />
<CoreMethods language="python" />
</File>
);
}
Loading