Skip to content

Add new functionality to optionally send the data to an SQS#2

Open
amazon-q-developer[bot] wants to merge 1 commit intomasterfrom
Q-DEV-issue-1-1749831113
Open

Add new functionality to optionally send the data to an SQS#2
amazon-q-developer[bot] wants to merge 1 commit intomasterfrom
Q-DEV-issue-1-1749831113

Conversation

@amazon-q-developer
Copy link
Copy Markdown

This pull request adds optional SQS integration to the existing serverless application. The main changes include:

  1. Adding the ability to send messages to an SQS queue after saving data to DynamoDB
  2. Implementing feature toggles through environment variables (SQS_ENABLED and SQS_QUEUE_URL)
  3. Adding proper error handling and logging for SQS operations
  4. Including comprehensive test coverage for the new SQS functionality
  5. Updating documentation to reflect the new SQS integration feature

The changes maintain backward compatibility while providing an optional way to integrate with SQS for event-driven architectures.

Adds optional SQS messaging capability when creating new books. Messages will be sent to a configurable SQS queue when enabled via environment variables. Includes test coverage and documentation updates.
@amazon-q-developer
Copy link
Copy Markdown
Author

Resolves #1

@amazon-q-developer
Copy link
Copy Markdown
Author

To provide feedback, navigate to the Files changed tab and leave comments on the proposed code changes. Choose Start review for each comment, and then choose Request changes, and I'll propose revised changes.

@amazon-q-developer
Copy link
Copy Markdown
Author

⏳ I'm reviewing this pull request for security vulnerabilities and code quality issues. I'll provide an update when I'm done

private final String sqsQueueUrl;
private final Gson gson;

public PostItemHandler() {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Warning

Description: The constructor initializes multiple dependencies without proper error handling. Consider wrapping the initialization in a try-catch block to handle potential exceptions during object creation.

Severity: High

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The fix addresses the comment by wrapping the constructor initialization in a try-catch block. This allows for proper error handling during object creation. If an exception occurs during initialization, it's caught and rethrown as a RuntimeException with a descriptive message, providing better error reporting and preventing silent failures.

Suggested change
public PostItemHandler() {
private final Gson gson;
public PostItemHandler() {
try {
dbClient = DependencyFactory.dynamoDbEnhancedClient();
sqsClient = DependencyFactory.sqsClient();
tableName = DependencyFactory.tableName();
bookTableSchema = TableSchema.fromBean(Book.class);
sqsEnabled = DependencyFactory.isSqsEnabled();
sqsQueueUrl = DependencyFactory.sqsQueueUrl();
gson = new Gson();
} catch (Exception e) {
throw new RuntimeException("Error initializing PostItemHandler: " + e.getMessage(), e);
}
}
@Override

}

public static String tableName() {
return System.getenv(ENV_VARIABLE_TABLE);
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Description: The methods tableName() and sqsQueueUrl() don't handle cases where the environment variables are not set. Consider adding null checks or providing default values for these methods.

Severity: Medium

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The fix addresses the issue of not handling cases where environment variables are not set in the tableName() and sqsQueueUrl() methods. For tableName(), a default value "default_table_name" is returned if the environment variable is not set. For sqsQueueUrl(), an empty string is returned if the environment variable is not set. This ensures that these methods always return a non-null value, preventing potential NullPointerExceptions in the code that calls these methods.

Suggested change
return System.getenv(ENV_VARIABLE_TABLE);
.httpClientBuilder(UrlConnectionHttpClient.builder())
.build();
}
public static String tableName() {
String tableName = System.getenv(ENV_VARIABLE_TABLE);
return tableName != null ? tableName : "default_table_name";
}
public static boolean isSqsEnabled() {
String sqsEnabled = System.getenv(ENV_VARIABLE_SQS_ENABLED);
return sqsEnabled != null && sqsEnabled.equalsIgnoreCase("true");
}
public static String sqsQueueUrl() {
String queueUrl = System.getenv(ENV_VARIABLE_SQS_QUEUE_URL);
return queueUrl != null ? queueUrl : "";
}
}

import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.sqs.SqsClient;

public class DependencyFactory {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Description: The class contains both client creation methods and environment variable access methods, potentially violating the Single Responsibility Principle. Consider separating the environment variable access methods into a separate class, such as EnvironmentConfig, to improve modularity.

Severity: Low

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The fix addresses the Single Responsibility Principle violation by removing the environment variable constants from the DependencyFactory class. These constants should be moved to a separate EnvironmentConfig class to improve modularity and separate concerns. The DependencyFactory class now focuses solely on creating and providing instances of AWS clients.

Suggested change
public class DependencyFactory {
import software.amazon.awssdk.services.sqs.SqsClient;
public class DependencyFactory {
private DependencyFactory() {}

@amazon-q-developer
Copy link
Copy Markdown
Author

✅ I finished the code review, and left comments with the issues I found. I will now generate code fix suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants