From 83bc9655565e54e68c7cb13ed26924c1e5bfb3ae Mon Sep 17 00:00:00 2001 From: jakedt Date: Fri, 14 Mar 2014 17:35:52 -0400 Subject: [PATCH] Link the org api calls to their related user resources. --- endpoints/api/__init__.py | 2 +- endpoints/api/billing.py | 11 +++++------ endpoints/api/discovery.py | 19 ++++++++++++++++--- endpoints/api/logs.py | 4 ++-- endpoints/api/organization.py | 12 +++++++----- endpoints/api/permission.py | 3 +-- endpoints/api/robot.py | 7 +++---- endpoints/api/team.py | 3 +-- 8 files changed, 36 insertions(+), 25 deletions(-) diff --git a/endpoints/api/__init__.py b/endpoints/api/__init__.py index 76ea8af1a..cedddab59 100644 --- a/endpoints/api/__init__.py +++ b/endpoints/api/__init__.py @@ -58,7 +58,7 @@ def method_metadata(func, name): nickname = partial(add_method_metadata, 'nickname') -internal_api = add_method_metadata('internal_api', True) +related_user_resource = partial(add_method_metadata, 'related_user_resource') def query_param(name, help_str, type=reqparse.text_type, default=None, diff --git a/endpoints/api/billing.py b/endpoints/api/billing.py index 52ee6d351..650dd1792 100644 --- a/endpoints/api/billing.py +++ b/endpoints/api/billing.py @@ -4,7 +4,8 @@ import stripe from flask import request from flask.ext.restful import abort -from endpoints.api import resource, nickname, ApiResource, validate_json_request, log_action +from endpoints.api import (resource, nickname, ApiResource, validate_json_request, log_action, + related_user_resource) from endpoints.api.subscribe import subscribe, subscription_view from auth.permissions import AdministerOrganizationPermission from auth.auth_context import get_authenticated_user @@ -126,6 +127,7 @@ class UserCard(ApiResource): @resource('/v1/organization//card') +@related_user_resource(UserCard) class OrganizationCard(ApiResource): """ Resource for managing an organization's credit card. """ schemas = { @@ -145,7 +147,6 @@ class OrganizationCard(ApiResource): } @nickname('getOrgCard') - # @org_api_call('getOrgCard') def get(self, orgname): """ Get the organization's credit card. """ permission = AdministerOrganizationPermission(orgname) @@ -156,7 +157,6 @@ class OrganizationCard(ApiResource): abort(403) @nickname('setOrgCard') - # @org_api_call('set_user_card') @validate_json_request('OrgCard') def post(self, orgname): """ Update the orgnaization's credit card. """ @@ -223,6 +223,7 @@ class UserPlan(ApiResource): @resource('/v1/organization//plan') +@related_user_resource(UserPlan) class OrganizationPlan(ApiResource): """ Resource for managing a org's subscription. """ schemas = { @@ -246,7 +247,6 @@ class OrganizationPlan(ApiResource): } @nickname('updateOrgSubscription') - # @org_api_call('update_user_subscription') @validate_json_request('OrgSubscription') def put(self, orgname): """ Create or update the org's subscription. """ @@ -261,7 +261,6 @@ class OrganizationPlan(ApiResource): abort(403) @nickname('getOrgSubscription') - # @org_api_call('get_user_subscription') def get(self, orgname): """ Fetch any existing subscription for the org. """ permission = AdministerOrganizationPermission(orgname) @@ -296,10 +295,10 @@ class UserInvoiceList(ApiResource): @resource('/v1/organization//invoices') +@related_user_resource(UserInvoiceList) class OrgnaizationInvoiceList(ApiResource): """ Resource for listing an orgnaization's invoices. """ @nickname('listOrgInvoices') - # @org_api_call('list_user_invoices') def get(self, orgname): """ List the invoices for the specified orgnaization. """ permission = AdministerOrganizationPermission(orgname) diff --git a/endpoints/api/discovery.py b/endpoints/api/discovery.py index 087b51c1c..6a56a74c7 100644 --- a/endpoints/api/discovery.py +++ b/endpoints/api/discovery.py @@ -23,6 +23,11 @@ TYPE_CONVERTER = { } +def fully_qualified_name(method_view_class): + inst = method_view_class() + return '%s.%s' % (inst.__module__, inst.__class__.__name__) + + def swagger_route_data(): apis = [] models = {} @@ -47,7 +52,9 @@ def swagger_route_data(): 'required': True, }) - if method is not None: + if method is None: + logger.debug('Unable to find method for %s in class %s', method_name, view_class) + else: req_schema_name = method_metadata(method, 'request_schema') if req_schema_name: parameters.append({ @@ -93,11 +100,17 @@ def swagger_route_data(): operations.append(new_operation) swagger_path = PARAM_REGEX.sub(r'{\2}', rule.rule) - apis.append({ + new_resource = { 'path': swagger_path, 'description': view_class.__doc__ if view_class.__doc__ else "", 'operations': operations, - }) + 'name': fully_qualified_name(view_class), + } + related_user_res = method_metadata(view_class, 'related_user_resource') + if related_user_res is not None: + new_resource['related'] = fully_qualified_name(related_user_res) + + apis.append(new_resource) swagger_data = { 'apiVersion': 'v1', diff --git a/endpoints/api/logs.py b/endpoints/api/logs.py index 912eab531..9a6045fd9 100644 --- a/endpoints/api/logs.py +++ b/endpoints/api/logs.py @@ -4,7 +4,7 @@ from datetime import datetime, timedelta from flask.ext.restful import abort from endpoints.api import (resource, nickname, ApiResource, query_param, parse_args, - RepositoryParamResource, require_repo_admin) + RepositoryParamResource, require_repo_admin, related_user_resource) from auth.permissions import AdministerOrganizationPermission, AdministerOrganizationPermission from auth.auth_context import get_authenticated_user from data import model @@ -82,6 +82,7 @@ class RepositoryLogs(RepositoryParamResource): @resource('/v1/organization//logs') +@related_user_resource(UserLogs) class OrgLogs(ApiResource): """ Resource for fetching logs for the entire organization. """ @nickname('listOrgLogs') @@ -89,7 +90,6 @@ class OrgLogs(ApiResource): @query_param('starttime', 'Earliest time from which to get logs. (%m/%d/%Y %Z)', type=str) @query_param('endtime', 'Latest time to which to get logs. (%m/%d/%Y %Z)', type=str) @query_param('performer', 'Username for which to filter logs.', type=str) - # @org_api_call('list_user_logs') def get(self, args, orgname): """ List the logs for the specified organization. """ permission = AdministerOrganizationPermission(orgname) diff --git a/endpoints/api/organization.py b/endpoints/api/organization.py index 5a68efae7..d683be039 100644 --- a/endpoints/api/organization.py +++ b/endpoints/api/organization.py @@ -4,8 +4,10 @@ import stripe from flask import request from flask.ext.restful import abort -from endpoints.api import resource, nickname, ApiResource, validate_json_request, request_error +from endpoints.api import (resource, nickname, ApiResource, validate_json_request, request_error, + related_user_resource) from endpoints.api.team import team_view +from endpoints.api.user import User, PrivateRepositories from auth.permissions import (AdministerOrganizationPermission, OrganizationMemberPermission, CreateRepositoryPermission) from auth.auth_context import get_authenticated_user @@ -35,7 +37,7 @@ def org_view(o, teams): return view -@resource('/v1/organization/', methods=['POST']) +@resource('/v1/organization/') class OrganizationList(ApiResource): """ Resource for creating organizations. """ schemas = { @@ -88,7 +90,8 @@ class OrganizationList(ApiResource): return request_error(exception=ex) -@resource('/v1/organization/', methods=['GET']) +@resource('/v1/organization/') +@related_user_resource(User) class Organization(ApiResource): """ Resource for managing organizations. """ schemas = { @@ -124,7 +127,6 @@ class Organization(ApiResource): abort(403) - # @org_api_call('change_user_details') @nickname('changeOrganizationDetails') @validate_json_request('UpdateOrg') def put(self, orgname): @@ -152,9 +154,9 @@ class Organization(ApiResource): @resource('/v1/organization//private') +@related_user_resource(PrivateRepositories) class OrgPrivateRepositories(ApiResource): """ Custom verb to compute whether additional private repositories are available. """ - # @org_api_call('get_user_private_allowed') @nickname('getOrganizationPrivateAllowed') def get(self, orgname): """ Return whether or not this org is allowed to create new private repositories. """ diff --git a/endpoints/api/permission.py b/endpoints/api/permission.py index df2633a52..ea329da21 100644 --- a/endpoints/api/permission.py +++ b/endpoints/api/permission.py @@ -79,8 +79,7 @@ class RepositoryUserPermissionList(RepositoryParamResource): } -@resource('/v1/repository//permissions/user/', - methods=['GET']) +@resource('/v1/repository//permissions/user/') class RepositoryUserPermission(RepositoryParamResource): """ Resource for managing individual user permissions. """ schemas = { diff --git a/endpoints/api/robot.py b/endpoints/api/robot.py index d3c9d6642..6bcc34027 100644 --- a/endpoints/api/robot.py +++ b/endpoints/api/robot.py @@ -1,6 +1,6 @@ from flask.ext.restful import abort -from endpoints.api import resource, nickname, ApiResource, log_action +from endpoints.api import resource, nickname, ApiResource, log_action, related_user_resource from auth.permissions import AdministerOrganizationPermission, OrganizationMemberPermission from auth.auth_context import get_authenticated_user from data import model @@ -50,10 +50,10 @@ class UserRobot(ApiResource): @resource('/v1/organization//robots') +@related_user_resource(UserRobotList) class OrgRobotList(ApiResource): """ Resource for listing an organization's robots. """ @nickname('getOrgRobots') - #@org_api_call('get_user_robots') def get(self, orgname): """ List the organization's robots. """ permission = OrganizationMemberPermission(orgname) @@ -67,10 +67,10 @@ class OrgRobotList(ApiResource): @resource('/v1/organization//robots/') +@related_user_resource(UserRobot) class OrgRobot(ApiResource): """ Resource for managing an organization's robots. """ @nickname('createOrgRobot') - #@org_api_call('create_user_robot') def put(self, orgname, robot_shortname): """ Create a new robot in the organization. """ permission = AdministerOrganizationPermission(orgname) @@ -85,7 +85,6 @@ class OrgRobot(ApiResource): abort(403) @nickname('deleteOrgRobot') - #@org_api_call('delete_user_robot') def delete(self, orgname, robot_shortname): """ Delete an existing organization robot. """ permission = AdministerOrganizationPermission(orgname) diff --git a/endpoints/api/team.py b/endpoints/api/team.py index 147b87b98..8892e68e8 100644 --- a/endpoints/api/team.py +++ b/endpoints/api/team.py @@ -27,8 +27,7 @@ def member_view(member): } -@resource('/v1/organization//team/', - methods=['PUT', 'POST']) +@resource('/v1/organization//team/') class OrganizationTeam(ApiResource): """ Resource for manging an organization's teams. """ schemas = {