Skip to content

Commit 55f764c

Browse files
authored
Add sqs operations (#6)
1 parent 1591845 commit 55f764c

File tree

71 files changed

+2747
-928
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+2747
-928
lines changed

.github/workflows/test.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ on:
44
pull_request:
55
paths:
66
- .github/workflows/test.yml
7-
- localstack-sdk-python/*
8-
- tests/*
9-
- packages/*
7+
- localstack-sdk-python/**
8+
- tests/**
9+
- packages/**
1010
push:
1111
branches:
1212
- main
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from localstack.sdk.aws.client import AWSClient
2+
3+
__all__ = ["AWSClient"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import json
2+
3+
from localstack.clients import BaseClient
4+
from localstack.sdk.api.aws_api import AwsApi
5+
from localstack.sdk.models import Message
6+
7+
8+
def _from_sqs_query_to_json(xml_dict: dict) -> list[Message]:
9+
"""
10+
todo: developer endpoint implements sqs-query protocol. Remove this workaround one we move them to json.
11+
"""
12+
raw_messages = (
13+
xml_dict.get("ReceiveMessageResponse", {}).get("ReceiveMessageResult", {}) or {}
14+
).get("Message", [])
15+
if isinstance(raw_messages, dict):
16+
raw_messages = [raw_messages]
17+
messages = []
18+
for msg in raw_messages:
19+
_attributes = msg.get("Attribute", [])
20+
attributes = {i["Name"]: i["Value"] for i in _attributes}
21+
_m = {
22+
"MessageId": msg.get("MessageId"),
23+
"ReceiptHandle": msg.get("ReceiptHandle"),
24+
"MD5OfBody": msg.get("MD5OfBody"),
25+
"Body": msg.get("Body"),
26+
"Attributes": attributes,
27+
}
28+
m = Message.from_dict(_m)
29+
messages.append(m)
30+
return messages
31+
32+
33+
class AWSClient(BaseClient):
34+
def __init__(self, **kwargs) -> None:
35+
super().__init__(**kwargs)
36+
self._client = AwsApi(self._api_client)
37+
38+
def list_sqs_messages(self, account_id: str, region: str, queue_name: str) -> list[Message]:
39+
response = self._client.list_sqs_messages_with_http_info(
40+
account_id=account_id, region=region, queue_name=queue_name
41+
)
42+
return _from_sqs_query_to_json(json.loads(response.raw_data))
43+
44+
def list_sqs_messages_from_queue_url(self, queue_url) -> list[Message]:
45+
response = self._client.list_all_sqs_messages_with_http_info(queue_url=queue_url)
46+
return _from_sqs_query_to_json(json.loads(response.raw_data))
47+
48+
49+
def get_default(**args) -> AwsApi:
50+
"""Return a client with a default configuration"""
51+
return AwsApi(args)

0 commit comments

Comments
 (0)