Explorar el Código

service_api: extract shared _raise_gql_errors for Booking GQL responses

_msg_gql and _gql_mutate had drifted copies of the errors -> RuntimeError
dance (one logged, one didn't); both now log and raise identically.
ZiB hace 3 meses
padre
commit
2eb7be9b30
Se han modificado 1 ficheros con 10 adiciones y 7 borrados
  1. 10 7
      room_cal/service_api/_booking.py

+ 10 - 7
room_cal/service_api/_booking.py

@@ -151,6 +151,14 @@ def _page_view_id() -> str:
   return ''.join(random.choices('0123456789abcdef', k=16))
 
 
+def _raise_gql_errors(raw: dict, op: str) -> None:
+  """Raise RuntimeError when a GraphQL response body carries top-level errors."""
+  if raw.get('errors'):
+    _log.warning('booking %s gql errors: %s', op, json.dumps(raw['errors']))
+    msgs = ', '.join(e.get('message', '') for e in raw['errors'])
+    raise RuntimeError(f'GraphQL error: {msgs}')
+
+
 @register_service('booking')
 class _BookingClient(ServiceClientBase):
   service_name = 'Booking.com'
@@ -371,10 +379,7 @@ class _BookingClient(ServiceClientBase):
         )
     r = await self._request(_do_request, op=operation_name)
     raw = r.json()
-    if raw.get('errors'):
-      _log.warning('booking _msg_gql %s gql errors: %s', operation_name, json.dumps(raw['errors']))
-      msgs = ', '.join(e.get('message', '') for e in raw['errors'])
-      raise RuntimeError(f'GraphQL error: {msgs}')
+    _raise_gql_errors(raw, operation_name)
     return raw
 
   async def _multipart_post(self, url: str, referer: str, body: dict, extra_headers: dict | None = None) -> dict:
@@ -400,9 +405,7 @@ class _BookingClient(ServiceClientBase):
       )
     r = await self._request(_attempt, op='updateRoomDateMutation')
     raw = r.json()
-    if raw.get('errors'):
-      msgs = ', '.join(e.get('message', '') for e in raw['errors'])
-      raise RuntimeError(f'GraphQL error: {msgs}')
+    _raise_gql_errors(raw, 'updateRoomDateMutation')
     result = (raw.get('data', {}).get('partnerAvailabilityMutations', {})
                   .get('partnerAvailabilityUpdate', {}))
     if not result.get('success'):