Skip to content

Commit dc6ebda

Browse files
[merchant-api] fix: parse API response correctly
* convert to camelCase fields
1 parent 423662d commit dc6ebda

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

libs/garf_community/google/merchant/garf_merchant_api/api_clients.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def get_response(
4646
response = client.search(request=merchant_request)
4747
results = []
4848
for page in response:
49-
for result in page.get('results'):
50-
results.append(result)
49+
for rows in page.get('results'):
50+
for _, row in rows.items():
51+
results.append(row)
5152
return api_clients.GarfApiResponse(results=results)

libs/garf_community/google/merchant/garf_merchant_api/query_editor.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,16 @@
1313
# limitations under the License.
1414
"""Defines MerchantQuery."""
1515

16+
from typing_extensions import Self
17+
1618
from garf_core import query_editor
1719

1820

21+
def _to_camel_case(field: str) -> str:
22+
first, *others = field.split('_')
23+
return ''.join([first.lower(), *map(str.title, others)])
24+
25+
1926
class MerchantApiQuery(query_editor.QuerySpecification):
2027
"""Query to Knowledge Graph Search api."""
2128

@@ -28,3 +35,12 @@ def __init__(
2835
) -> None:
2936
"""Initializes MerchantApiQuery."""
3037
super().__init__(text, title, args, **kwargs)
38+
39+
def extract_fields(self) -> Self:
40+
for line in self._extract_query_lines():
41+
line_elements = query_editor.ExtractedLineElements.from_query_line(line)
42+
if field := line_elements.field:
43+
self.query.fields.append(
44+
_to_camel_case(field) if '_' in field else field
45+
)
46+
return self

0 commit comments

Comments
 (0)