@@ -37,7 +37,7 @@ def get_caps():
37
37
return jsonify (res ), res_code
38
38
39
39
40
- def parse_batch_req ( r ):
40
+ def parse_batch_request ( req ):
41
41
"""
42
42
Checks a batch request dict for the required fields:
43
43
@@ -56,61 +56,61 @@ def parse_batch_req(r):
56
56
dict within the request for json bodies, and `body` will be the *bytes* data (i.e. decoded from
57
57
base64, when using `b64`) for 'b64' or 'bytes' requests.
58
58
"""
59
- if not isinstance (r , dict ):
59
+ if not isinstance (req , dict ):
60
60
app .logger .warning ("Invalid batch request: batch request is not a dict" )
61
61
abort (http .BAD_REQUEST )
62
- if 'method' not in r :
62
+ if 'method' not in req :
63
63
app .logger .warning ("Invalid batch request: batch request has no method" )
64
64
abort (http .BAD_REQUEST )
65
- if 'path' not in r :
65
+ if 'path' not in req :
66
66
app .logger .warning ("Invalid batch request: batch request has no path" )
67
67
abort (http .BAD_REQUEST )
68
68
69
- method , path , headers , json , body = r ['method' ], r ['path' ], {}, None , None
69
+ method , path , headers , json , body = req ['method' ], req ['path' ], {}, None , None
70
70
71
- if 'headers' in r :
72
- if not isinstance (r ['headers' ], dict ):
71
+ if 'headers' in req :
72
+ if not isinstance (req ['headers' ], dict ):
73
73
app .logger .warning ("Bad batch request: 'headers' must be a dict" )
74
74
abort (http .BAD_REQUEST )
75
- if any (not isinstance (k , str ) or not isinstance (v , str ) for k , v in r ['headers' ].items ()):
75
+ if any (not isinstance (k , str ) or not isinstance (v , str ) for k , v in req ['headers' ].items ()):
76
76
app .logger .warning ("Bad batch request: 'headers' must contain only str/str pairs" )
77
77
abort (http .BAD_REQUEST )
78
- headers = r ['headers' ]
78
+ headers = req ['headers' ]
79
79
80
- has_body = r [ ' method' ] in ('POST' , 'PUT' )
81
- if not has_body and r [ ' method' ] not in ('GET' , 'DELETE' ):
82
- app .logger .warning (f"Bad batch request: invalid request method { r [ ' method' ] } " )
80
+ has_body = method in ('POST' , 'PUT' )
81
+ if not has_body and method not in ('GET' , 'DELETE' ):
82
+ app .logger .warning (f"Bad batch request: invalid request method { method } " )
83
83
abort (http .BAD_REQUEST )
84
84
85
85
if not path .startswith ('/' ):
86
86
app .logger .warning (f"Bad batch request: path must start with /, got: [{ path } ]" )
87
87
abort (http .BAD_REQUEST )
88
88
89
- n_bodies = sum (k in r for k in ('b64' , 'json' , 'bytes' ))
89
+ n_bodies = sum (k in req for k in ('b64' , 'json' , 'bytes' ))
90
90
if has_body :
91
91
if not n_bodies :
92
- app .logger .warning (f"Bad batch request: { r [ ' method' ] } requires one of json/b64/bytes" )
92
+ app .logger .warning (f"Bad batch request: { method } requires one of json/b64/bytes" )
93
93
abort (http .BAD_REQUEST )
94
94
elif n_bodies > 1 :
95
95
app .logger .warning (
96
- f"Bad batch request: { r [ ' method' ] } cannot have more than one of json/bytes/b64"
96
+ f"Bad batch request: { method } cannot have more than one of json/bytes/b64"
97
97
)
98
98
abort (http .BAD_REQUEST )
99
99
100
- if 'b64' in r :
100
+ if 'b64' in req :
101
101
try :
102
- body = utils .decode_base64 (r ['b64' ])
102
+ body = utils .decode_base64 (req ['b64' ])
103
103
except Exception :
104
104
app .logger .warning ("Bad batch request: b64 value is not valid base64" )
105
- elif 'bytes' in r :
106
- body = r ['bytes' ]
105
+ elif 'bytes' in req :
106
+ body = req ['bytes' ]
107
107
if not isinstance (body , bytes ):
108
108
body = body .encode ()
109
109
else :
110
- json = r ['json' ]
110
+ json = req ['json' ]
111
111
112
112
elif n_bodies :
113
- app .logger .warning (f"Bad batch request: { r ['method' ]} cannot have a json/b64/bytes body" )
113
+ app .logger .warning (f"Bad batch request: { req ['method' ]} cannot have a json/b64/bytes body" )
114
114
abort (http .BAD_REQUEST )
115
115
116
116
return method , path , headers , json , body
@@ -138,7 +138,7 @@ def batch(_sequential=False):
138
138
139
139
# Expand this into a list first (rather than during iteration below) so that we abort everything
140
140
# if any subrequest is invalid.
141
- subreqs = [parse_batch_req (r ) for r in subreqs ]
141
+ subreqs = [parse_batch_request (r ) for r in subreqs ]
142
142
143
143
response = []
144
144
for method , path , headers , json , body in subreqs :
0 commit comments