|
|
@@ -426,21 +426,43 @@ class TencentClient:
|
|
|
{"adgroup_id": adgroup_id, "begin_date": begin_date}
|
|
|
for adgroup_id in adgroup_ids
|
|
|
]
|
|
|
- params = {
|
|
|
- **self._common_params(account_id),
|
|
|
- "user_token": self._user_token(account_id),
|
|
|
- }
|
|
|
- response = self.session.post(
|
|
|
- f"{self.base_url}/adgroups/update_datetime",
|
|
|
- params=params,
|
|
|
- json={
|
|
|
- "account_id": account_id,
|
|
|
- "update_datetime_spec": specs,
|
|
|
- },
|
|
|
- timeout=self.timeout,
|
|
|
- )
|
|
|
- response.raise_for_status()
|
|
|
- data = self._check(response.json(), "update_ad_begin_dates")
|
|
|
+ try:
|
|
|
+ params = {
|
|
|
+ **self._common_params(account_id),
|
|
|
+ "user_token": self._user_token(account_id),
|
|
|
+ }
|
|
|
+ except Exception as exc:
|
|
|
+ raise TencentWriteNotSentError(str(exc)) from exc
|
|
|
+ try:
|
|
|
+ response = self.session.post(
|
|
|
+ f"{self.base_url}/adgroups/update_datetime",
|
|
|
+ params=params,
|
|
|
+ json={
|
|
|
+ "account_id": account_id,
|
|
|
+ "update_datetime_spec": specs,
|
|
|
+ },
|
|
|
+ timeout=self.timeout,
|
|
|
+ )
|
|
|
+ except requests.RequestException as exc:
|
|
|
+ raise TencentWriteOutcomeUnknownError(str(exc)) from exc
|
|
|
+ if response.status_code == 408 or response.status_code >= 500:
|
|
|
+ raise TencentWriteOutcomeUnknownError(
|
|
|
+ f"Tencent HTTP {response.status_code}: {response.text[:500]}"
|
|
|
+ )
|
|
|
+ try:
|
|
|
+ response.raise_for_status()
|
|
|
+ except requests.HTTPError as exc:
|
|
|
+ raise TencentWriteRejectedError(str(exc)) from exc
|
|
|
+ try:
|
|
|
+ payload = response.json()
|
|
|
+ except Exception as exc:
|
|
|
+ raise TencentWriteOutcomeUnknownError(
|
|
|
+ f"Tencent returned non-JSON success response: {response.text[:500]}"
|
|
|
+ ) from exc
|
|
|
+ try:
|
|
|
+ data = self._check(payload, "update_ad_begin_dates")
|
|
|
+ except Exception as exc:
|
|
|
+ raise TencentWriteRejectedError(str(exc)) from exc
|
|
|
item_failures = [
|
|
|
item
|
|
|
for item in data.get("list") or []
|
|
|
@@ -448,33 +470,39 @@ class TencentClient:
|
|
|
]
|
|
|
failed_ids = [int(value) for value in data.get("fail_id_list") or []]
|
|
|
if item_failures or failed_ids:
|
|
|
- raise RuntimeError(
|
|
|
+ raise TencentWriteRejectedError(
|
|
|
"update_ad_begin_dates partially failed: "
|
|
|
f"items={item_failures} fail_id_list={failed_ids}"
|
|
|
)
|
|
|
|
|
|
target_ids = set(adgroup_ids)
|
|
|
- last_actual: dict[int, str] = {}
|
|
|
+ last_actual: dict[str, Any] = {}
|
|
|
for attempt in range(1, self.verify_attempts + 1):
|
|
|
- ads = {
|
|
|
- int(ad.get("adgroup_id") or 0): ad
|
|
|
- for ad in self.get_ads(account_id)
|
|
|
- if int(ad.get("adgroup_id") or 0) in target_ids
|
|
|
- }
|
|
|
- last_actual = {
|
|
|
- adgroup_id: str(
|
|
|
- (ads.get(adgroup_id) or {}).get("begin_date") or ""
|
|
|
- )
|
|
|
- for adgroup_id in adgroup_ids
|
|
|
- }
|
|
|
- if all(value == begin_date for value in last_actual.values()):
|
|
|
- return [ads[adgroup_id] for adgroup_id in adgroup_ids]
|
|
|
+ try:
|
|
|
+ ads = {
|
|
|
+ int(ad.get("adgroup_id") or 0): ad
|
|
|
+ for ad in self.get_ads(account_id)
|
|
|
+ if int(ad.get("adgroup_id") or 0) in target_ids
|
|
|
+ }
|
|
|
+ begin_dates = {
|
|
|
+ adgroup_id: str(
|
|
|
+ (ads.get(adgroup_id) or {}).get("begin_date") or ""
|
|
|
+ )
|
|
|
+ for adgroup_id in adgroup_ids
|
|
|
+ }
|
|
|
+ last_actual = {"begin_dates": begin_dates}
|
|
|
+ if all(value == begin_date for value in begin_dates.values()):
|
|
|
+ return [ads[adgroup_id] for adgroup_id in adgroup_ids]
|
|
|
+ except Exception as exc:
|
|
|
+ last_actual = {"verification_error": str(exc)}
|
|
|
if attempt < self.verify_attempts:
|
|
|
time.sleep(self.verify_delay_seconds)
|
|
|
|
|
|
- raise RuntimeError(
|
|
|
- "Tencent begin_date verification failed: "
|
|
|
- f"account={account_id} expected={begin_date} actual={last_actual}"
|
|
|
+ raise PostWriteVerificationError(
|
|
|
+ account_id=account_id,
|
|
|
+ adgroup_id=adgroup_ids[0],
|
|
|
+ expected={"begin_date": begin_date},
|
|
|
+ actual=last_actual,
|
|
|
)
|
|
|
|
|
|
def get_today_ad_metrics(
|