|
| 1 | +from xai_components.base import InArg, OutArg, Component, xai_component |
| 2 | +import boto3 |
| 3 | + |
| 4 | + |
| 5 | +@xai_component |
| 6 | +class SQSSendMessage(Component): |
| 7 | + """Component to send a message to an SQS queue. |
| 8 | +
|
| 9 | + ##### inPorts: |
| 10 | + - queue_url (str): The URL of the SQS queue. |
| 11 | + - message_body (str): The message body to send. |
| 12 | + |
| 13 | + ##### outPorts: |
| 14 | + - message_id (str): The ID of the sent message. |
| 15 | + """ |
| 16 | + queue_url: InArg[str] |
| 17 | + message_body: InArg[str] |
| 18 | + message_id: OutArg[str] |
| 19 | + |
| 20 | + def execute(self, ctx) -> None: |
| 21 | + sqs = boto3.client('sqs') |
| 22 | + response = sqs.send_message( |
| 23 | + QueueUrl=self.queue_url.value, |
| 24 | + MessageBody=self.message_body.value |
| 25 | + ) |
| 26 | + self.message_id.value = response['MessageId'] |
| 27 | + |
| 28 | + |
| 29 | +@xai_component |
| 30 | +class SQSReceiveMessage(Component): |
| 31 | + """Component to receive messages from an SQS queue. |
| 32 | +
|
| 33 | + ##### inPorts: |
| 34 | + - queue_url (str): The URL of the SQS queue. |
| 35 | + |
| 36 | + ##### outPorts: |
| 37 | + - messages (list): A list of received messages. |
| 38 | + """ |
| 39 | + queue_url: InArg[str] |
| 40 | + messages: OutArg[list] |
| 41 | + |
| 42 | + def execute(self, ctx) -> None: |
| 43 | + sqs = boto3.client('sqs') |
| 44 | + response = sqs.receive_message( |
| 45 | + QueueUrl=self.queue_url.value, |
| 46 | + MaxNumberOfMessages=10, |
| 47 | + WaitTimeSeconds=10 |
| 48 | + ) |
| 49 | + self.messages.value = response.get('Messages', []) |
| 50 | + |
| 51 | + |
| 52 | +@xai_component |
| 53 | +class SQSDeleteMessage(Component): |
| 54 | + """Component to delete a message from an SQS queue. |
| 55 | +
|
| 56 | + ##### inPorts: |
| 57 | + - queue_url (str): The URL of the SQS queue. |
| 58 | + - receipt_handle (str): The receipt handle of the message to delete. |
| 59 | + """ |
| 60 | + queue_url: InArg[str] |
| 61 | + receipt_handle: InArg[str] |
| 62 | + |
| 63 | + def execute(self, ctx) -> None: |
| 64 | + sqs = boto3.client('sqs') |
| 65 | + sqs.delete_message( |
| 66 | + QueueUrl=self.queue_url.value, |
| 67 | + ReceiptHandle=self.receipt_handle.value |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +@xai_component |
| 72 | +class S3UploadFile(Component): |
| 73 | + """Component to upload a file to an S3 bucket. |
| 74 | +
|
| 75 | + ##### inPorts: |
| 76 | + - bucket_name (str): The name of the S3 bucket. |
| 77 | + - file_path (str): The local path of the file to upload. |
| 78 | + - object_name (str): The name to assign to the file in S3. |
| 79 | + |
| 80 | + ##### outPorts: |
| 81 | + - response (dict): The response from the S3 upload operation. |
| 82 | + """ |
| 83 | + bucket_name: InArg[str] |
| 84 | + file_path: InArg[str] |
| 85 | + object_name: InArg[str] |
| 86 | + response: OutArg[dict] |
| 87 | + |
| 88 | + def execute(self, ctx) -> None: |
| 89 | + s3 = boto3.client('s3') |
| 90 | + response = s3.upload_file( |
| 91 | + Filename=self.file_path.value, |
| 92 | + Bucket=self.bucket_name.value, |
| 93 | + Key=self.object_name.value |
| 94 | + ) |
| 95 | + self.response.value = response |
| 96 | + |
| 97 | + |
| 98 | +@xai_component |
| 99 | +class S3DownloadFile(Component): |
| 100 | + """Component to download a file from an S3 bucket. |
| 101 | +
|
| 102 | + ##### inPorts: |
| 103 | + - bucket_name (str): The name of the S3 bucket. |
| 104 | + - object_name (str): The name of the file in S3. |
| 105 | + - file_path (str): The local path to save the downloaded file. |
| 106 | + |
| 107 | + ##### outPorts: |
| 108 | + - response (dict): The response from the S3 download operation. |
| 109 | + """ |
| 110 | + bucket_name: InArg[str] |
| 111 | + object_name: InArg[str] |
| 112 | + file_path: InArg[str] |
| 113 | + response: OutArg[dict] |
| 114 | + |
| 115 | + def execute(self, ctx) -> None: |
| 116 | + s3 = boto3.client('s3') |
| 117 | + response = s3.download_file( |
| 118 | + Bucket=self.bucket_name.value, |
| 119 | + Key=self.object_name.value, |
| 120 | + Filename=self.file_path.value |
| 121 | + ) |
| 122 | + self.response.value = response |
| 123 | + |
| 124 | + |
| 125 | +@xai_component |
| 126 | +class S3ListObjects(Component): |
| 127 | + """Component to list objects in an S3 bucket. |
| 128 | +
|
| 129 | + ##### inPorts: |
| 130 | + - bucket_name (str): The name of the S3 bucket. |
| 131 | + |
| 132 | + ##### outPorts: |
| 133 | + - objects (list): A list of objects in the specified S3 bucket. |
| 134 | + """ |
| 135 | + bucket_name: InArg[str] |
| 136 | + objects: OutArg[list] |
| 137 | + |
| 138 | + def execute(self, ctx) -> None: |
| 139 | + s3 = boto3.client('s3') |
| 140 | + response = s3.list_objects_v2(Bucket=self.bucket_name.value) |
| 141 | + self.objects.value = response.get('Contents', []) |
| 142 | + |
| 143 | + |
| 144 | +@xai_component |
| 145 | +class DynamoDBPutItem(Component): |
| 146 | + """Component to insert an item into a DynamoDB table. |
| 147 | +
|
| 148 | + ##### inPorts: |
| 149 | + - table_name (str): The name of the table. |
| 150 | + - item (dict): The item to insert (e.g., {'id': {'S': '123'}, 'name': {'S': 'John'}}). |
| 151 | + |
| 152 | + ##### outPorts: |
| 153 | + - response (dict): The response from the PutItem operation. |
| 154 | + """ |
| 155 | + table_name: InArg[str] |
| 156 | + item: InArg[dict] |
| 157 | + response: OutArg[dict] |
| 158 | + |
| 159 | + def execute(self, ctx) -> None: |
| 160 | + dynamodb = boto3.client('dynamodb') |
| 161 | + response = dynamodb.put_item( |
| 162 | + TableName=self.table_name.value, |
| 163 | + Item=self.item.value |
| 164 | + ) |
| 165 | + self.response.value = response |
| 166 | + |
| 167 | + |
| 168 | +@xai_component |
| 169 | +class DynamoDBGetItem(Component): |
| 170 | + """Component to retrieve an item from a DynamoDB table. |
| 171 | +
|
| 172 | + ##### inPorts: |
| 173 | + - table_name (str): The name of the table. |
| 174 | + - key (dict): The key of the item to retrieve (e.g., {'id': {'S': '123'}}). |
| 175 | + |
| 176 | + ##### outPorts: |
| 177 | + - item (dict): The retrieved item. |
| 178 | + """ |
| 179 | + table_name: InArg[str] |
| 180 | + key: InArg[dict] |
| 181 | + item: OutArg[dict] |
| 182 | + |
| 183 | + def execute(self, ctx) -> None: |
| 184 | + dynamodb = boto3.client('dynamodb') |
| 185 | + response = dynamodb.get_item( |
| 186 | + TableName=self.table_name.value, |
| 187 | + Key=self.key.value |
| 188 | + ) |
| 189 | + self.item.value = response.get('Item', {}) |
| 190 | + |
| 191 | + |
| 192 | +@xai_component |
| 193 | +class DynamoDBDeleteItem(Component): |
| 194 | + """Component to delete an item from a DynamoDB table. |
| 195 | +
|
| 196 | + ##### inPorts: |
| 197 | + - table_name (str): The name of the table. |
| 198 | + - key (dict): The key of the item to delete (e.g., {'id': {'S': '123'}}). |
| 199 | + |
| 200 | + ##### outPorts: |
| 201 | + - response (dict): The response from the DeleteItem operation. |
| 202 | + """ |
| 203 | + table_name: InArg[str] |
| 204 | + key: InArg[dict] |
| 205 | + response: OutArg[dict] |
| 206 | + |
| 207 | + def execute(self, ctx) -> None: |
| 208 | + dynamodb = boto3.client('dynamodb') |
| 209 | + response = dynamodb.delete_item( |
| 210 | + TableName=self.table_name.value, |
| 211 | + Key=self.key.value |
| 212 | + ) |
| 213 | + self.response.value = response |
0 commit comments