22from io import StringIO
33from typing import Any , Dict , Iterable , List , Optional , Union
44
5- from flask import Response , jsonify , stream_with_context , request
5+ from flask import Response , jsonify , stream_with_context
66from flask .json import dumps
77import orjson
88
99from ._config import MAX_RESULTS , MAX_COMPATIBILITY_RESULTS
10- # TODO: remove warnings after once we are past the API_KEY_REQUIRED_STARTING_AT date
11- from ._security import show_hard_api_key_warning , show_soft_api_key_warning , ROLLOUT_WARNING_RATE_LIMIT , ROLLOUT_WARNING_MULTIPLES , _ROLLOUT_WARNING_AD_FRAGMENT , PHASE_1_2_STOPGAP
1210from ._common import is_compatibility_mode , log_info_with_request
13- from ._limiter import requests_left , get_multiples_count
1411from delphi .epidata .common .logger import get_structured_logger
1512
1613
@@ -25,15 +22,7 @@ def print_non_standard(format: str, data):
2522 message = "no results"
2623 result = - 2
2724 else :
28- warning = ""
29- if show_hard_api_key_warning ():
30- if requests_left () == 0 :
31- warning = f"{ ROLLOUT_WARNING_RATE_LIMIT } "
32- if get_multiples_count (request ) < 0 :
33- warning = f"{ warning } { ROLLOUT_WARNING_MULTIPLES } "
34- if requests_left () == 0 or get_multiples_count (request ) < 0 :
35- warning = f"{ warning } { _ROLLOUT_WARNING_AD_FRAGMENT } { PHASE_1_2_STOPGAP } "
36- message = warning .strip () or "success"
25+ message = "success"
3726 result = 1
3827 if result == - 1 and is_compatibility_mode ():
3928 return jsonify (dict (result = result , message = message ))
@@ -127,40 +116,21 @@ class ClassicPrinter(APrinter):
127116 """
128117
129118 def _begin (self ):
130- if is_compatibility_mode () and not show_hard_api_key_warning () :
119+ if is_compatibility_mode ():
131120 return "{ "
132- r = '{ "epidata": ['
133- if show_hard_api_key_warning ():
134- warning = ""
135- if requests_left () == 0 :
136- warning = f"{ warning } { ROLLOUT_WARNING_RATE_LIMIT } "
137- if get_multiples_count (request ) < 0 :
138- warning = f"{ warning } { ROLLOUT_WARNING_MULTIPLES } "
139- if requests_left () == 0 or get_multiples_count (request ) < 0 :
140- warning = f"{ warning } { _ROLLOUT_WARNING_AD_FRAGMENT } { PHASE_1_2_STOPGAP } "
141- if warning != "" :
142- return f'{ r } "{ warning .strip ()} ",'
143- return r
121+ return '{ "epidata": ['
144122
145123 def _format_row (self , first : bool , row : Dict ):
146- if first and is_compatibility_mode () and not show_hard_api_key_warning () :
124+ if first and is_compatibility_mode ():
147125 sep = b'"epidata": ['
148126 else :
149127 sep = b"," if not first else b""
150128 return sep + orjson .dumps (row )
151129
152130 def _end (self ):
153- warning = ""
154- if show_soft_api_key_warning ():
155- if requests_left () == 0 :
156- warning = f"{ warning } { ROLLOUT_WARNING_RATE_LIMIT } "
157- if get_multiples_count (request ) < 0 :
158- warning = f"{ warning } { ROLLOUT_WARNING_MULTIPLES } "
159- if requests_left () == 0 or get_multiples_count (request ) < 0 :
160- warning = f"{ warning } { _ROLLOUT_WARNING_AD_FRAGMENT } { PHASE_1_2_STOPGAP } "
161- message = warning .strip () or "success"
131+ message = "success"
162132 prefix = "], "
163- if self .count == 0 and is_compatibility_mode () and not show_hard_api_key_warning () :
133+ if self .count == 0 and is_compatibility_mode ():
164134 # no array to end
165135 prefix = ""
166136
@@ -194,7 +164,7 @@ def _format_row(self, first: bool, row: Dict):
194164 self ._tree [group ].append (row )
195165 else :
196166 self ._tree [group ] = [row ]
197- if first and is_compatibility_mode () and not show_hard_api_key_warning () :
167+ if first and is_compatibility_mode ():
198168 return b'"epidata": ['
199169 return None
200170
@@ -205,10 +175,7 @@ def _end(self):
205175 tree = orjson .dumps (self ._tree )
206176 self ._tree = dict ()
207177 r = super (ClassicTreePrinter , self )._end ()
208- r = tree + r
209- if show_hard_api_key_warning () and (requests_left () == 0 or get_multiples_count (request ) < 0 ):
210- r = b", " + r
211- return r
178+ return tree + r
212179
213180
214181class CSVPrinter (APrinter ):
@@ -243,17 +210,6 @@ def _format_row(self, first: bool, row: Dict):
243210 columns = list (row .keys ())
244211 self ._writer = DictWriter (self ._stream , columns , lineterminator = "\n " )
245212 self ._writer .writeheader ()
246- if show_hard_api_key_warning ():
247- warning = ""
248- if requests_left () == 0 :
249- warning = f"{ warning } { ROLLOUT_WARNING_RATE_LIMIT } "
250- if get_multiples_count (request ) < 0 :
251- warning = f"{ warning } { ROLLOUT_WARNING_MULTIPLES } "
252- if requests_left () == 0 or get_multiples_count (request ) < 0 :
253- warning = f"{ warning } { _ROLLOUT_WARNING_AD_FRAGMENT } { PHASE_1_2_STOPGAP } "
254- if warning .strip () != "" :
255- self ._writer .writerow ({columns [0 ]: warning })
256-
257213 self ._writer .writerow (row )
258214
259215 # remove the stream content to print just one line at a time
@@ -274,18 +230,7 @@ class JSONPrinter(APrinter):
274230 """
275231
276232 def _begin (self ):
277- r = b"["
278- if show_hard_api_key_warning ():
279- warning = ""
280- if requests_left () == 0 :
281- warning = f"{ warning } { ROLLOUT_WARNING_RATE_LIMIT } "
282- if get_multiples_count (request ) < 0 :
283- warning = f"{ warning } { ROLLOUT_WARNING_MULTIPLES } "
284- if requests_left () == 0 or get_multiples_count (request ) < 0 :
285- warning = f"{ warning } { _ROLLOUT_WARNING_AD_FRAGMENT } { PHASE_1_2_STOPGAP } "
286- if warning .strip () != "" :
287- r = b'["' + bytes (warning , "utf-8" ) + b'",'
288- return r
233+ return b"["
289234
290235 def _format_row (self , first : bool , row : Dict ):
291236 sep = b"," if not first else b""
@@ -303,19 +248,6 @@ class JSONLPrinter(APrinter):
303248 def make_response (self , gen , headers = None ):
304249 return Response (gen , mimetype = " text/plain; charset=utf8" , headers = headers )
305250
306- def _begin (self ):
307- if show_hard_api_key_warning ():
308- warning = ""
309- if requests_left () == 0 :
310- warning = f"{ warning } { ROLLOUT_WARNING_RATE_LIMIT } "
311- if get_multiples_count (request ) < 0 :
312- warning = f"{ warning } { ROLLOUT_WARNING_MULTIPLES } "
313- if requests_left () == 0 or get_multiples_count (request ) < 0 :
314- warning = f"{ warning } { _ROLLOUT_WARNING_AD_FRAGMENT } { PHASE_1_2_STOPGAP } "
315- if warning .strip () != "" :
316- return bytes (warning , "utf-8" ) + b"\n "
317- return None
318-
319251 def _format_row (self , first : bool , row : Dict ):
320252 # each line is a JSON file with a new line to separate them
321253 return orjson .dumps (row , option = orjson .OPT_APPEND_NEWLINE )
@@ -338,4 +270,4 @@ def create_printer(format: str) -> APrinter:
338270 return CSVPrinter ()
339271 if format == "jsonl" :
340272 return JSONLPrinter ()
341- return ClassicPrinter ()
273+ return ClassicPrinter ()
0 commit comments