def get_goals(): if request.method == 'GET': return jsonify( snake_to_camel_case_dict({ 'goals': [goal.serialize() for goal in Goal.query.all()], })) elif request.method == 'POST': return jsonify( snake_to_camel_case_dict({ 'goal': { 'id': 1, 'amount': 1000, 'name': 'Cool Goal 1', 'start_date': datetime.now(), 'end_date': (datetime.now() + timedelta(days=30)), }, }))
def test_get_transactions_with_valid_user_institution_should_succeed(self): url = url_for('transactions_api.get_transactions', institution=self.institution.name) headers = self.create_headers(user=self.user) response = self.request_endpoint('GET', url, headers, status.HTTP_200_OK) self.assertListEqual( json.loads(response.data.decode('utf-8'))['transactions'], [snake_to_camel_case_dict(self.transaction.serialize())])
def _post_to_endpoint(self, url, expected_status_code=status.HTTP_200_OK, expected_error_msg=None, **payload): response = self.client.post(url, data=json.dumps( snake_to_camel_case_dict(payload)), content_type='application/json') self.assertEqual(response.status_code, expected_status_code) if expected_error_msg: self.assertEqual(response.json['message'], expected_error_msg)
def create_user(first_name, last_name, email, password): """ Creates a new user with the provided credentials, and returns a token. """ if User.by_email(email): raise exceptions.Conflict(description='User already exists.') user = UserFactory.instance.create(first_name, last_name, email, password).save() token = generate_token_for_user(user) return jsonify(snake_to_camel_case_dict({'token': token.decode("utf-8")}))
def create_token(email, password): """ If the provided email and password combination is valid, generates a JWT for client-use which expires in 7 days. """ existing_user = User.query.filter_by(email=email).first_or_404() if not PasswordManager.context.verify(password, existing_user.password_hash): raise exceptions.Unauthorized( description='Email or password were not correct.') token = generate_token_for_user(existing_user) return jsonify(snake_to_camel_case_dict({'token': token.decode("utf-8")}))
def request_endpoint(self, method, url, headers, expected_status_code=status.HTTP_200_OK, expected_error_msg=None, **payload): if method == 'GET': response = self.client.get(url, headers=headers) elif method == 'DELETE': response = self.client.delete(url, headers=headers) elif method == 'POST': response = self.client.post(url, headers=headers, data=snake_to_camel_case_dict(payload)) elif method == 'PUT': response = self.client.put(url, headers=headers, data=snake_to_camel_case_dict(payload)) self.assertEqual(response.status_code, expected_status_code) if expected_error_msg: self.assertEqual(response.json['message'], expected_error_msg) return response
def get_profile(user): """ Profile things. """ user_profile = user.profile if user.profile else UserProfile( user=user).save() user_profile_json = { 'email': user.email, 'birthday': user_profile.birthday } return jsonify( snake_to_camel_case_dict({'user_profile': user_profile_json}))
def get_transactions(institution_name, user): """ Get transactions associated with an institution. """ transactions = Transaction.query.filter( Transaction.post_date >= datetime.now() - timedelta(days=7)) if institution_name: instn = Institution.query.filter_by( name=institution_name).first_or_404() transactions = transactions.filter_by(institution=instn) return jsonify( snake_to_camel_case_dict({ 'transactions': [transaction.serialize() for transaction in transactions] }))
def decorator(func): @functools.wraps(func) def wrapped(*args, **kwargs): kwargs = camel_to_snake_case_dict(kwargs) return func(*args, **kwargs) return use_kwargs(snake_to_camel_case_dict(kwargs_))(wrapped)