33"""
44from pinterest .utils .sdk_exceptions import SdkException
55
6+
7+ def _get_field (obj , field ):
8+ """
9+ Read `field` off either a raw dict response or a generated model object
10+ """
11+ if isinstance (obj , dict ):
12+ return obj .get (field )
13+ return getattr (obj , field , None )
14+
15+
16+ def _get_first_exception (response ):
17+ """
18+ Return the first exception reported in `response`, or None if there is none.
19+
20+ Bulk endpoints return one element per requested entity. Depending on the endpoint that
21+ element's `exceptions` field is either a list of exceptions (campaigns, ad groups) or a
22+ single exception object (ads).
23+ """
24+ items = _get_field (response , 'items' )
25+ if not items :
26+ return None
27+
28+ exceptions = _get_field (items [0 ], 'exceptions' )
29+ if not exceptions :
30+ return None
31+
32+ if isinstance (exceptions , list ):
33+ return exceptions [0 ]
34+ return exceptions
35+
36+
637def verify_api_response (response ) -> bool :
7- # pylint: disable=too-many-boolean-expressions
838 """
939 Verify that there are no errors in `response` received from api
1040
@@ -14,36 +44,16 @@ def verify_api_response(response) -> bool:
1444 Returns:
1545 bool: If the `response` is without any exceptions
1646 """
17- if isinstance (response , dict ):
18- if (
19- response .get ('items' )
20- and len (response .get ('items' )) > 0
21- and response .get ('items' )[0 ].get ('exceptions' )
22- and isinstance (response .get ('items' )[0 ].get ('exceptions' ), list )
23- and len (response .get ('items' )[0 ].get ('exceptions' )) > 0
24- and response .get ('items' )[0 ].get ('exceptions' )[0 ].get ('code' )
25- and response .get ('items' )[0 ].get ('exceptions' )[0 ].get ('message' )
26- ): # pylint: disable-msg=too-many-boolean-expressions
27- raise SdkException (
28- status = f"Failed with code { response .get ('items' )[0 ].get ('exceptions' )[0 ].get ('code' )} " ,
29- reason = response .get ('items' )[0 ].get ('exceptions' )[0 ].get ('message' )
30- )
31- else :
32- if (
33- hasattr (response , "items" )
34- and response .items
35- and len (response .items ) > 0
36- and hasattr (response .items [0 ], "exceptions" )
37- and response .items [0 ].exceptions
38- and isinstance (response .items [0 ].exceptions , list )
39- and len (response .items [0 ].exceptions ) > 0
40- and hasattr (response .items [0 ].exceptions [0 ], "code" )
41- and response .items [0 ].exceptions [0 ].code
42- and hasattr (response .items [0 ].exceptions [0 ], "message" )
43- and response .items [0 ].exceptions [0 ].message
44- ):
45- raise SdkException (
46- status = f"Failed with code { response .items [0 ].exceptions [0 ].code } " ,
47- reason = response .items [0 ].exceptions [0 ].message
48- )
47+ exception = _get_first_exception (response )
48+ if exception is None :
49+ return True
50+
51+ code = _get_field (exception , 'code' )
52+ message = _get_field (exception , 'message' )
53+ if code and message :
54+ raise SdkException (
55+ status = f"Failed with code { code } " ,
56+ reason = message ,
57+ )
58+
4959 return True
0 commit comments