Browse Source

booking: migrate calendar read + price write to GraphQL

Booking.com's admin UI no longer uses the legacy fresa/extranet/inventory/fetch
and inventory/update REST endpoints (confirmed absent from live HAR captures);
it reads the calendar via roomInventoryQuery and writes price via the same
updateRoomDateMutation call set_availability already used.

- _fetch_month_calendar now calls roomInventoryQuery; _parse_calendar_days
  rewritten for its per-rate dates[] list shape
- set_price folds into updateRoomDateMutation's updateAvailability array
  alongside availability writes, instead of a separate REST POST
- removed _multipart_post (dead once both REST calls were migrated off it)
- DRIFT_MANIFEST and the conformance test's Booking adapter updated to match
- BOOKING_CSRF_TOKEN capture is now unused dead code (no request reads it) —
  marked deprecated rather than removed, pending a stable pilot run
- dropped the unused verify-api.ts smoke-test script and its npm entry

Verified against tests/test_services.py::test_price_roundtrip and the other
relevant @pytest.mark.live tests with ROOM_CAL_LIVE_TESTS=1 (real credentials);
two bugs the HAR-informed draft missed were caught and fixed this way: an
unused GraphQL variable in the calendar query (misclassified as an auth
failure) and a string-vs-int type mismatch on rateId in the price mutation.
ZiB 12 hours ago
parent
commit
ce2d53e80d

+ 196 - 93
room_cal/service_api/_booking.py

@@ -4,6 +4,7 @@ import logging
 import random
 import time
 from dataclasses import dataclass
+from datetime import date, timedelta
 from room_cal._utils import local_today as _local_today
 from urllib.parse import parse_qs, urlparse
 
@@ -25,6 +26,17 @@ def _is_past(d: str) -> bool:
   return d < _local_today().isoformat()
 
 
+def _dates_in_range(date_from: str, date_to: str) -> list[str]:
+  """Inclusive list of YYYY-MM-DD strings; roomInventoryQuery wants every date explicitly."""
+  start, end = date.fromisoformat(date_from), date.fromisoformat(date_to)
+  out = []
+  cur = start
+  while cur <= end:
+    out.append(cur.isoformat())
+    cur += timedelta(days=1)
+  return out
+
+
 _BASE = 'https://admin.booking.com'
 _SENDER_LABELS: dict[str, str] = {'GUEST': 'Guest', 'PARTNER': 'Host'}
 
@@ -34,6 +46,103 @@ _GQL_CLIENT_VERSION = 'cfJLLJeC'
 _GQL_TIMEOUT_MS = '60000'
 _MSG_CLIENT_VERSION = 'EQDFPbZe'
 
+# Verbatim from a live capture (data/admin.booking.com_Archive [26-11-03 16-25-07].har) — the
+# abridged first draft of this query dropped the @include directive on restrictionAdvice while
+# still declaring $includeRestrictionAdvice, which GraphQL rejects as an unused variable
+# (HTTP 400, "GRAPHQL_VALIDATION_FAILED" — wrongly auth-shaped and retried as a credential
+# failure by the base's default _auth_shaped). Keep this query text byte-for-byte matched to a
+# captured working request rather than hand-trimming fields we don't read.
+_ROOM_INVENTORY_QUERY = """query roomInventoryQuery($input: PartnerAvailabilityFetchInput!, $includeRestrictionAdvice: Boolean = true) {
+  partnerAvailability {
+    partnerAvailabilityFetch(input: $input) {
+      hotel {
+        rooms {
+          roomId
+          name
+          numGuests
+          lastUpdate
+          nrRooms
+          rateIds
+          permissions {
+            name
+            edit
+            __typename
+          }
+          dates {
+            date
+            status
+            statusReason
+            hidden
+            netBooked
+            roomsToSell
+            __typename
+          }
+          rates {
+            rateId
+            name
+            parentRateId
+            occupancies
+            derivedPrices
+            xml
+            permissions {
+              name
+              edit
+              enabled
+              __typename
+            }
+            pricePermissions {
+              occupancy
+              edit
+              __typename
+            }
+            defaults {
+              name
+              value
+              __typename
+            }
+            restrictionAdvice @include(if: $includeRestrictionAdvice) {
+              date
+              minStayThrough
+              __typename
+            }
+            dates {
+              date
+              roomsToSell
+              netBooked
+              status
+              statusReason
+              closed
+              closedOnArrival
+              closedOnDeparture
+              exactStayArrival
+              lengthOfStay
+              maxAdvanceRes
+              maxStayArrival
+              maxStayThrough
+              minAdvanceRes
+              minStayArrival
+              minStayThrough
+              policygroupId
+              priceSingleUse
+              price {
+                occupancy
+                value
+                __typename
+              }
+              __typename
+            }
+            __typename
+          }
+          __typename
+        }
+        __typename
+      }
+      __typename
+    }
+    __typename
+  }
+}"""
+
 _UPDATE_MUTATION = """mutation updateRoomDateMutation($input: PartnerAvailabilityUpdateInput) {
   partnerAvailabilityMutations {
     partnerAvailabilityUpdate(input: $input) {
@@ -182,6 +291,9 @@ class _BookingClient(ServiceClientBase):
     self._account_id = account_id
     self._room_id = room_id
     self._rate_id = rate_id
+    # Deprecated: unused since the GraphQL calendar/price migration (2026-11-17) — no request
+    # reads this anymore, _gql_headers() always sends a hardcoded placeholder instead. Captured
+    # passively for now; drop the field, _update_csrf, and BOOKING_CSRF_TOKEN once stable.
     self._csrf_token = csrf_token
     self._lang = lang
     self._rts: dict[str, int] = {}  # date -> rooms_to_sell; internal, not exposed on DayInfo
@@ -222,7 +334,10 @@ class _BookingClient(ServiceClientBase):
     self._csrf_token = env.get('BOOKING_CSRF_TOKEN', self._csrf_token)
 
   def _update_csrf(self, headers: httpx.Headers) -> None:
-    """Capture CSRF from a response header if present and not yet known."""
+    """Capture CSRF from a response header if present and not yet known.
+
+    Deprecated: see the `_csrf_token` comment in __init__. Nothing reads this value anymore.
+    """
     csrf = headers.get('x-booking-csrf')
     if csrf and csrf != 'undefined' and csrf != self._csrf_token:
       self._csrf_token = csrf
@@ -380,30 +495,28 @@ class _BookingClient(ServiceClientBase):
     _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:
+  async def _gql(self, operation_name: str, query: str, variables: dict, referer: str,
+                 extra_params: dict | None = None) -> dict:
     async def _attempt() -> httpx.Response:
-      headers = self._rest_headers(referer, extra_headers)
-      return await self._http.post(url, params=self._common_params(),
-                                   files={'request': (None, json.dumps(body))}, headers=headers)
-    r = await self._request(_attempt, op=url.removeprefix(_BASE))
-    self._update_csrf(r.headers)
-    return r.json()
-
-  async def _gql_mutate(self, variables: dict) -> dict:
-    async def _attempt() -> httpx.Response:
-      referer = self._matrix_referer()
-      params = {'view_mode': 'MATRIX', 'hotel_id': str(self._hotel_id),
-                'lang': self._lang, 'ses': self._session}
+      # params built per call so a retry after _ensure_refreshed uses the reloaded session
+      params = {'hotel_id': str(self._hotel_id), 'lang': self._lang, 'ses': self._session}
+      if extra_params:
+        params.update(extra_params)
       return await self._http.post(
         f'{_BASE}/dml/graphql.json',
         params=params,
-        json={'operationName': 'updateRoomDateMutation', 'variables': variables,
-              'extensions': {}, 'query': _UPDATE_MUTATION},
+        json={'operationName': operation_name, 'variables': variables,
+              'extensions': {}, 'query': query},
         headers=self._gql_headers(referer),
       )
-    r = await self._request(_attempt, op='updateRoomDateMutation')
+    r = await self._request(_attempt, op=operation_name)
     raw = r.json()
-    _raise_gql_errors(raw, 'updateRoomDateMutation')
+    _raise_gql_errors(raw, operation_name)
+    return raw
+
+  async def _gql_mutate(self, variables: dict) -> dict:
+    raw = await self._gql('updateRoomDateMutation', _UPDATE_MUTATION, variables,
+                          self._matrix_referer(), {'view_mode': 'MATRIX'})
     result = (raw.get('data', {}).get('partnerAvailabilityMutations', {})
                   .get('partnerAvailabilityUpdate', {}))
     if not result.get('success'):
@@ -412,58 +525,56 @@ class _BookingClient(ServiceClientBase):
     return raw
 
   def _parse_calendar_days(self, raw: dict) -> dict[str, DayInfo]:
-    hotel_data = raw.get('data', {}).get('hotel', {}).get(str(self._hotel_id), {})
-    room_data = hotel_data.get('rooms', {}).get(str(self._room_id), {})
-    rate_dates = room_data.get('rates', {}).get(str(self._rate_id), {}).get('dates', {})
+    # `hotel` is a list (one entry per requested hotelId, always length 1 for us), not an
+    # object — confirmed against a live capture.
+    hotels = ((raw.get('data') or {}).get('partnerAvailability') or {}).get('partnerAvailabilityFetch', {}).get('hotel') or []
+    rooms = hotels[0].get('rooms') if hotels else []
+    room = next((r for r in (rooms or []) if str(r.get('roomId')) == str(self._room_id)), None)
     result: dict[str, DayInfo] = {}
-    room_level_dates = room_data.get('dates', {})
-    if room_level_dates:
-      # Legacy backend: room-level dates include hidden/net_booked/rooms_to_sell
-      for d, info in room_level_dates.items():
-        price_str = rate_dates.get(d, {}).get('price', {}).get('2')
-        result[d] = DayInfo(
-          date=d,
-          price=round(float(price_str)) if price_str else None,
-          available=(info.get('hidden', 0) == 0),
-          booked=(info.get('net_booked', 0) > 0),
-        )
-        self._rts[d] = int(info.get('rooms_to_sell') or 0)
-    else:
-      # Partner availability API: only rate-level dates; status encodes availability
-      # "restricted" = open, "closed" = unavailable, "soldout" = booked
-      for d, rate_info in rate_dates.items():
-        price_str = rate_info.get('price', {}).get('2')
-        status = rate_info.get('status', '')
-        result[d] = DayInfo(
-          date=d,
-          price=round(float(price_str)) if price_str else None,
-          available=(status == 'restricted'),
-          booked=(status == 'soldout'),
-        )
-        self._rts.pop(d, None)
+    if not room:
+      return result
+    rates = room.get('rates') or []
+    rate = next((r for r in rates if str(r.get('rateId')) == str(self._rate_id)), None)
+    rate_dates = {rd['date']: rd for rd in (rate.get('dates') or [])} if rate else {}
+    for day in room.get('dates') or []:
+      d = day['date']
+      prices = rate_dates.get(d, {}).get('price') or []
+      price_val = next((p.get('value') for p in prices if str(p.get('occupancy')) == '2'), None)
+      result[d] = DayInfo(
+        date=d,
+        price=round(float(price_val)) if price_val is not None else None,
+        available=not day.get('hidden', False),
+        booked=(day.get('netBooked') or 0) > 0,
+      )
+      self._rts[d] = int(day.get('roomsToSell') or 0)
     return result
 
   async def _fetch_month_calendar(self, month: int, year: int) -> dict[str, DayInfo]:
     start, end = month_bounds(month, year)
     if _is_past(end):
       return {}
-    body = {
-      'dates': {'range': True, 'dates': [start, end]},
-      'hotel': {
-        'fields': ['rooms', 'policygroups'],
-        'rooms': {
-          'id': [self._room_id],
-          'fields': ['status', 'rooms_to_sell', 'permissions', 'rates', 'nr_rooms', 'net_booked', 'num_guests'],
-          'rates': {'fields': ['permissions', 'price', 'restrictions', 'status', 'rooms_to_sell',
-                               'policygroup_id', 'policy_overrides', 'default_policygroup_id', 'name']},
+    variables = {
+      'includeRestrictionAdvice': False,
+      'input': {
+        'roomIds': [self._room_id],
+        'hotelId': self._hotel_id,
+        'dates': _dates_in_range(start, end),
+        'rateIds': [],
+        'additional': {'includeFakePrice': True},
+        'options': {
+          'checkEmptyPrice': True,
+          'mlosStatusReason': False,
+          'checkDynamicRestrictions': True,
+          'formatPrice': True,
+          'icalDates': True,
+          'lastUpdate': True,
+          'multiUnitsCheck': True,
+          'postProcessDataPlugins': [],
         },
-        'policy': {'fields': ['info']},
       },
-      'post_process_data_plugins': ['format_price', 'multi_units_check', 'last_update', 'check_dynamic_restrictions'],
     }
-    raw = await self._multipart_post(
-      f'{_BASE}/fresa/extranet/inventory/fetch', self._monthly_referer(), body
-    )
+    raw = await self._gql('roomInventoryQuery', _ROOM_INVENTORY_QUERY, variables,
+                          self._monthly_referer(), {'view_mode': 'MONTHLY'})
     return self._parse_calendar_days(raw)
 
   def _suspect_month(self, key: tuple[int, int], result: dict[str, DayInfo]) -> bool:
@@ -632,32 +743,22 @@ class _BookingClient(ServiceClientBase):
       return
     if (cached := self._cache.get(d)) and cached.price == price:
       return
-    if not self._csrf_token:
-      raise ValueError('BOOKING_CSRF_TOKEN not available — call get_calendar() first, or run refresh-creds')
-    price_str = str(price)
-    body = {
-      'update': [{
-        'room_id': self._room_id,
-        'from_date': d,
-        'until_date': d,
-        'limit_to_weekdays': [0, 1, 2, 3, 4, 5, 6],
-        'rate_id': self._rate_id,
-        'field_name': 'price',
-        'field_value': price_str,
+    await self._gql_mutate({'input': {
+      'checkAddingAVToClosedProperty': False,
+      'hotelId': self._hotel_id,
+      'postProcessDataPlugins': ['mlos_status_reason'],
+      'preventAcav': True,
+      'statusReason': True,
+      'updateAvailability': [{
+        'fromDate': d,
+        'roomId': self._room_id,
+        'untilDate': d,
+        'limitToWeekDays': ['MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN'],
+        'rateId': int(self._rate_id),
+        'price': str(price),
         'occupancy': occupancy,
       }],
-      'modified_fields': {'price_changed': 1, 'price_changed_value': price_str,
-                          'date_selection_mode': 'CELLS'},
-      'post_process_data_plugins': ['format_price', 'last_update'],
-      'update_source': {'product': 'calendar', 'extra_data': {'new_calendar': 1, 'location': 'input'}},
-      'status_reason': 1,
-      'prevent_acav': '1',
-      'check_adding_av_to_closed_property': '0',
-    }
-    await self._multipart_post(
-      f'{_BASE}/fresa/extranet/inventory/update', self._monthly_referer(), body,
-      extra_headers={'X-Booking-CSRF': self._csrf_token},
-    )
+    }})
     self.clear_cache()
     _log.info('booking set_price date=%s price=$%d', d, price)
 
@@ -745,22 +846,24 @@ class _BookingClient(ServiceClientBase):
 DRIFT_MANIFEST = {
   'domain': 'admin.booking.com',
   'endpoints': {
-    'inventory/fetch': {
-      'url_fragment': 'fresa/extranet/inventory/fetch',
-      'hint': 'load the monthly calendar',
-    },
-    'inventory/update': {
-      'url_fragment': 'fresa/extranet/inventory/update',
-      'hint': 'change a price on any date',
-    },
     'get_reservations': {
       'url_fragment': 'fresa/extranet/calendar/get_reservations',
       'hint': 'load the reservations list',
     },
+    'roomInventoryQuery': {
+      'url_fragment': 'dml/graphql.json',
+      'operation_name': 'roomInventoryQuery',
+      'hint': 'load the monthly calendar',
+      'headers': {
+        'apollographql-client-version': _GQL_CLIENT_VERSION,
+        'x-envoy-expected-rq-timeout-ms': _GQL_TIMEOUT_MS,
+      },
+      'source': '_gql_headers()',
+    },
     'updateRoomDateMutation': {
       'url_fragment': 'dml/graphql.json',
       'operation_name': 'updateRoomDateMutation',
-      'hint': 'block or unblock a date on the calendar',
+      'hint': 'change a price on any date, or block/unblock it',
       'headers': {
         'apollographql-client-version': _GQL_CLIENT_VERSION,
         'x-envoy-expected-rq-timeout-ms': _GQL_TIMEOUT_MS,

+ 0 - 1
room_cal/service_api/typescript/package.json

@@ -10,7 +10,6 @@
     "check-credentials": "tsx scripts/check-credentials.ts",
     "dump-session": "tsx scripts/dump-session.ts",
     "intercept-session": "tsx scripts/intercept-session.ts",
-    "verify-api": "tsx scripts/verify-api.ts",
     "record-fixtures": "tsx scripts/record-fixtures.ts",
     "typecheck": "tsc --noEmit"
   },

+ 0 - 67
room_cal/service_api/typescript/scripts/verify-api.ts

@@ -1,67 +0,0 @@
-/**
- * Smoke-test live credentials by making a real API call and reporting success/failure.
- *
- * Use after refreshing credentials or after updating the client code to confirm
- * the Python API client is still working.
- *
- * Usage: tsx scripts/verify-api.ts [airbnb|booking]
- */
-import * as fs from 'fs';
-import * as path from 'path';
-import * as os from 'os';
-import { AirbnbClient } from '../airbnb/src/client';
-import { BookingClient } from '../booking/src/client';
-
-const DATA_HOME = process.env.ROOM_CAL_DATA_HOME ?? path.join(os.homedir(), '.local', 'room_cal');
-const SERVICE = process.argv[2] ?? 'all';
-
-function readEnv(service: string): Record<string, string> {
-  const envPath = path.join(DATA_HOME, `${service}.env`);
-  if (!fs.existsSync(envPath)) throw new Error(`${envPath} not found`);
-  const env: Record<string, string> = {};
-  for (const line of fs.readFileSync(envPath, 'utf8').split('\n')) {
-    const trimmed = line.trim();
-    if (!trimmed || trimmed.startsWith('#') || !trimmed.includes('=')) continue;
-    const eqIdx = trimmed.indexOf('=');
-    env[trimmed.slice(0, eqIdx)] = trimmed.slice(eqIdx + 1);
-  }
-  return env;
-}
-
-async function verifyAirbnb(): Promise<void> {
-  const env = readEnv('airbnb');
-  const client = new AirbnbClient({
-    cookies: env.AIRBNB_COOKIES ?? '',
-    listingId: env.AIRBNB_LISTING_ID ?? '',
-  });
-  const today = new Date();
-  const month = today.getMonth() + 1;
-  const year = today.getFullYear();
-  const prices = await client.getPrices(month, year);
-  console.log(`[airbnb] OK — fetched ${prices.size} days for ${year}-${String(month).padStart(2, '0')}`);
-}
-
-async function verifyBooking(): Promise<void> {
-  const env = readEnv('booking');
-  const client = new BookingClient({
-    cookies: env.BOOKING_COOKIES ?? '',
-    session: env.BOOKING_SESSION ?? '',
-    hotelId: Number(env.BOOKING_HOTEL_ID),
-    hotelAccountId: Number(env.BOOKING_ACCOUNT_ID),
-    roomId: Number(env.BOOKING_ROOM_ID),
-    rateId: env.BOOKING_RATE_ID ?? '',
-    csrfToken: env.BOOKING_CSRF_TOKEN,
-  });
-  const today = new Date().toISOString().slice(0, 10);
-  const info = await client.getDateInfo(today, today);
-  console.log(`[booking] OK — fetched ${info.size} day(s) for ${today}`);
-}
-
-async function main(): Promise<void> {
-  const tasks: Promise<void>[] = [];
-  if (SERVICE === 'all' || SERVICE === 'airbnb') tasks.push(verifyAirbnb());
-  if (SERVICE === 'all' || SERVICE === 'booking') tasks.push(verifyBooking());
-  await Promise.all(tasks);
-}
-
-main().catch(err => { console.error(`FAIL: ${err.message}`); process.exit(1); });

+ 3 - 3
tests/test_service_client_conformance.py

@@ -130,7 +130,7 @@ def _airbnb_adapter() -> ClientAdapter:
 # ── Booking.com adapter ──────────────────────────────────────────────────────
 
 def _booking_adapter() -> ClientAdapter:
-  from room_cal.service_api._booking import _BASE, BookingAuthError, _BookingClient
+  from room_cal.service_api._booking import BookingAuthError, _BookingClient
 
   def make_client(responses):
     client = _BookingClient(cookies='cookie', session='ses-1', hotel_id=1,
@@ -140,8 +140,8 @@ def _booking_adapter() -> ClientAdapter:
     return client, captured
 
   async def raw_request(client):
-    return await client._multipart_post(
-      f'{_BASE}/fresa/extranet/inventory/fetch', client._monthly_referer(), {})
+    return await client._gql('roomInventoryQuery', 'query roomInventoryQuery { x }', {},
+                             client._monthly_referer(), {'view_mode': 'MONTHLY'})
 
   def state_marker(request):
     return request.url.params.get('ses', '')