Esempio n. 1
0
 def get_route_info(self, request, route_name):
     intr_route = request.registry.introspector.get('routes', route_name)
     if intr_route is not None:
         route = intr_route['object']
         params = {k: '{{%s}}' % camelcase(k) for k in lookup_for_route_params(route)}
         url = '%s%s' % (request.application_url, unquote(route.generate(params)))
         return intr_route, url, params.keys()
Esempio n. 2
0
 def get_route_info(self, request, route_name):
     intr_route = request.registry.introspector.get("routes", route_name)
     if intr_route is not None:
         route = intr_route["object"]
         params = dict((k, "{{%s}}" % camelcase(k)) for k in lookup_for_route_params(route))
         url = "%s%s" % (request.application_url, unquote(route.generate(params)))
         return intr_route, url, params.keys()
Esempio n. 3
0
    def __call__(self, context, request):
        requests = []
        folders = MissingList()
        folders_descriptions = {}
        config = request.registry.config

        for schema_view in request.registry.getAllUtilitiesRegisteredFor(ISchemaView):
            # Make route for url
            schema_route = request.registry.introspector.get('routes', schema_view.route_name)
            if schema_route is not None:
                # Schema permission
                headers = set()
                permissions = lookup_for_route_permissions(request.registry, schema_route)
                method_permissions = maybe_list(permissions.get('GET'))
                for method_permission in method_permissions:
                    if method_permission not in (Everyone, NotAuthenticated):
                        headers.add('Authorization: Token {{token}}')
                        break

                request_id = self.new_unique_id()
                requests.append({
                    'id': request_id,
                    'headers': '\n'.join(headers),
                    'url': request.route_url(schema_view.route_name),
                    'preRequestScript': '',
                    'pathVariables': {},
                    'method': u'GET',
                    'data': [],
                    'dataMode': 'params',
                    'version': 2,
                    'tests': '',
                    'currentHelper': 'normal',
                    'helperAttributes': {},
                    'time': self.collection_time,
                    'name': u'Schema: %s' % schema_view.title,
                    'description': schema_view.description or '',
                    'collectionId': self.collection_id,
                    'responses': [],
                    'owner': 0,
                    'synced': False})
                folders[schema_view.title].append(request_id)
                folders_descriptions[schema_view.title] = schema_view.description

            for route_name, request_methods in schema_view.routes_names.items():
                # Make route for url
                intr_route = request.registry.introspector.get('routes', route_name)
                if intr_route is None:
                    continue
                route = intr_route['object']
                permissions = lookup_for_route_permissions(request.registry, intr_route)
                params = dict((k, '{{%s}}' % camelcase(k)) for k in lookup_for_route_params(route))
                url = '%s%s' % (request.application_url, unquote(route.generate(params)))

                schemas_by_methods = MissingList()
                for schema in config.lookup_input_schema(route_name, request_methods):
                    for request_method in maybe_list(schema.request_method) or DEFAULT_METHODS:
                        schemas_by_methods[request_method].append(schema)
                for schema in config.lookup_output_schema(route_name, request_methods):
                    for request_method in maybe_list(schema.request_method) or DEFAULT_METHODS:
                        schemas_by_methods[request_method].append(schema)

                for request_method, schemas in schemas_by_methods.items():
                    title = None
                    description = None
                    schema_data = []
                    tests = []

                    for schema in schemas:
                        if schema.schema_type == 'request':
                            if schema.schema:
                                schema_data.extend(
                                    self.construct_data(request_method, schema.schema))
                            if schema.fields_schema:
                                schema_data.extend(
                                    self.construct_data(request_method, schema.fields_schema))

                        if schema.schema and not title:
                            title = schema.schema.title
                        if schema.schema and not description:
                            description = schema.schema.description

                        variables = getattr(schema.schema, 'postman_environment_variables', None)
                        if variables:
                            for environment_key, response_key in variables.items():
                                environment_key = camelcase(environment_key)
                                response_key = camelcase(response_key)
                                tests.append(
                                    'if (answer.%s){ postman.setEnvironmentVariable("%s", answer.%s); }'
                                    % (response_key, environment_key, response_key))

                    if tests:
                        tests.insert(0, 'var answer = JSON.parse(responseBody);')

                    # Input params
                    method_url = url
                    request_schema_data = []
                    request_method = request_method.upper()
                    if request_method == 'GET':
                        queries = []
                        for url_param in schema_data:
                            if url_param['value']:
                                queries.append(
                                    '%s=%s'
                                    % (quote(url_param['key']), quote(url_param['value'])))
                            else:
                                queries.append(quote(url_param['key']))
                        if queries:
                            method_url = '%s?%s' % (method_url, '&'.join(queries))
                    else:
                        request_schema_data = schema_data

                    # Method permission
                    headers = set()
                    method_permissions = maybe_list(permissions.get(request_method))
                    for method_permission in method_permissions:
                        if method_permission not in (Everyone, NotAuthenticated):
                            headers.add('Authorization: Token {{token}}')
                            break

                    if not title:
                        title = route_name.replace('_', ' ').title()

                    request_id = self.new_unique_id()
                    requests.append({
                        'id': request_id,
                        'headers': '\n'.join(headers),
                        'url': method_url,
                        'preRequestScript': '',
                        'pathVariables': {},
                        'method': request_method,
                        'data': request_schema_data,
                        'dataMode': 'params',
                        'version': 2,
                        'tests': '\n'.join(tests),
                        'currentHelper': 'normal',
                        'helperAttributes': {},
                        'time': self.collection_time,
                        'name': title,
                        'description': description or '',
                        'collectionId': self.collection_id,
                        'responses': [],
                        'owner': 0,
                        'synced': False})
                    folders[schema_view.title].append(request_id)

        response_folders = []
        for key, requests_ids in folders.items():
            response_folders.append({
                'id': self.new_unique_id(),
                'name': key,
                'description': folders_descriptions.get(key) or '',
                'order': requests_ids,
                'collection_name': self.title,
                'collection_id': self.collection_id,
                'collection_owner': '',
                'write': True})

        return {
            'id': self.collection_id,
            'name': self.title,
            'description': self.description or '',
            'timestamp': self.collection_time,
            'synced': False,
            'owner': '',
            'subscribed': False,
            'remoteLink': '',
            'public': False,
            'write': True,
            'order': [],
            'folders': response_folders,
            'requests': requests}
Esempio n. 4
0
    def __call__(self, context, request):
        nodes = MissingDict()
        requested_methods = [key.lower() for key in request.GET.keys()]

        types = MissingList()
        models = MissingList()
        for route_name, request_methods in self.routes_names.items():
            route_methods = []
            for request_method in maybe_list(request_methods
                                             or DEFAULT_METHODS):
                if not requested_methods or request_method.lower(
                ) in requested_methods:
                    route_methods.append(request_method)
            if not route_methods:
                continue

            intr_route = request.registry.introspector.get(
                'routes', route_name)
            if intr_route is None:
                continue
            route = intr_route['object']
            params = dict((k, '{{%s}}' % camelcase(k))
                          for k in lookup_for_route_params(route))
            url = '%s%s' % (request.application_url,
                            unquote(route.generate(params)))

            schemas = request.registry.config.lookup_input_schema(
                route_name, route_methods)
            schemas.extend(
                request.registry.config.lookup_output_schema(
                    route_name, route_methods))
            for schema in schemas:
                fields = []
                if schema.schema:
                    details = self.construct_structure(request, schema.schema,
                                                       schema.schema_type,
                                                       types, models)
                    if isinstance(details, dict):
                        fields.append(details)
                    else:
                        fields.extend(details)

                if schema.schema_type == 'request' and schema.fields_schema:
                    details = self.construct_structure(request,
                                                       schema.fields_schema,
                                                       schema.schema_type,
                                                       types, models)
                    if isinstance(details, dict):
                        fields.append(details)
                    else:
                        fields.extend(details)

                name = camelcase('%s_%s' %
                                 (schema.request_method, schema.route_name))
                nodes[name][schema.schema_type] = fields
                nodes[name]['method'] = schema.request_method.upper()
                nodes[name]['url'] = url

        nodes['fieldTypes'] = lookup_for_common_fields(types,
                                                       ignore_key='fieldType')
        nodes['models'] = lookup_for_common_fields(models, ignore_key='model')
        return nodes