Changeset View
Changeset View
Standalone View
Standalone View
swh/core/api/__init__.py
- This file was moved from swh/core/api.py.
# Copyright (C) 2015-2017 The Software Heritage developers | # Copyright (C) 2015-2017 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import collections | import collections | ||||
import functools | import functools | ||||
import inspect | import inspect | ||||
import json | import json | ||||
import logging | import logging | ||||
import pickle | import pickle | ||||
import requests | import requests | ||||
import datetime | import datetime | ||||
from flask import Flask, Request, Response | from flask import Flask, Request, Response, request, abort | ||||
from .serializers import (decode_response, | from .serializers import (decode_response, | ||||
encode_data_client as encode_data, | encode_data_client as encode_data, | ||||
msgpack_dumps, msgpack_loads, SWHJSONDecoder) | msgpack_dumps, msgpack_loads, SWHJSONDecoder) | ||||
from negotiate.flask import Formatter | from .negotiate import (Formatter as FormatterBase, | ||||
Negotiator as NegotiatorBase, | |||||
negotiate as _negotiate) | |||||
logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||
# support for content negotation | |||||
class Negotiator(NegotiatorBase): | |||||
def best_mimetype(self): | |||||
return request.accept_mimetypes.best_match( | |||||
self.accept_mimetypes, 'text/html') | |||||
def _abort(self, status_code, err=None): | |||||
return abort(status_code, err) | |||||
def negotiate(formatter_cls, *args, **kwargs): | |||||
return _negotiate(Negotiator, formatter_cls, *args, **kwargs) | |||||
class Formatter(FormatterBase): | |||||
def _make_response(self, body, content_type): | |||||
return Response(body, content_type=content_type) | |||||
vlorentz: Should be in `negotiate.py` (it makes sense logically, and it will get the required copyright… | |||||
class SWHJSONEncoder(json.JSONEncoder): | class SWHJSONEncoder(json.JSONEncoder): | ||||
def default(self, obj): | def default(self, obj): | ||||
if isinstance(obj, (datetime.datetime, datetime.date)): | if isinstance(obj, (datetime.datetime, datetime.date)): | ||||
return obj.isoformat() | return obj.isoformat() | ||||
if isinstance(obj, datetime.timedelta): | if isinstance(obj, datetime.timedelta): | ||||
return str(obj) | return str(obj) | ||||
# Let the base class default method raise the TypeError | # Let the base class default method raise the TypeError | ||||
return super().default(obj) | return super().default(obj) | ||||
Show All 10 Lines | |||||
class MsgpackFormatter(Formatter): | class MsgpackFormatter(Formatter): | ||||
format = 'msgpack' | format = 'msgpack' | ||||
mimetypes = ['application/x-msgpack'] | mimetypes = ['application/x-msgpack'] | ||||
def render(self, obj): | def render(self, obj): | ||||
return msgpack_dumps(obj) | return msgpack_dumps(obj) | ||||
# base API classes | |||||
class RemoteException(Exception): | class RemoteException(Exception): | ||||
pass | pass | ||||
def remote_api_endpoint(path): | def remote_api_endpoint(path): | ||||
def dec(f): | def dec(f): | ||||
f._endpoint_path = path | f._endpoint_path = path | ||||
return f | return f | ||||
▲ Show 20 Lines • Show All 226 Lines • Show Last 20 Lines |
Should be in negotiate.py (it makes sense logically, and it will get the required copyright notice.)