Conditions | 5 |
Total Lines | 109 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | """ |
||
124 | @api.route('/gages/<int:gid>/sample', methods=['POST']) |
||
125 | def gage_new_samples(gid): |
||
126 | """ |
||
127 | Submit new samples to gage *id* |
||
128 | |||
129 | Parameters: |
||
130 | id (int): Primary id key number of a gage |
||
131 | |||
132 | Samples are formatted in body of request as a JSON Web Signature using the |
||
133 | ``Gage.key`` |
||
134 | |||
135 | Example sample submitter: :: |
||
136 | |||
137 | from itsdangerous import JSONWebSignatureSerializer |
||
138 | import requests |
||
139 | |||
140 | payload = {'samples':[ |
||
141 | {'type':'level', |
||
142 | 'value':16.7, |
||
143 | 'datetime': str(datetime.datetime.now()) |
||
144 | }, |
||
145 | {'type':'amps', |
||
146 | 'value':367.3, |
||
147 | 'datetime': str(datetime.datetime.now()) |
||
148 | }, |
||
149 | {'type':'voltage', |
||
150 | 'value':14.3, |
||
151 | 'datetime': str(datetime.datetime.now()) |
||
152 | }, |
||
153 | {'type':'discharge', |
||
154 | 'value':480, |
||
155 | 'datetime': str(datetime.datetime.now()) |
||
156 | } |
||
157 | ], |
||
158 | 'gage':{ |
||
159 | 'id':5 |
||
160 | }} |
||
161 | |||
162 | s = JSONWebSignatureSerializer('<gage.key>') |
||
163 | |||
164 | def submit(payload): |
||
165 | data = s.dumps(payload) |
||
166 | url = "http://riverflo.ws/api/1.0/gages/<gage.id>/sample" |
||
167 | r = requests.post(url, data=data) |
||
168 | if r.status_code is 200: |
||
169 | return True |
||
170 | else: |
||
171 | return False |
||
172 | |||
173 | If the key matches the stored key for the gage id in the url, \ |
||
174 | the server will iterate over the samples and add them to the database \ |
||
175 | creating new sensors for the gage if a new sample type is found. \ |
||
176 | Then the server will return JSON with a status code of 200. |
||
177 | |||
178 | Example response: :: |
||
179 | |||
180 | { 'gage': {u'id': 5, |
||
181 | 'location': 'Androscoggin River downstream of I-95 in Auburn ME', |
||
182 | 'name': 'Androscoggin River at Auburn', |
||
183 | 'url': 'http://riverflo.ws/api/1.0/gages/5'}, |
||
184 | 'result': 'created', |
||
185 | 'samples': [{'datetime': 'Tue, 04 Nov 2014 20:43:39 GMT', |
||
186 | 'id': 10781, |
||
187 | 'url': 'http://riverflo.ws/api/1.0/samples/10781', |
||
188 | 'value': 16.7}, |
||
189 | {'datetime': 'Tue, 04 Nov 2014 20:43:39 GMT', |
||
190 | 'id': 10782, |
||
191 | 'url': 'http://riverflo.ws/api/1.0/samples/10782', |
||
192 | 'value': 367.3}, |
||
193 | {'datetime': 'Tue, 04 Nov 2014 20:43:39 GMT', |
||
194 | 'id': 10783, |
||
195 | 'url': 'http://riverflo.ws/api/1.0/samples/10783', |
||
196 | 'value': 14.3}, |
||
197 | {'datetime': 'Tue, 04 Nov 2014 20:43:39 GMT', |
||
198 | 'id': 10784, |
||
199 | 'url': 'http://riverflo.ws/api/1.0/samples/10784', |
||
200 | 'value': 480.0}]} |
||
201 | |||
202 | If the signature does not match, the server will return JSON \ |
||
203 | with a status code of 401 - Unauthorized: :: |
||
204 | |||
205 | {'error': 'unauthorized', 'message': 'bad signature'} |
||
206 | |||
207 | """ |
||
208 | gage = Gage.query.get_or_404(gid) |
||
209 | s = JSONWebSignatureSerializer(gage.key) |
||
210 | try: |
||
211 | req_json = s.loads(request.data) |
||
212 | except BadSignature: # If the signature doesn't match |
||
213 | return unauthorized('bad signature') |
||
214 | except TypeError: # Return unauthorized if no key defined for gage |
||
215 | return unauthorized('bad signature') |
||
216 | else: |
||
217 | samples = req_json['samples'] |
||
218 | output = [] |
||
219 | print(samples) |
||
220 | for sample in samples: |
||
221 | result = gage.new_sample(stype=sample['type'].lower(), |
||
222 | value=sample['value'], |
||
223 | sdatetime=sample['datetime']) |
||
224 | result_json = result.to_sensor_json() |
||
225 | print(result_json) |
||
226 | result_json['sender_id'] = sample['sender_id'] |
||
227 | print(result_json) |
||
228 | output.append(result_json) |
||
229 | return jsonify({ |
||
230 | 'gage': gage.to_json(), |
||
231 | 'samples': output, |
||
232 | 'result': 'created' |
||
233 | }) |
||
234 |