def register_service(endpoint_type, settings): """Registers a service in cornice, for the given type.""" path_pattern = getattr(viewset, '%s_path' % endpoint_type) path = path_pattern.format(**path_formatters) name = viewset.get_service_name(endpoint_type, resource_cls) service = Service(name, path, depth=depth, **viewset.get_service_arguments()) # Attach viewset and resource to the service for later reference. service.viewset = viewset service.resource = resource_cls service.collection_path = viewset.collection_path.format( **path_formatters) service.record_path = viewset.record_path.format(**path_formatters) service.type = endpoint_type methods = getattr(viewset, '%s_methods' % endpoint_type) for method in methods: if not viewset.is_endpoint_enabled(endpoint_type, resource_name, method.lower(), settings): continue argument_getter = getattr(viewset, '%s_arguments' % endpoint_type) view_args = argument_getter(resource_cls, method) view = viewset.get_view(endpoint_type, method.lower()) service.add_view(method, view, klass=resource_cls, **view_args) return service
def register_service(endpoint_type, settings): """Registers a service in cornice, for the given type.""" path_pattern = getattr(viewset, '%s_path' % endpoint_type) path = path_pattern.format(**path_formatters) name = viewset.get_service_name(endpoint_type, resource_cls) service = Service(name, path, depth=depth, **viewset.get_service_arguments()) # Attach viewset and resource to the service for later reference. service.viewset = viewset service.resource = resource_cls service.collection_path = viewset.collection_path.format( **path_formatters) service.record_path = viewset.record_path.format(**path_formatters) service.type = endpoint_type methods = getattr(viewset, '%s_methods' % endpoint_type) for method in methods: if not viewset.is_endpoint_enabled( endpoint_type, resource_name, method.lower(), settings): continue argument_getter = getattr(viewset, '%s_arguments' % endpoint_type) view_args = argument_getter(resource_cls, method) view = viewset.get_view(endpoint_type, method.lower()) service.add_view(method, view, klass=resource_cls, **view_args) return service
from cliquet import Service from cliquet.authorization import RouteFactory attachment = Service(name='attachment', description='Attach file to record', path='/attachment', factory=RouteFactory) @attachment.post(permission='attach') def attachment_post(request): return {"ok": True} def includeme(config): config.add_cornice_service(attachment)
Linux_x86_64-gcc3/ en-US/ default/ Linux%203.13.0-66-generic%20%28GTK%203.10.8%29/ default/ default/ 7/ 64/ 1/ """ from cliquet import Service blocklist = Service( name="blocklist", path='/blocklist/{api_ver}/{application_guid}/{application_ver}/', description="Blocklist data") @blocklist.get() def get_blocklist(request): api_ver = request.matchdict['api_ver'] app = request.matchdict['application_guid'] app_ver = request.matchdict['application_ver'] query = request.body print api_ver, app, app_ver, query return { 'API version': api_ver,
""" settings = request.registry.settings method = request.method.lower() setting_key = 'record_%s_%s_enabled' % (collection_name, method) enabled = settings.get(setting_key, False) if not enabled: error_msg = 'Endpoint disabled for this collection in configuration.' response = errors.http_error(httpexceptions.HTTPMethodNotAllowed(), errno=errors.ERRORS.METHOD_NOT_ALLOWED, message=error_msg) raise response record = Service(name='record', description='Firefox Sync Collection Record service', path=('/buckets/{bucket_id}/collections/' '{collection_name}/records/{record_id}'), cors_headers=('Last-Modified', 'ETag')) @record.get(permission=NO_PERMISSION_REQUIRED) def record_get(request): collection_name = request.matchdict['collection_name'] record_id = request.matchdict['record_id'] sync_client = build_sync_client(request) headers = import_headers(request) record = sync_client.get_record(collection_name, record_id, headers=headers)
from pyramid.security import NO_PERMISSION_REQUIRED from cliquet import Service heartbeat = Service(name="heartbeat", path='/__heartbeat__', description="Server health") @heartbeat.get(permission=NO_PERMISSION_REQUIRED) def get_heartbeat(request): """Return information about server health.""" status = {} heartbeats = request.registry.heartbeats for name, callable in heartbeats.items(): status[name] = callable(request) has_error = not all([v or v is None for v in status.values()]) if has_error: request.response.status = 503 return status lbheartbeat = Service(name="lbheartbeat", path='/__lbheartbeat__', description="Web head health") @lbheartbeat.get(permission=NO_PERMISSION_REQUIRED)
from pyramid.security import NO_PERMISSION_REQUIRED from cliquet import Service from cliquet.statsd import statsd_count from cliquet.errors import raise_invalid from syncto.authentication import build_sync_client from syncto.headers import import_headers, export_headers collection = Service(name='collection', description='Firefox Sync Collection service', path=('/buckets/{bucket_id}/collections' '/{collection_name}/records'), cors_headers=('Next-Page', 'Total-Records', 'Last-Modified', 'ETag', 'Quota-Remaining')) @collection.get(permission=NO_PERMISSION_REQUIRED) def collection_get(request): collection_name = request.matchdict['collection_name'] sync_client = build_sync_client(request) headers = import_headers(request) params = {} if '_since' in request.GET: try: params['newer'] = '%.2f' % (int(request.GET['_since']) / 1000.0) except ValueError: error_msg = ("_since should be a number.") raise_invalid(request,
from pyramid.security import NO_PERMISSION_REQUIRED, Authenticated from cliquet import Service, PROTOCOL_VERSION hello = Service(name="hello", path='/', description="Welcome") @hello.get(permission=NO_PERMISSION_REQUIRED) def get_hello(request): """Return information regarding the current instance.""" settings = request.registry.settings project_name = settings['project_name'] project_version = settings['project_version'] data = dict(project_name=project_name, project_version=project_version, http_api_version=settings['http_api_version'], project_docs=settings['project_docs'], cliquet_protocol_version=PROTOCOL_VERSION, url=request.route_url(hello.name)) eos = get_eos(request) if eos: data['eos'] = eos data['settings'] = {} public_settings = request.registry.public_settings # Public settings will be prefixed with project name, unless explicitly # specified with cliquet. (for retrocompability of clients for example). for setting in list(public_settings): if setting.startswith('cliquet.'): unprefixed = setting.replace('cliquet.', '', 1)
iterating on each field to deserialize them. """ # On defaults, path is not mandatory. self.get('defaults').get('path').missing = colander.drop # Fill requests values with defaults. requests = data.get('requests', []) for request in requests: defaults = data.get('defaults') if isinstance(defaults, dict): merge_dicts(request, defaults) return data batch = Service(name="batch", path='/batch', description="Batch operations") @batch.post(schema=BatchPayloadSchema, permission=NO_PERMISSION_REQUIRED) def post_batch(request): requests = request.validated['requests'] batch_size = len(requests) limit = request.registry.settings['batch_max_requests'] if limit and len(requests) > int(limit): error_msg = 'Number of requests is limited to %s' % limit request.errors.add('body', 'requests', error_msg) return if any([batch.path in req['path'] for req in requests]): error_msg = 'Recursive call on %s endpoint is forbidden.' % batch.path