Change to nicer API method names and add the internal_api_call decorator in prep for the automatic discovery mechanism

This commit is contained in:
Joseph Schorr 2013-12-24 21:56:03 -05:00
parent 445145f9b9
commit 1904e6d0c8
2 changed files with 72 additions and 44 deletions

View file

@ -60,6 +60,15 @@ def api_login_required(f):
return decorated_view
def internal_api_call(f):
@wraps(f)
def decorated_view(*args, **kwargs):
return f(*args, **kwargs)
decorated_view.__internal_call = True
return decorated_view
@app.errorhandler(model.DataModelException)
def handle_dme(ex):
return make_response(ex.message, 400)
@ -71,12 +80,13 @@ def handle_dme_key_error(ex):
@app.route('/api/')
@internal_api_call
def welcome():
return make_response('welcome', 200)
@app.route('/api/plans/')
def plans_list():
def list_plans():
return jsonify({
'plans': PLANS,
})
@ -108,6 +118,7 @@ def user_view(user):
@app.route('/api/user/', methods=['GET'])
@internal_api_call
def get_logged_in_user():
if current_user.is_anonymous():
return jsonify({'anonymous': True})
@ -121,6 +132,7 @@ def get_logged_in_user():
@app.route('/api/user/private', methods=['GET'])
@api_login_required
@internal_api_call
def get_user_private_count():
user = current_user.db_user()
private_repos = model.get_private_repo_count(user.username)
@ -141,6 +153,7 @@ def get_user_private_count():
@app.route('/api/user/convert', methods=['POST'])
@api_login_required
@internal_api_call
def convert_user_to_organization():
user = current_user.db_user()
convert_data = request.get_json()
@ -177,6 +190,7 @@ def convert_user_to_organization():
@app.route('/api/user/', methods=['PUT'])
@api_login_required
@internal_api_call
def change_user_details():
user = current_user.db_user()
@ -203,7 +217,8 @@ def change_user_details():
@app.route('/api/user/', methods=['POST'])
def create_user_api():
@internal_api_call
def create_new_user():
user_data = request.get_json()
existing_user = model.get_user(user_data['username'])
@ -229,7 +244,8 @@ def create_user_api():
@app.route('/api/signin', methods=['POST'])
def signin_api():
@internal_api_call
def signin_user():
signin_data = request.get_json()
username = signin_data['username']
@ -263,6 +279,7 @@ def conduct_signin(username, password):
@app.route("/api/signout", methods=['POST'])
@api_login_required
@internal_api_call
def logout():
logout_user()
identity_changed.send(app, identity=AnonymousIdentity())
@ -270,7 +287,8 @@ def logout():
@app.route("/api/recovery", methods=['POST'])
def send_recovery():
@internal_api_call
def request_recovery_email():
email = request.get_json()['email']
code = model.create_reset_password_email_code(email)
send_recovery_email(email, code.code)
@ -355,7 +373,8 @@ def team_view(orgname, team):
@app.route('/api/organization/', methods=['POST'])
@api_login_required
def create_organization_api():
@internal_api_call
def create_organization():
org_data = request.get_json()
existing = None
@ -496,6 +515,7 @@ def get_organization_member(orgname, membername):
@app.route('/api/organization/<orgname>/private', methods=['GET'])
@api_login_required
@internal_api_call
def get_organization_private_allowed(orgname):
permission = CreateRepositoryPermission(orgname)
if permission.can():
@ -657,7 +677,7 @@ def delete_organization_team_member(orgname, teamname, membername):
@app.route('/api/repository', methods=['POST'])
@api_login_required
def create_repo_api():
def create_repo():
owner = current_user.db_user()
req = request.get_json()
namespace_name = req['namespace'] if 'namespace' in req else owner.username
@ -690,7 +710,7 @@ def create_repo_api():
@app.route('/api/find/repository', methods=['GET'])
def match_repos_api():
def find_repos():
prefix = request.args.get('query', '')
def repo_view(repo):
@ -713,7 +733,7 @@ def match_repos_api():
@app.route('/api/repository/', methods=['GET'])
def list_repos_api():
def list_repos():
def repo_view(repo_obj):
return {
'namespace': repo_obj.namespace,
@ -756,7 +776,7 @@ def list_repos_api():
@app.route('/api/repository/<path:repository>', methods=['PUT'])
@api_login_required
@parse_repository_name
def update_repo_api(namespace, repository):
def update_repo(namespace, repository):
permission = ModifyRepositoryPermission(namespace, repository)
if permission.can():
repo = model.get_repository(namespace, repository)
@ -779,7 +799,7 @@ def update_repo_api(namespace, repository):
methods=['POST'])
@api_login_required
@parse_repository_name
def change_repo_visibility_api(namespace, repository):
def change_repo_visibility(namespace, repository):
permission = AdministerRepositoryPermission(namespace, repository)
if permission.can():
repo = model.get_repository(namespace, repository)
@ -823,7 +843,7 @@ def image_view(image):
@app.route('/api/repository/<path:repository>', methods=['GET'])
@parse_repository_name
def get_repo_api(namespace, repository):
def get_repo(namespace, repository):
logger.debug('Get repo: %s/%s' % (namespace, repository))
def tag_view(tag):
@ -1006,6 +1026,7 @@ def delete_webhook(namespace, repository, public_id):
@app.route('/api/filedrop/', methods=['POST'])
@api_login_required
@internal_api_call
def get_filedrop_url():
mime_type = request.get_json()['mimeType']
(url, file_id) = user_files.prepare_for_drop(mime_type)
@ -1436,14 +1457,16 @@ def subscription_view(stripe_subscription, used_repos):
@app.route('/api/user/card', methods=['GET'])
@api_login_required
def get_user_card_api():
@internal_api_call
def get_user_card():
user = current_user.db_user()
return get_card(user)
@app.route('/api/organization/<orgname>/card', methods=['GET'])
@api_login_required
def get_org_card_api(orgname):
@internal_api_call
def get_org_card(orgname):
permission = AdministerOrganizationPermission(orgname)
if permission.can():
organization = model.get_organization(orgname)
@ -1454,7 +1477,8 @@ def get_org_card_api(orgname):
@app.route('/api/user/card', methods=['POST'])
@api_login_required
def set_user_card_api():
@internal_api_call
def set_user_card():
user = current_user.db_user()
token = request.get_json()['token']
response = set_card(user, token)
@ -1464,7 +1488,7 @@ def set_user_card_api():
@app.route('/api/organization/<orgname>/card', methods=['POST'])
@api_login_required
def set_org_card_api(orgname):
def set_org_card(orgname):
permission = AdministerOrganizationPermission(orgname)
if permission.can():
organization = model.get_organization(orgname)
@ -1515,7 +1539,8 @@ def get_card(user):
@app.route('/api/user/plan', methods=['PUT'])
@api_login_required
def subscribe_api():
@internal_api_call
def update_user_subscription():
request_data = request.get_json()
plan = request_data['plan']
token = request_data['token'] if 'token' in request_data else None
@ -1607,7 +1632,7 @@ def subscribe(user, plan, token, require_business_plan):
@app.route('/api/user/invoices', methods=['GET'])
@api_login_required
def user_invoices_api():
def list_user_invoices():
user = current_user.db_user()
if not user.stripe_id:
abort(404)
@ -1617,7 +1642,7 @@ def user_invoices_api():
@app.route('/api/organization/<orgname>/invoices', methods=['GET'])
@api_login_required
def org_invoices_api(orgname):
def list_org_invoices(orgname):
permission = AdministerOrganizationPermission(orgname)
if permission.can():
organization = model.get_organization(orgname)
@ -1653,7 +1678,8 @@ def get_invoices(customer_id):
@app.route('/api/organization/<orgname>/plan', methods=['PUT'])
@api_login_required
def subscribe_org_api(orgname):
@internal_api_call
def update_org_subscription(orgname):
permission = AdministerOrganizationPermission(orgname)
if permission.can():
request_data = request.get_json()
@ -1667,6 +1693,7 @@ def subscribe_org_api(orgname):
@app.route('/api/user/plan', methods=['GET'])
@api_login_required
@internal_api_call
def get_subscription():
user = current_user.db_user()
private_repos = model.get_private_repo_count(user.username)
@ -1685,6 +1712,7 @@ def get_subscription():
@app.route('/api/organization/<orgname>/plan', methods=['GET'])
@api_login_required
@internal_api_call
def get_org_subscription(orgname):
permission = AdministerOrganizationPermission(orgname)
if permission.can():
@ -1804,7 +1832,7 @@ def log_view(log):
@app.route('/api/repository/<path:repository>/logs', methods=['GET'])
@api_login_required
@parse_repository_name
def repo_logs_api(namespace, repository):
def list_repo_logs(namespace, repository):
permission = AdministerRepositoryPermission(namespace, repository)
if permission.can():
repo = model.get_repository(namespace, repository)
@ -1820,7 +1848,7 @@ def repo_logs_api(namespace, repository):
@app.route('/api/organization/<orgname>/logs', methods=['GET'])
@api_login_required
def org_logs_api(orgname):
def list_org_logs(orgname):
permission = AdministerOrganizationPermission(orgname)
if permission.can():
performer_name = request.args.get('performer', None)
@ -1835,7 +1863,7 @@ def org_logs_api(orgname):
@app.route('/api/user/logs', methods=['GET'])
@api_login_required
def user_logs_api():
def list_user_logs():
performer_name = request.args.get('performer', None)
start_time = request.args.get('starttime', None)
end_time = request.args.get('endtime', None)

View file

@ -105,20 +105,20 @@ def build_specs():
return [
TestSpec(url_for('welcome'), 200, 200, 200, 200),
TestSpec(url_for('plans_list'), 200, 200, 200, 200),
TestSpec(url_for('list_plans'), 200, 200, 200, 200),
TestSpec(url_for('get_logged_in_user'), 200, 200, 200, 200),
TestSpec(url_for('change_user_details'),
401, 200, 200, 200).set_method('PUT'),
TestSpec(url_for('create_user_api'), 201, 201, 201,
TestSpec(url_for('create_new_user'), 201, 201, 201,
201).set_method('POST').set_data_from_obj(NEW_USER_DETAILS),
TestSpec(url_for('signin_api'), 200, 200, 200,
TestSpec(url_for('signin_user'), 200, 200, 200,
200).set_method('POST').set_data_from_obj(SIGNIN_DETAILS),
TestSpec(url_for('send_recovery'), 201, 201, 201,
TestSpec(url_for('request_recovery_email'), 201, 201, 201,
201).set_method('POST').set_data_from_obj(SEND_RECOVERY_DETAILS),
TestSpec(url_for('get_matching_users', prefix='dev'), 401, 200, 200, 200),
@ -161,29 +161,29 @@ def build_specs():
teamname=ORG_READERS, membername=ORG_OWNER),
admin_code=400).set_method('DELETE'),
(TestSpec(url_for('create_repo_api'))
(TestSpec(url_for('create_repo'))
.set_method('POST')
.set_data_from_obj(NEW_ORG_REPO_DETAILS)),
TestSpec(url_for('match_repos_api'), 200, 200, 200, 200),
TestSpec(url_for('find_repos'), 200, 200, 200, 200),
TestSpec(url_for('list_repos_api'), 200, 200, 200, 200),
TestSpec(url_for('list_repos'), 200, 200, 200, 200),
TestSpec(url_for('update_repo_api', repository=PUBLIC_REPO),
TestSpec(url_for('update_repo', repository=PUBLIC_REPO),
admin_code=403).set_method('PUT'),
(TestSpec(url_for('update_repo_api', repository=ORG_REPO))
(TestSpec(url_for('update_repo', repository=ORG_REPO))
.set_method('PUT')
.set_data_from_obj(UPDATE_REPO_DETAILS)),
(TestSpec(url_for('update_repo_api', repository=PRIVATE_REPO))
(TestSpec(url_for('update_repo', repository=PRIVATE_REPO))
.set_method('PUT')
.set_data_from_obj(UPDATE_REPO_DETAILS)),
(TestSpec(url_for('change_repo_visibility_api', repository=PUBLIC_REPO),
(TestSpec(url_for('change_repo_visibility', repository=PUBLIC_REPO),
admin_code=403).set_method('POST')
.set_data_from_obj(CHANGE_VISIBILITY_DETAILS)),
(TestSpec(url_for('change_repo_visibility_api', repository=ORG_REPO))
(TestSpec(url_for('change_repo_visibility', repository=ORG_REPO))
.set_method('POST').set_data_from_obj(CHANGE_VISIBILITY_DETAILS)),
(TestSpec(url_for('change_repo_visibility_api', repository=PRIVATE_REPO))
(TestSpec(url_for('change_repo_visibility', repository=PRIVATE_REPO))
.set_method('POST').set_data_from_obj(CHANGE_VISIBILITY_DETAILS)),
TestSpec(url_for('delete_repository', repository=PUBLIC_REPO),
@ -193,11 +193,11 @@ def build_specs():
TestSpec(url_for('delete_repository', repository=PRIVATE_REPO),
admin_code=204).set_method('DELETE'),
TestSpec(url_for('get_repo_api', repository=PUBLIC_REPO),
TestSpec(url_for('get_repo', repository=PUBLIC_REPO),
200, 200, 200,200),
TestSpec(url_for('get_repo_api', repository=ORG_REPO),
TestSpec(url_for('get_repo', repository=ORG_REPO),
403, 403, 200, 200),
TestSpec(url_for('get_repo_api', repository=PRIVATE_REPO),
TestSpec(url_for('get_repo', repository=PRIVATE_REPO),
403, 403, 200, 200),
TestSpec(url_for('get_repo_builds', repository=PUBLIC_REPO),
@ -403,20 +403,20 @@ def build_specs():
TestSpec(url_for('delete_token', repository=PRIVATE_REPO,
code=FAKE_TOKEN), admin_code=400).set_method('DELETE'),
TestSpec(url_for('subscribe_api'), 401, 400, 400, 400).set_method('PUT'),
TestSpec(url_for('update_user_subscription'), 401, 400, 400, 400).set_method('PUT'),
TestSpec(url_for('subscribe_org_api', orgname=ORG),
TestSpec(url_for('update_org_subscription', orgname=ORG),
401, 403, 403, 400).set_method('PUT'),
TestSpec(url_for('get_subscription'), 401, 200, 200, 200),
TestSpec(url_for('get_org_subscription', orgname=ORG)),
TestSpec(url_for('repo_logs_api', repository=PUBLIC_REPO), admin_code=403),
TestSpec(url_for('repo_logs_api', repository=ORG_REPO)),
TestSpec(url_for('repo_logs_api', repository=PRIVATE_REPO)),
TestSpec(url_for('list_repo_logs', repository=PUBLIC_REPO), admin_code=403),
TestSpec(url_for('list_repo_logs', repository=ORG_REPO)),
TestSpec(url_for('list_repo_logs', repository=PRIVATE_REPO)),
TestSpec(url_for('org_logs_api', orgname=ORG)),
TestSpec(url_for('list_org_logs', orgname=ORG)),
]