22
33from __future__ import annotations
44
5+ import contextlib
56import typing as t
67from datetime import datetime , timezone
78from pathlib import Path
@@ -55,8 +56,8 @@ def http_headers(self) -> dict:
5556 def get_next_page_token (
5657 self ,
5758 response : requests .Response ,
58- previous_token : t .Any | None ,
59- ) -> t .Any | None :
59+ previous_token : t .Any | None , # noqa: ANN401
60+ ) -> t .Any | None : # noqa: ANN401
6061 """Return a token for identifying next page or None if no more pages."""
6162 # If pagination is required, return a token which can be used to get the
6263 # next page. If this is the final page, return "None" to end the
@@ -67,20 +68,19 @@ def get_next_page_token(
6768
6869 elements = resp_json .get ("elements" )
6970
70- if elements is not None :
71- if len (elements ) == 0 or len (elements ) == previous_token + 1 :
72- return None
73- else :
71+ if elements is None :
7472 page = resp_json
75- if len (page ) == 0 or len ( page ) == previous_token + 1 :
73+ if len (page ) in [ 0 , previous_token + 1 ] :
7674 return None
7775
76+ elif len (elements ) in [0 , previous_token + 1 ]:
77+ return None
7878 return previous_token + 1
7979
8080 def get_url_params (
8181 self ,
8282 context : dict | None , # noqa: ARG002
83- next_page_token : t .Any | None ,
83+ next_page_token : t .Any | None , # noqa: ANN401
8484 ) -> dict [str , t .Any ]:
8585 """Return a dictionary of values to be used in URL parameterization.
8686
@@ -100,7 +100,7 @@ def get_url_params(
100100
101101 return params
102102
103- def parse_response ( # noqa: PLR0912
103+ def parse_response (
104104 self ,
105105 response : requests .Response ,
106106 ) -> t .Iterable [dict ]:
@@ -117,72 +117,58 @@ def parse_response( # noqa: PLR0912
117117 results = resp_json ["elements" ]
118118 try :
119119 columns = results [0 ]
120- except : # noqa: E722
120+ except Exception : # noqa: BLE001
121121 columns = results
122- pass
123- try :
124- created_time = (
125- columns .get ("changeAuditStamps" ).get ("created" ).get ("time" )
126- )
127- last_modified_time = (
128- columns .get ("changeAuditStamps" ).get ("lastModified" ).get ("time" )
129- )
130- columns ["created_time" ] = datetime .fromtimestamp (
131- int (created_time ) / 1000 ,
132- tz = UTC ,
133- ).isoformat ()
134- columns ["last_modified_time" ] = datetime .fromtimestamp (
135- int (last_modified_time ) / 1000 ,
136- tz = UTC ,
137- ).isoformat ()
138- except : # noqa: E722, S110
139- pass
122+ with contextlib .suppress (Exception ):
123+ self ._add_datetime_columns (columns )
124+
140125 else :
141126 results = resp_json
142127 try :
143128 columns = results
144- created_time = (
145- columns .get ("changeAuditStamps" ).get ("created" ).get ("time" )
146- )
147- last_modified_time = (
148- columns .get ("changeAuditStamps" ).get ("lastModified" ).get ("time" )
149- )
150- columns ["created_time" ] = datetime .fromtimestamp (
151- int (created_time ) / 1000 ,
152- tz = UTC ,
153- ).isoformat ()
154- columns ["last_modified_time" ] = datetime .fromtimestamp (
155- int (last_modified_time ) / 1000 ,
156- tz = UTC ,
157- ).isoformat ()
158- except : # noqa: E722
129+ self ._add_datetime_columns (columns )
130+ except Exception : # noqa: BLE001
159131 columns = results
160- pass
161-
162- try :
163- account_column = columns .get ("account" )
164- account_id = int (account_column .split (":" )[3 ])
165- columns ["account_id" ] = account_id
166- except : # noqa: E722, S110
167- pass
168- try :
169- campaign_column = columns .get ("campaignGroup" )
170- campaign = int (campaign_column .split (":" )[3 ])
171- columns ["campaign_group_id" ] = campaign
172- except : # noqa: E722, S110
173- pass
174- try :
132+ with contextlib .suppress (Exception ):
133+ self ._to_id_column (columns , "account" , "account_id" )
134+
135+ with contextlib .suppress (Exception ):
136+ self ._to_id_column (
137+ columns ,
138+ "campaignGroup" ,
139+ "campaign_group_id" ,
140+ )
141+ with contextlib .suppress (Exception ):
175142 schedule_column = columns .get ("runSchedule" ).get ("start" )
176143 columns ["run_schedule_start" ] = datetime .fromtimestamp ( # noqa: DTZ006
177144 int (schedule_column ) / 1000 ,
178145 ).isoformat ()
179- except : # noqa: E722, S110
180- pass
181-
182- results = (
146+ yield from (
183147 resp_json ["elements" ]
184148 if resp_json .get ("elements" ) is not None
185149 else [columns ]
186150 )
187151
188- yield from results
152+ def _to_id_column (
153+ self ,
154+ columns , # noqa: ANN001
155+ arg1 , # noqa: ANN001
156+ arg2 , # noqa: ANN001
157+ ) -> None :
158+ account_column = columns .get (arg1 )
159+ account_id = int (account_column .split (":" )[3 ])
160+ columns [arg2 ] = account_id
161+
162+ def _add_datetime_columns (self , columns ): # noqa: ANN202, ANN001
163+ created_time = columns .get ("changeAuditStamps" ).get ("created" ).get ("time" )
164+ last_modified_time = (
165+ columns .get ("changeAuditStamps" ).get ("lastModified" ).get ("time" )
166+ )
167+ columns ["created_time" ] = datetime .fromtimestamp (
168+ int (created_time ) / 1000 ,
169+ tz = UTC ,
170+ ).isoformat ()
171+ columns ["last_modified_time" ] = datetime .fromtimestamp (
172+ int (last_modified_time ) / 1000 ,
173+ tz = UTC ,
174+ ).isoformat ()
0 commit comments