Skip to content

Commit 0e024b5

Browse files
refactor: simplify label management methods
1 parent 7c5c6f0 commit 0e024b5

4 files changed

Lines changed: 83 additions & 154 deletions

File tree

src/sap_cloud_sdk/destination/certificate_client.py

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,22 @@ def get_certificate_labels(
307307
DestinationOperationError: If an HTTP error occurs or response parsing fails.
308308
"""
309309
try:
310-
return self._get_labels(name=name, level=level)
310+
path = self._sub_path_for_level(level)
311+
resp = self._http.get(f"{API_V1}/{path}/{name}/labels")
312+
data = resp.json()
313+
if not isinstance(data, list):
314+
raise DestinationOperationError(
315+
f"expected list in labels response, got {type(data)}"
316+
)
317+
return [Label.from_dict(item) for item in data]
311318
except HttpError as e:
312319
raise DestinationOperationError(
313320
f"failed to get labels for certificate '{name}': {e}"
314321
)
322+
except DestinationOperationError:
323+
raise
324+
except Exception as e:
325+
raise DestinationOperationError(f"invalid JSON in get labels response: {e}")
315326

316327
@record_metrics(Module.DESTINATION, Operation.CERTIFICATE_UPDATE_LABELS)
317328
def update_certificate_labels(
@@ -328,8 +339,13 @@ def update_certificate_labels(
328339
HttpError: Propagated for HTTP errors.
329340
DestinationOperationError: For unexpected errors.
330341
"""
342+
resolved_level = level or Level.SUB_ACCOUNT
331343
try:
332-
self._update_labels(name=name, labels=labels, level=level)
344+
path = self._sub_path_for_level(resolved_level)
345+
self._http.put(
346+
f"{API_V1}/{path}/{name}/labels",
347+
body=[lbl.to_dict() for lbl in labels],
348+
)
333349
except HttpError:
334350
raise
335351
except Exception as e:
@@ -352,8 +368,13 @@ def patch_certificate_labels(
352368
HttpError: Propagated for HTTP errors.
353369
DestinationOperationError: For unexpected errors.
354370
"""
371+
resolved_level = level or Level.SUB_ACCOUNT
355372
try:
356-
self._patch_labels(name=name, patch=patch, level=level)
373+
path = self._sub_path_for_level(resolved_level)
374+
self._http.patch(
375+
f"{API_V1}/{path}/{name}/labels",
376+
body=patch.to_dict(),
377+
)
357378
except HttpError:
358379
raise
359380
except Exception as e:
@@ -363,40 +384,6 @@ def patch_certificate_labels(
363384

364385
# ---------- Internal helpers ----------
365386

366-
def _get_labels(self, name: str, level: Level) -> List[Label]:
367-
"""Internal helper to fetch labels for a certificate."""
368-
try:
369-
path = self._sub_path_for_level(level)
370-
resp = self._http.get(f"{API_V1}/{path}/{name}/labels")
371-
data = resp.json()
372-
if not isinstance(data, list):
373-
raise DestinationOperationError(
374-
f"expected list in labels response, got {type(data)}"
375-
)
376-
return [Label.from_dict(item) for item in data]
377-
except HttpError:
378-
raise
379-
except DestinationOperationError:
380-
raise
381-
except Exception as e:
382-
raise DestinationOperationError(f"invalid JSON in get labels response: {e}")
383-
384-
def _update_labels(self, name: str, labels: List[Label], level: Level) -> None:
385-
"""Internal helper to replace labels for a certificate."""
386-
path = self._sub_path_for_level(level)
387-
self._http.put(
388-
f"{API_V1}/{path}/{name}/labels",
389-
body=[lbl.to_dict() for lbl in labels],
390-
)
391-
392-
def _patch_labels(self, name: str, patch: PatchLabels, level: Level) -> None:
393-
"""Internal helper to add or remove labels for a certificate."""
394-
path = self._sub_path_for_level(level)
395-
self._http.patch(
396-
f"{API_V1}/{path}/{name}/labels",
397-
body=patch.to_dict(),
398-
)
399-
400387
def _get_certificate(
401388
self,
402389
name: str,

src/sap_cloud_sdk/destination/client.py

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -522,11 +522,22 @@ def get_destination_labels(
522522
DestinationOperationError: If an HTTP error occurs or response parsing fails.
523523
"""
524524
try:
525-
return self._get_labels(name=name, level=level)
525+
path = self._sub_path_for_level(level)
526+
resp = self._http.get(f"{API_V1}/{path}/{name}/labels")
527+
data = resp.json()
528+
if not isinstance(data, list):
529+
raise DestinationOperationError(
530+
f"expected list in labels response, got {type(data)}"
531+
)
532+
return [Label.from_dict(item) for item in data]
526533
except HttpError as e:
527534
raise DestinationOperationError(
528535
f"failed to get labels for destination '{name}': {e}"
529536
)
537+
except DestinationOperationError:
538+
raise
539+
except Exception as e:
540+
raise DestinationOperationError(f"invalid JSON in get labels response: {e}")
530541

531542
@record_metrics(Module.DESTINATION, Operation.DESTINATION_UPDATE_LABELS)
532543
def update_destination_labels(
@@ -543,8 +554,13 @@ def update_destination_labels(
543554
HttpError: Propagated for HTTP errors.
544555
DestinationOperationError: For unexpected errors.
545556
"""
557+
resolved_level = level or Level.SUB_ACCOUNT
546558
try:
547-
self._update_labels(name=name, labels=labels, level=level)
559+
path = self._sub_path_for_level(resolved_level)
560+
self._http.put(
561+
f"{API_V1}/{path}/{name}/labels",
562+
body=[lbl.to_dict() for lbl in labels],
563+
)
548564
except HttpError:
549565
raise
550566
except Exception as e:
@@ -567,8 +583,13 @@ def patch_destination_labels(
567583
HttpError: Propagated for HTTP errors.
568584
DestinationOperationError: For unexpected errors.
569585
"""
586+
resolved_level = level or Level.SUB_ACCOUNT
570587
try:
571-
self._patch_labels(name=name, patch=patch, level=level)
588+
path = self._sub_path_for_level(resolved_level)
589+
self._http.patch(
590+
f"{API_V1}/{path}/{name}/labels",
591+
body=patch.to_dict(),
592+
)
572593
except HttpError:
573594
raise
574595
except Exception as e:
@@ -578,40 +599,6 @@ def patch_destination_labels(
578599

579600
# ---------- Internal helpers ----------
580601

581-
def _get_labels(self, name: str, level: Level) -> List[Label]:
582-
"""Internal helper to fetch labels for a destination."""
583-
try:
584-
path = self._sub_path_for_level(level)
585-
resp = self._http.get(f"{API_V1}/{path}/{name}/labels")
586-
data = resp.json()
587-
if not isinstance(data, list):
588-
raise DestinationOperationError(
589-
f"expected list in labels response, got {type(data)}"
590-
)
591-
return [Label.from_dict(item) for item in data]
592-
except HttpError:
593-
raise
594-
except DestinationOperationError:
595-
raise
596-
except Exception as e:
597-
raise DestinationOperationError(f"invalid JSON in get labels response: {e}")
598-
599-
def _update_labels(self, name: str, labels: List[Label], level: Level) -> None:
600-
"""Internal helper to replace labels for a destination."""
601-
path = self._sub_path_for_level(level)
602-
self._http.put(
603-
f"{API_V1}/{path}/{name}/labels",
604-
body=[lbl.to_dict() for lbl in labels],
605-
)
606-
607-
def _patch_labels(self, name: str, patch: PatchLabels, level: Level) -> None:
608-
"""Internal helper to add or remove labels for a destination."""
609-
path = self._sub_path_for_level(level)
610-
self._http.patch(
611-
f"{API_V1}/{path}/{name}/labels",
612-
body=patch.to_dict(),
613-
)
614-
615602
def _get_destination(
616603
self,
617604
name: str,

src/sap_cloud_sdk/destination/fragment_client.py

Lines changed: 24 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,22 @@ def get_fragment_labels(
290290
DestinationOperationError: If an HTTP error occurs or response parsing fails.
291291
"""
292292
try:
293-
return self._get_labels(name=name, level=level)
293+
path = self._sub_path_for_level(level)
294+
resp = self._http.get(f"{API_V1}/{path}/{name}/labels")
295+
data = resp.json()
296+
if not isinstance(data, list):
297+
raise DestinationOperationError(
298+
f"expected list in labels response, got {type(data)}"
299+
)
300+
return [Label.from_dict(item) for item in data]
294301
except HttpError as e:
295302
raise DestinationOperationError(
296303
f"failed to get labels for fragment '{name}': {e}"
297304
)
305+
except DestinationOperationError:
306+
raise
307+
except Exception as e:
308+
raise DestinationOperationError(f"invalid JSON in get labels response: {e}")
298309

299310
@record_metrics(Module.DESTINATION, Operation.FRAGMENT_UPDATE_LABELS)
300311
def update_fragment_labels(
@@ -311,8 +322,13 @@ def update_fragment_labels(
311322
HttpError: Propagated for HTTP errors.
312323
DestinationOperationError: For unexpected errors.
313324
"""
325+
resolved_level = level or Level.SUB_ACCOUNT
314326
try:
315-
self._update_labels(name=name, labels=labels, level=level)
327+
path = self._sub_path_for_level(resolved_level)
328+
self._http.put(
329+
f"{API_V1}/{path}/{name}/labels",
330+
body=[lbl.to_dict() for lbl in labels],
331+
)
316332
except HttpError:
317333
raise
318334
except Exception as e:
@@ -335,8 +351,13 @@ def patch_fragment_labels(
335351
HttpError: Propagated for HTTP errors.
336352
DestinationOperationError: For unexpected errors.
337353
"""
354+
resolved_level = level or Level.SUB_ACCOUNT
338355
try:
339-
self._patch_labels(name=name, patch=patch, level=level)
356+
path = self._sub_path_for_level(resolved_level)
357+
self._http.patch(
358+
f"{API_V1}/{path}/{name}/labels",
359+
body=patch.to_dict(),
360+
)
340361
except HttpError:
341362
raise
342363
except Exception as e:
@@ -346,72 +367,6 @@ def patch_fragment_labels(
346367

347368
# ---------- Internal helpers ----------
348369

349-
def _get_labels(self, name: str, level: Level) -> List[Label]:
350-
"""Internal helper to fetch labels for a fragment.
351-
352-
Args:
353-
name: Fragment name.
354-
level: Scope to query.
355-
356-
Returns:
357-
List of labels. Returns empty list if none assigned.
358-
359-
Raises:
360-
HttpError: Propagated for HTTP errors.
361-
DestinationOperationError: If response JSON is invalid.
362-
"""
363-
try:
364-
path = self._sub_path_for_level(level)
365-
resp = self._http.get(f"{API_V1}/{path}/{name}/labels")
366-
data = resp.json()
367-
if not isinstance(data, list):
368-
raise DestinationOperationError(
369-
f"expected list in labels response, got {type(data)}"
370-
)
371-
return [Label.from_dict(item) for item in data]
372-
except HttpError:
373-
raise
374-
except DestinationOperationError:
375-
raise
376-
except Exception as e:
377-
raise DestinationOperationError(f"invalid JSON in get labels response: {e}")
378-
379-
def _update_labels(self, name: str, labels: List[Label], level: Level) -> None:
380-
"""Internal helper to replace labels for a fragment.
381-
382-
Args:
383-
name: Fragment name.
384-
labels: Labels to set.
385-
level: Scope to target.
386-
387-
Raises:
388-
HttpError: Propagated for HTTP errors.
389-
DestinationOperationError: For unexpected errors.
390-
"""
391-
path = self._sub_path_for_level(level)
392-
self._http.put(
393-
f"{API_V1}/{path}/{name}/labels",
394-
body=[lbl.to_dict() for lbl in labels],
395-
)
396-
397-
def _patch_labels(self, name: str, patch: PatchLabels, level: Level) -> None:
398-
"""Internal helper to add or remove labels for a fragment.
399-
400-
Args:
401-
name: Fragment name.
402-
patch: PatchLabels with action and labels.
403-
level: Scope to target.
404-
405-
Raises:
406-
HttpError: Propagated for HTTP errors.
407-
DestinationOperationError: For unexpected errors.
408-
"""
409-
path = self._sub_path_for_level(level)
410-
self._http.patch(
411-
f"{API_V1}/{path}/{name}/labels",
412-
body=patch.to_dict(),
413-
)
414-
415370
def _get_fragment(
416371
self,
417372
name: str,

0 commit comments

Comments
 (0)