def run(self, req): """on get request return Welcome message """ jdata = jsend.error('ERROR') if 'id' in req.params: data = self.process(req.params['id']) if isinstance(data, list): jdata = jsend.success({'locations': self.etl(data)}) elif isinstance(data, str): jdata = jsend.error(data) elif isinstance(data, dict): jdata = jsend.fail(data) return jdata
def on_get(self, req, resp): """ on get request """ try: if req.params['token'] != os.environ.get('EXPORT_TOKEN') and req.params['token'] != os.environ.get('ACCESS_KEY'): raise ValueError(ERROR_ACCESS_401) timezone = pytz.timezone('America/Los_Angeles') current_time = datetime.datetime.now(timezone) file_name = 'DBI_permits_' + str(current_time.year) + str(current_time.month) + str(current_time.day) #DBI_permits_YYYYMMDDHHMI_response.csv where HH = 24 hour clock Mi = minutes #file_name = 'PTS_Export_09_26.csv' current_time = datetime.datetime.now(timezone) hours_added = datetime.timedelta(hours=-1) # rewind 1 hour to get the correct uploaded file name file_time = current_time + hours_added file_name = 'DBI_permits_' + str(file_time.year) + str(file_time.month) + str(file_time.day) + str(file_time.hour) + str(file_time.minute) + '_response.csv' result = self.get_result_file(file_name) if result == file_name: # dowloaded the result file self.process_file(self.data_file_path + file_name) resp.body = json.dumps(jsend.success({'message': file_name, 'responses':len(file_name)})) resp.status = falcon.HTTP_200 #pylint: disable=broad-except except Exception as exception: logging.exception('ProcessResultFile.on_get Exception') resp.status = falcon.HTTP_500 msg_error = ERROR_GENERIC if exception.__class__.__name__ == 'ValueError': msg_error = "{0}".format(exception) resp.body = json.dumps(jsend.error(msg_error))
def resp_error(msg, resp): """ Sets response error """ with sentry_sdk.configure_scope() as scope: scope.set_extra('message', msg) sentry_sdk.capture_message('sftp.resp_error', 'error') resp.body = json.dumps(jsend.error(msg)) resp.status = falcon.HTTP_400
def on_post(self, _req, resp): #pylint: disable=no-self-use """ append a new row """ try: request_body = _req.bounded_stream.read() request_params_json = json.loads(request_body) validate_post_params(request_params_json) gc = gspread.service_account(filename=CREDENTIALS_FILE) # pylint: disable=invalid-name worksheet = gc.open_by_key( request_params_json['spreadsheet_key'] ).worksheet( request_params_json['worksheet_title'] ) row = request_params_json['row_values'] worksheet.append_rows(row) resp.body = json.dumps(jsend.success({ 'row': row })) except Exception as err: # pylint: disable=broad-except err_msg = "{0}".format(err) resp.body = json.dumps(jsend.error(err_msg)) resp.status = falcon.HTTP_500
def on_get(self, _req, resp, permit_type): """on GET request return list of permits """ msg = False if permit_type in ('retail', 'retail_legacy'): permit_list = self.get_permit_list(permit_type) permit_list.sort(key=lambda v: ((v.get('dba_name') if v.get( 'dba_name') else v.get('business_name', '')) + ' ' + v.get( 'application_id', '')).upper()) if isinstance(permit_list, list): if permit_type == 'retail_legacy': data = self.get_legacy_list_transform(permit_list) else: data = {'list': permit_list} data_json = jsend.success(data) msg = 'success (' + str(len(permit_list)) + ')' else: pass if msg is not False: sentry_sdk.capture_message(msg, 'info') resp.body = json.dumps(data_json) resp.status = falcon.HTTP_200 else: msg = 'ERROR' sentry_sdk.capture_message(msg, 'error') resp.body = json.dumps(jsend.error(msg)) resp.status = falcon.HTTP_400
def default_error(_req, resp): """Handle default error""" resp.status = falcon.HTTP_404 msg_error = jsend.error('404 - Not Found') sentry_sdk.capture_message(msg_error) resp.body = json.dumps(msg_error)
def on_patch(self, _req, resp, row_id): #pylint: disable=no-self-use """ update an existing row """ try: request_body = _req.bounded_stream.read() request_params_json = json.loads(request_body) validate_patch_params(request_params_json) gc = gspread.service_account(filename=CREDENTIALS_FILE) # pylint: disable=invalid-name worksheet = gc.open_by_key( request_params_json['spreadsheet_key'] ).worksheet( request_params_json['worksheet_title'] ) column_idx = gspread.utils.a1_to_rowcol(request_params_json['id_column_label'] + '1')[1] row_to_edit_idx = worksheet.find(row_id, in_column=column_idx).row updates = [] for column_label, column_value in request_params_json['label_value_map'].items(): updates.append({ 'range': column_label + str(row_to_edit_idx), 'values': [[column_value]] }) worksheet.batch_update(updates) resp.body = json.dumps(jsend.success({ 'updates': updates })) resp.status = falcon.HTTP_200 except Exception as err: # pylint: disable=broad-except err_msg = "{0}".format(err) resp.body = json.dumps(jsend.error(err_msg)) resp.status = falcon.HTTP_500
def handle_set_converter_state(state: str): try: message = jsend.success({"converter_state":converter.set_converter_state(state)}) except ConverterError as e: message = jsend.error(e.message) return jsonify(message)
def on_post(self, req, resp): """Handle Submission POST requests""" submission = None try: json_params = req.media validate(json_params) # log submission to database submission = create_submission(self.session, json_params) # pylint: disable=no-member # schedule dispatch to external systems jobs_scheduled = tasks.schedule(submission_obj=submission,\ systems_dict=MAP) # return adu dispatcher id resp.body = json.dumps( jsend.success({ 'submission_id': submission.id, 'job_ids': [job.id for job in jobs_scheduled], 'params': json.dumps(req.media) })) resp.status = falcon.HTTP_200 except Exception as err: # pylint: disable=broad-except if (submission is not None and hasattr(submission, 'id') and isinstance(submission.id, int)): self.session.delete(submission) # pylint: disable=no-member self.session.commit() # pylint: disable=no-member # print("caught error in submission on_post") # print("traceback:") # traceback.print_exc(file=sys.stdout) print("error:") print("{0}".format(err)) resp.body = json.dumps(jsend.error("{0}".format(err))) resp.status = falcon.HTTP_500
def default_error(self, _req, resp): """Handle default error""" msg = falcon.HTTP_404 status = falcon.HTTP_404 resp.status = status msg_error = jsend.error(msg) resp.body = json.dumps(msg_error)
def default_error(self, _req, resp): # pylint: disable=no-self-use """Handle default error""" msg = falcon.HTTP_404 status = falcon.HTTP_404 resp.status = status msg_error = jsend.error(msg) resp.body = json.dumps(msg_error)
def test_default_error(client, mock_env_access_key): """Test default error response""" response = client.simulate_get('/some_page_that_does_not_exist') assert response.status_code == 404 expected_msg_error = jsend.error('404 - Not Found') assert json.loads(response.content) == expected_msg_error
def on_get(self, req, resp): """handle the get request""" response = FireRequest.get() if response.status_code == 200: resp.body = json.dumps(jsend.success(response.json())) else: resp.body = json.dumps(jsend.error(Records.ERROR_MSG)) resp.status = str( response.status_code) + " " + responses[response.status_code]
def create(self, request, *args, **kwargs): serializer = self.serializer_class(data=request.data) if serializer.is_valid(raise_exception=True): self.perform_create(serializer) return Response(jsend.success({"invoice": serializer.data}), status=status.HTTP_201_CREATED) else: return Response(jsend.error("INVALID REQUEST"), status=status.HTTP_400_BAD_REQUEST)
def handle_get(status: str): try: response, text = converter.read_status(status) message = jsend.success( {"response":response, "response_text":text}) except ConverterError as e: message = jsend.error(e.message) return jsonify(message)
def decode(): if strtobool(os.environ.get('ENABLE_AUTH_HEADER', 'false')): key = os.environ['AUTH_TOKEN_KEY'] value = os.environ['AUTH_TOKEN'] if value != request.headers.get(key): abort(403) data = request.get_json(True) if 'src' not in data: return jsonify(jsend.error('params/src')) try: src = str(data['src']) b = bytearray() b.extend(src.encode()) ret = base64.decodebytes(b).decode('ascii') return jsonify(jsend.success({'result': ret})) except: return jsonify(jsend.error('app/decoding'))
def on_get(self, _req, resp, version): #pylint: disable=no-self-use,no-member """ on get request determine which service and get the file """ try: if set(['name']).issubset(_req.params.keys()): object_name = _req.params['name'] temp_file_path = os.path.join(self.TEMP_FILE_DIR, object_name.replace('/', '-')) # download the file from provider client = self.determine_client(version) client.download_file(object_name, temp_file_path) # determine content type mime = magic.Magic(mime=True) resp.content_type = mime.from_file(temp_file_path) # build response resp.status = falcon.HTTP_200 with open(temp_file_path, 'rb') as f: # pylint: disable=invalid-name resp.body = f.read() # cleanup os.remove(temp_file_path) else: raise Exception('The name parameter is required') except botocore.exceptions.ClientError as err: if err.response['Error']['Code'] == "404": resp.status = falcon.HTTP_404 resp.body = json.dumps(jsend.error("The object does not exist")) else: resp.status = falcon.HTTP_500 resp.body = json.dumps(jsend.error(err.response['Error']['Message'])) except azure.core.exceptions.ResourceNotFoundError as err: resp.status = falcon.HTTP_404 resp.body = json.dumps(jsend.error("The object does not exist")) except Exception as err: # pylint: disable=broad-except print("FileHandler.on_get Err:{}".format(err)) print("Err type:{}".format(type(err).__name__)) resp.status = falcon.HTTP_500 resp.body = json.dumps(jsend.error("{0}".format(err)))
def _create_error(self, status_code): """ Construct an error message in jsend format. :param int status_code: The status code to translate into an error message :return: A dictionary in jsend format with the error and the code :rtype: dict """ return jsend.error(message=ComodoCA.status_code[status_code], code=status_code)
def update_record(self, req, resp, custom_type=None): """ example update_record """ record_id = None if 'id' in req.params: record_id = req.params['id'] if not record_id: resp.status = falcon.HTTP_400 msg = "The ID of the record to fetch is missing" resp.body = json.dumps(jsend.error(msg)) return if req.content_length: params = req.params del params['id'] data = req.stream.read(sys.maxsize) if custom_type == 'tables': response = self.accela.records.update_record_custom_tables( record_id, data, params) elif custom_type == 'forms': response = self.accela.records.update_record_custom_forms( record_id, data, params) else: response = self.accela.records.update_record( record_id, data, params) # default resp.status = falcon.HTTP_400 resp.body = json.dumps(jsend.error(response.text)) # if successful if response.status_code == 200: resp.status = falcon.HTTP_200 resp.body = json.dumps(response.json()) else: resp.status = falcon.HTTP_400 msg = "Request body is required." resp.body = json.dumps(jsend.error(msg)) return
def create_record(self, req, resp): """ example create_record """ if req.content_length: params = {'fields':'customId,id'} record = req.stream.read(sys.maxsize) response = self.accela.records.create_record(record, params) # default resp.status = falcon.HTTP_400 resp.body = json.dumps(jsend.error(response.text)) # if successful if response.status_code == 200: resp.status = falcon.HTTP_200 resp.body = json.dumps(response.json()) else: resp.status = falcon.HTTP_400 msg = "The create record information is missing" resp.body = json.dumps(jsend.error(msg)) return
def to_dict(self): ''' Transforms this exception to a dict ''' if self.status_code >= 400 and self.status_code < 500: result = dict( jsend.fail({ 'message': self.message, 'payload': self.payload })) elif self.status_code >= 500: result = dict( jsend.error(self.message, self.status_code, self.payload)) return result
def users(request): usersResourceUrl = os.environ.get('PREREQS_SERVICE_URL') + '/users' if request.method == 'GET': response = requests.get(usersResourceUrl) elif request.method == 'POST': postParams = json.loads(request.body) response = requests.post(usersResourceUrl, json=postParams) if response.status_code >= 300: return JsonResponse(jsend.error(response.text)) return JsonResponse(json.loads(response.text), safe=False)
def update_invoice(self, request, pk=None): if self.queryset.filter(id=pk).exists(): self.queryset.filter(id=pk).update(**request.data) inv = self.queryset.get(id=pk) serializer = self.serializer_class(inv) return Response( jsend.success({"invoice": serializer.data}), status=status.HTTP_200_OK, ) else: return Response((jsend.error("Invoice not found")), status=status.HTTP_404_NOT_FOUND)
def get_records(self, req, resp): """ example get_records response """ if 'ids' in req.params: record_id = req.params['ids'] params = req.params del params['ids'] response = self.accela.records.get_records(record_id, params, 'AccessToken') # default resp.status = falcon.HTTP_400 resp.body = json.dumps(jsend.error(response.text)) # if successful if response.status_code == 200: resp.status = falcon.HTTP_200 resp.body = json.dumps(response.json()) else: resp.status = falcon.HTTP_400 msg = "Parameter ids is required" resp.body = json.dumps(jsend.error(msg)) return
def parcels(self, req, resp): """ get parcels """ missing_env = self.has_missing_env(['PLN_ARCGIS_PARCEL']) if missing_env: resp.status = falcon.HTTP_404 msg_error = jsend.error( 'Missing or invalid environment variable(s): ' + ", ".join(missing_env)) resp.body = json.dumps(msg_error) return response = {'message': 'parcels'} if 'address' in req.params: address = req.params['address'] options = { 'returnGeometry': False, 'returnSuggestions': False, 'outFields': 'blklot,block_num,lot_num,ADDRESS' } if 'returnSuggestions' in req.params and req.params[ 'returnSuggestions'] == 'true': options['returnSuggestions'] = True if 'returnGeometry' in req.params: options['returnGeometry'] = req.params['returnGeometry'] if 'outFields' in req.params: options['outFields'] = req.params['outFields'] sfarcgis = SfArcgis() sfarcgis.set_layer('parcel', os.environ.get('PLN_ARCGIS_PARCEL')) parcels = sfarcgis.get_fields_by_address(address, options) response = {'parcels': parcels} with sentry_sdk.configure_scope() as scope: scope.set_extra('arcgis.parcels', { 'address': address, 'options': options, 'response': parcels }) blank_response_msg = "" if not parcels: blank_response_msg = "(blank)" sentry_sdk.capture_message( ('parcel response ' + blank_response_msg).strip(), 'info') resp.body = json.dumps(jsend.success(response)) resp.status = falcon.HTTP_200
def on_get(self, req, resp, name): """on get request return Welcome message """ msg = jsend.error('Service Call failed.') if name == 'config.js': return self.config_js(req, resp) if name.isalnum() and os.path.isfile( os.path.join(self.svcs_path, name + '.py')): svc_module = importlib.import_module('service.svcs.' + name) svc_class = getattr(svc_module, name.capitalize()) svc = svc_class() msg = svc.run(req) resp.body = json.dumps(msg) resp.status = falcon.HTTP_200
def submit(self, cert_type_name, csr, term, subject_alt_names=''): """ Submit a certificate request to Comodo. :param string cert_type_name: The full cert type name (Example: 'PlatinumSSL Certificate') the supported certificate types for your account can be obtained with the get_cert_types() method. :param string csr: The Certificate Signing Request (CSR) :param int term: The length, in days, for the certificate to be issued :param string subject_alt_names: Subject Alternative Names separated by a ",". :return: The certificate_id and the normal status messages for errors. :rtype: dict """ cert_types = self.get_cert_types() # If collection of cert types fails we simply pass the error back. if cert_types['status'] == 'fail': return cert_types # Find the certificate type ID for cert_type in cert_types['data']['types']: if cert_type['name'] == cert_type_name: cert_type_id = cert_type['id'] url = self._create_url('enroll') data = { 'orgId': self.org_id, 'csr': csr, 'subjAltNames': subject_alt_names, 'certType': cert_type_id, 'numberServers': 1, 'serverType': -1, 'term': term, 'comments': 'Requested with comodo_proxy', 'externalRequester': '' } result = self.session.post(url, json=data) if result.status_code == 200: return jsend.success({'certificate_id': result.json()['sslId']}) # Anything else is an error else: return jsend.error(result.json()['description'])
def revoke(self, cert_id, reason=''): """ Revoke a certificate. :param int cert_id: The certificate ID :param str reason: Reason for revocation (up to 512 characters), can be blank: '', but must exist. :return: The result of the operation, 'Successful' on success :rtype: dict """ url = self._create_url('revoke/{}'.format(cert_id)) data = {'reason': reason} result = self.session.post( url, json=data, ) if result.status_code == 204: return jsend.success() else: return jsend.error(result.json()['description'])
def on_post(self, _req, resp): #pylint: disable=no-self-use """ Record post into the database """ try: request_body = _req.bounded_stream.read() json_params = json.loads(request_body) submission = create_submission(self.session, json_params) # pylint: disable=no-member resp.status = falcon.HTTP_200 resp.body = json.dumps( jsend.success({ 'submission_id': submission.id, 'date_received': submission.date_received.astimezone(LOCAL_TZ).strftime( "%Y/%m/%d, %H:%M:%S") })) except Exception as err: # pylint: disable=broad-except print("error:") print("{0}".format(err)) resp.status = falcon.HTTP_500 resp.body = json.dumps(jsend.error("{0}".format(err)))
def on_get(self, _req, resp): #pylint: disable=no-self-use """ returns status of the export """ try: if 'export_id' in _req.params: export_id = _req.params['export_id'] if not is_valid_uuid(export_id): raise Exception("Invalid export_id") exports = self.session.query(ExportStatusModel).filter( # pylint: disable=no-member ExportStatusModel.guid == export_id) if exports.count() == 1: export_status = exports.first() resp.status = falcon.HTTP_200 if export_status.result is None: export_status.result = {'success': [], 'failure': []} resp.body = json.dumps( jsend.success({ 'is_finished': export_status.date_finished is not None, 'success': export_status.result['success'], 'failure': export_status.result['failure'] })) else: raise Exception("Invalid export_id") else: raise Exception("export_id is required") except Exception as err: # pylint: disable=broad-except err_msg = "{0}".format(err) print(err_msg) resp.status = falcon.HTTP_500 resp.body = json.dumps(jsend.error(err_msg))
def test_error(self): jsend_obj = jsend.error(message='error message') self.assertTrue(jsend_obj.is_error) self.assertEqual(jsend_obj.message, 'error message')
def test_error_message_must_be_str(self): try: jsend.error(message=1) except ValueError: return self.fail()
def test_error_code_must_be_number(self): try: jsend.error(code='1') except ValueError: return self.fail()
def test_error_data_must_be_dict(self): try: jsend.error(data=1) except ValueError: return self.fail()