def test_welcome(client): """Test welcome message response""" response = client.simulate_get('/welcome') assert response.status_code == 200 expected_msg = jsend.success({'message': 'Welcome'}) assert json.loads(response.content) == expected_msg
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 on_get(self, _req, resp): """on get request return Welcome message """ msg = {'message': 'Welcome'} resp.body = json.dumps(jsend.success(msg)) resp.status = falcon.HTTP_200
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 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): """ Implement POST """ request_body = req.bounded_stream.read() json_params = json.loads(request_body) data = json_params["submission"] if data and data["data"] and data["data"]["notifyMeByTextMessage"]: _from_number = os.environ.get('TWILIO_FROM') _to_number = data["data"]["phoneNumber"] _message = self.get_sms(data) # Update .env with Live Credentials to send actual sms account_sid = os.environ.get('TWILIO_SID') auth_token = os.environ.get('TWILIO_TOKEN') client = Client(account_sid, auth_token) message = client.messages.create( to=_to_number, from_=_from_number, #test From number from twilio body=_message) if message.sid: resp.status = falcon.HTTP_200 resp.body = json.dumps(jsend.success({'message': 'SMS sent!'})) else: resp.status = falcon.HTTP_400 resp.body = json.dumps( jsend.fail({'message': 'Failed to send SMS'}))
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 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 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 get_exceptions(result: object): """ Catch and return errors from a request result. :param result: A requests session result. :return: A jsend formatted result with either success or failure. :rtype: dict """ logger.debug('Examining result for exceptions.') # Determine if they gave us JSON, if not set the data to nothing. try: r_json = {'results': result.json()} except JSONDecodeError: logger.debug('Results are not JSON.') r_json = {'results': []} try: result.raise_for_status() except HTTPError: logger.debug('HTTP Error Code: %s detected', result.status_code) return jsend.fail(r_json) # 204 is essentially an error, so we catch it. if result.status_code == 204: return jsend.fail(r_json) else: return jsend.success(r_json)
def collect(self, cert_id, format_type): """ Poll for certificate availability after submission. :param int cert_id: The certificate ID :param str format_type: The format type to use (example: 'X509 PEM Certificate only') :return: The certificate_id or the certificate depending on whether the certificate is ready (check status code) :rtype: dict """ result = self.client.service.collect( authData=self.auth, id=cert_id, formatType=ComodoCA.format_type[format_type]) # The certificate is ready for collection if result.statusCode == 2: return jsend.success({ 'certificate': result.SSL.certificate, 'certificate_status': 'issued', 'certificate_id': cert_id }) # The certificate is not ready for collection yet elif result.statusCode == 0: return jsend.fail({ 'certificate_id': cert_id, 'certificate': '', 'certificate_status': 'pending' }) # Some error occurred else: return self._create_error(result.statusCode)
def get_total_client(self, request): total_clients = Client.objects.count() return Response( jsend.success({"client": { "total": total_clients }}), status=status.HTTP_200_OK, )
def test_jsend_to_stringify_f(self): ret_json_string = jsend.success().stringify() try: jsend.is_success(ret_json_string) except TypeError: return self.fail()
def get_total_invoice(self, request): total_invoice = Invoice.objects.count() return Response( jsend.success({"invoice": { "total": total_invoice }}), status=status.HTTP_200_OK, )
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 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 on_get_records(self, req, resp, parcel): """ Look up record for `parcel` multiple `fields` names can be passed using comma delimiter """ fields = [] if 'fields' in req.params: fields_string = req.params['fields'] or None if fields_string: fields = fields_string.split(',') msg = Record(parcel).get(fields) resp.body = json.dumps(jsend.success(msg)) resp.status = falcon.HTTP_200
def on_get(self, req, resp, parcel): """ Handles GET requests If `parcel` number is passed, do a look up on data available on the parcel record Parcel (Block/Lot) can be found online via https://sfplanninggis.org/pim/ """ if parcel: self.on_get_records(req, resp, parcel) else: msg = {'message': falcon.HTTP_200} resp.body = json.dumps(jsend.success(msg)) resp.status = falcon.HTTP_200
def on_get(self, req, resp, name=None): """on get request return Arcgis message """ if not name: name = req.path.strip('/') if name in dir(self): getattr(self, name)(req, resp) else: msg = {'message': 'Welcome'} resp.body = json.dumps(jsend.success(msg)) resp.status = falcon.HTTP_200
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 submit(self, cert_type_name, csr, revoke_password, term, subject_alt_names='', server_type='OTHER'): """ 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 string revoke_password: A password for certificate revocation :param int term: The length, in years, for the certificate to be issued :param string subject_alt_names: Subject Alternative Names separated by a ",". :param string server_type: The type of server for the TLS certificate e.g 'Apache/ModSSL' full list available in ComodoCA.server_type (Default: OTHER) :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'] == 'error': return cert_types # We do this because we need to pass the entire cert type definition back to Comodo # not just the name. for cert_type in cert_types['data']['cert_types']: if cert_type.name == cert_type_name: cert_type_def = cert_type result = self.client.service.enroll( authData=self.auth, orgId=self.org_id, secretKey=self.secret_key, csr=csr, phrase=revoke_password, subjAltNames=subject_alt_names, certType=cert_type_def, numberServers=1, serverType=ComodoCA.formats[server_type], term=term, comments='') # Anything greater than 0 is the certificate ID if result > 0: return jsend.success({'certificate_id': result}) # Anything else is an error else: return self._create_error(result)
def get_cert_types(self): """ Collect the certificate types that are available to the customer. :return: A list of dictionaries of certificate types :rtype: list """ result = self.client.service.getCustomerCertTypes(authData=self.auth) if result.statusCode == 0: return jsend.success({'cert_types': result.types}) else: return self._create_error(result.statusCode)
def test_welcome(client, mock_env_access_key): # pylint: disable=unused-argument # mock_env_access_key is a fixture and creates a false positive for pylint """Test welcome message response""" response = client.simulate_get('/welcome') assert response.status_code == 200 expected_msg = jsend.success({'message': 'Welcome'}) assert json.loads(response.content) == expected_msg # Test welcome request with no ACCESS_KEY in header client_no_access_key = testing.TestClient(service.microservice.start_service()) response = client_no_access_key.simulate_get('/welcome') assert response.status_code == 403
def get_cert_types(self): """ Collect the certificate types that are available to the customer. :return: A list of dictionaries of certificate types :rtype: list """ url = self._create_url('types') result = self._get(url) if result.status_code == 200: return jsend.success({'types': result.json()}) else: return jsend.fail(result.json())
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 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 edit_user(): username = request.json['username'] cellphone = request.json['cellphone'] name = request.json['name'] last_name = request.json['last_name'] date_of_birth = request.json['date_of_birth'] rg = request.json['rg'] cpf = request.json['cpf'] user = User.query.filter_by(username=username).first() user.update(cellphone, name, last_name, date_of_birth, rg, cpf) db.session.commit() user = User.query.filter_by(username=username).first() return jsend.success(user.serialize)
def renew(self, cert_id): """ Renew a certificate by ID. :param int cert_id: The certificate ID :return: The result of the operation, 'Successful' on success :rtype: dict """ url = self._create_url('renewById/{}'.format(cert_id)) result = self.session.post(url, json='') if result.status_code == 200: return jsend.success({'certificate_id': result.json()['sslId']}) else: return jsend.fail(result.json())
def revoke(self, cert_id, reason=''): """ Revoke a certificate. :param int cert_id: The certificate ID :param str reason: Reason for revocation (up to 256 characters), can be blank: '' :return: The result of the operation, 'Successful' on success :rtype: dict """ result = self.client.service.revoke(authData=self.auth, id=cert_id, reason=reason) if result == 0: return jsend.success() else: return self._create_error(result)
def test_jsend_to_stringify(self): ret_json_string = jsend.success().stringify() jsend_obj = jsend.loads(ret_json_string) self.assertTrue(jsend_obj.is_success)
def test_success_data_must_be_dict(self): try: jsend.success(data=1) except ValueError: return self.fail()
def test_jsend_from_string(self): jsend_str = jsend.success(data={'key': 'value'}).stringify() jsend_obj = jsend.loads(jsend_str) self.assertEqual(jsend_str, jsend_obj.stringify()) self.assertTrue(jsend_obj.is_success)
def test_success(self): jsend_obj = jsend.success(data={'key': 'value'}) self.assertTrue(jsend_obj.is_success) self.assertEqual(jsend_obj.data['key'], 'value')