2
2
from io import StringIO
3
3
from typing import Any , Dict , Iterable , List , Optional , Union
4
4
5
- from flask import Response , jsonify , stream_with_context , request
5
+ from flask import Response , jsonify , stream_with_context
6
6
from flask .json import dumps
7
7
import orjson
8
8
9
9
from ._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
12
10
from ._common import is_compatibility_mode , log_info_with_request
13
- from ._limiter import requests_left , get_multiples_count
14
11
from delphi .epidata .common .logger import get_structured_logger
15
12
16
13
@@ -25,15 +22,7 @@ def print_non_standard(format: str, data):
25
22
message = "no results"
26
23
result = - 2
27
24
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"
37
26
result = 1
38
27
if result == - 1 and is_compatibility_mode ():
39
28
return jsonify (dict (result = result , message = message ))
@@ -127,40 +116,21 @@ class ClassicPrinter(APrinter):
127
116
"""
128
117
129
118
def _begin (self ):
130
- if is_compatibility_mode () and not show_hard_api_key_warning () :
119
+ if is_compatibility_mode ():
131
120
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": ['
144
122
145
123
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 ():
147
125
sep = b'"epidata": ['
148
126
else :
149
127
sep = b"," if not first else b""
150
128
return sep + orjson .dumps (row )
151
129
152
130
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"
162
132
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 ():
164
134
# no array to end
165
135
prefix = ""
166
136
@@ -194,7 +164,7 @@ def _format_row(self, first: bool, row: Dict):
194
164
self ._tree [group ].append (row )
195
165
else :
196
166
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 ():
198
168
return b'"epidata": ['
199
169
return None
200
170
@@ -205,10 +175,7 @@ def _end(self):
205
175
tree = orjson .dumps (self ._tree )
206
176
self ._tree = dict ()
207
177
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
212
179
213
180
214
181
class CSVPrinter (APrinter ):
@@ -243,17 +210,6 @@ def _format_row(self, first: bool, row: Dict):
243
210
columns = list (row .keys ())
244
211
self ._writer = DictWriter (self ._stream , columns , lineterminator = "\n " )
245
212
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
-
257
213
self ._writer .writerow (row )
258
214
259
215
# remove the stream content to print just one line at a time
@@ -274,18 +230,7 @@ class JSONPrinter(APrinter):
274
230
"""
275
231
276
232
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"["
289
234
290
235
def _format_row (self , first : bool , row : Dict ):
291
236
sep = b"," if not first else b""
@@ -303,19 +248,6 @@ class JSONLPrinter(APrinter):
303
248
def make_response (self , gen , headers = None ):
304
249
return Response (gen , mimetype = " text/plain; charset=utf8" , headers = headers )
305
250
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
-
319
251
def _format_row (self , first : bool , row : Dict ):
320
252
# each line is a JSON file with a new line to separate them
321
253
return orjson .dumps (row , option = orjson .OPT_APPEND_NEWLINE )
@@ -338,4 +270,4 @@ def create_printer(format: str) -> APrinter:
338
270
return CSVPrinter ()
339
271
if format == "jsonl" :
340
272
return JSONLPrinter ()
341
- return ClassicPrinter ()
273
+ return ClassicPrinter ()
0 commit comments