def add_url_rule(self, rule, endpoint=None, view_func=None, **options): """A helper method to register a rule (and optionally a view function) to the application. The endpoint is automatically prefixed with the blueprint's name. The URL rule is registered twice. """ # Static assets are not localized if endpoint == 'static': return super(I18nBlueprintSetupState, self).add_url_rule(rule, endpoint=endpoint, view_func=view_func, **options) if self.url_prefix: rule = self.url_prefix + rule options.setdefault('subdomain', self.subdomain) if endpoint is None: endpoint = _endpoint_from_view_func(view_func) defaults = self.url_defaults if 'defaults' in options: defaults = dict(defaults, **options.pop('defaults')) # Handle an optionnal cors=True parameter options.setdefault('cors', False) if options.pop('cors', False): methods = options.get('methods', ['GET']) if 'HEAD' not in methods: methods.append('HEAD') if 'OPTIONS' not in methods: methods.append('OPTIONS') decorator = cors.crossdomain('*', headers='*', credentials=True) view_func = decorator(view_func) options['methods'] = methods if options.pop('localize', True): self.app.add_url_rule('/<lang:lang_code>' + rule, '%s.%s' % (self.blueprint.name, endpoint), view_func, defaults=defaults, **options) self.app.add_url_rule(rule, '%s.%s_redirect' % (self.blueprint.name, endpoint), redirect_to_lang, defaults=defaults, **options) else: self.app.add_url_rule(rule, '%s.%s' % (self.blueprint.name, endpoint), view_func, defaults=defaults, **options) self.app.add_url_rule('/<lang:lang_code>' + rule, '%s.%s_redirect' % (self.blueprint.name, endpoint), redirect_to_unlocalized, defaults=defaults, **options)
def add_url_rule(self, rule, endpoint=None, view_func=None, **options): """A helper method to register a rule (and optionally a view function) to the application. The endpoint is automatically prefixed with the blueprint's name. The URL rule is registered twice. """ # Static assets are not localized if endpoint == 'static': return super(I18nBlueprintSetupState, self).add_url_rule( rule, endpoint=endpoint, view_func=view_func, **options) if self.url_prefix: rule = self.url_prefix + rule options.setdefault('subdomain', self.subdomain) if endpoint is None: endpoint = _endpoint_from_view_func(view_func) defaults = self.url_defaults if 'defaults' in options: defaults = dict(defaults, **options.pop('defaults')) # Handle an optionnal cors=True parameter options.setdefault('cors', False) if options.pop('cors', False): methods = options.get('methods', ['GET']) if 'HEAD' not in methods: methods.append('HEAD') if 'OPTIONS' not in methods: methods.append('OPTIONS') decorator = cors.crossdomain('*', headers='*', credentials=True) view_func = decorator(view_func) options['methods'] = methods if options.pop('localize', True): self.app.add_url_rule('/<lang:lang_code>' + rule, '%s.%s' % (self.blueprint.name, endpoint), view_func, defaults=defaults, **options) self.app.add_url_rule( rule, '%s.%s_redirect' % (self.blueprint.name, endpoint), redirect_to_lang, defaults=defaults, **options) else: self.app.add_url_rule(rule, '%s.%s' % (self.blueprint.name, endpoint), view_func, defaults=defaults, **options) self.app.add_url_rule( '/<lang:lang_code>' + rule, '%s.%s_redirect' % (self.blueprint.name, endpoint), redirect_to_unlocalized, defaults=defaults, **options)
def __init__(self): logging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s') logging.root.setLevel(level=logging.INFO) logging.basicConfig(level=logging.ERROR) logger = logging.getLogger(__name__) self._logger = logger self.app = Flask(__name__) self.app.wsgi_app = ReverseProxied(self.app.wsgi_app) self.api = Api( self.app, version='0.1', title='OD2WD API', description='Mapping Open Data to Wikidata', # doc = environment_config["swagger-url"] ) self.api.decorators = [cors.crossdomain(origin='*')] self.ns = self.api.namespace('main') self.load_logger() self.load_index() self.load_model()
from mlab_api.endpoints.locations import LOCATIONS_NS from mlab_api.endpoints.debug import DEBUG_NS from mlab_api.endpoints.clients import CLIENT_ASN_NS from mlab_api.endpoints.servers import SERVER_ASN_NS from mlab_api.endpoints.raw import RAW_NS from mlab_api.decorators import format_from_url_decorator, download_decorator # API is defined here from mlab_api.rest_api import API ROOT = logging.getLogger() ROOT.setLevel(logging.DEBUG) # This provides CORS for all API Requests and adds in our media type coercing # based on `format` API.decorators = [cors.crossdomain(origin='*'), format_from_url_decorator, download_decorator] # Add namespaces defined in endpoints module API.add_namespace(LOCATIONS_NS) API.add_namespace(CLIENT_ASN_NS) API.add_namespace(SERVER_ASN_NS) API.add_namespace(RAW_NS) # init API with Flask App API.init_app(app) DEBUG_FLAG = False API_MODE = os.environ.get("API_MODE") print(API_MODE)
from mlab_api.endpoints.debug import debug_ns from mlab_api.endpoints.clients import client_asn_ns from mlab_api.endpoints.servers import server_asn_ns from mlab_api.endpoints.raw import raw_ns from mlab_api.decorators import format_from_url_decorator, download_decorator # API is defined here from mlab_api.rest_api import api root = logging.getLogger() root.setLevel(logging.DEBUG) # This provides CORS for all API Requests and adds in our media type coercing based on `format` api.decorators = [ cors.crossdomain(origin='*'), format_from_url_decorator, download_decorator ] # Add namespaces defined in endpoints module api.add_namespace(locations_ns) api.add_namespace(client_asn_ns) api.add_namespace(server_asn_ns) api.add_namespace(raw_ns) # init api with Flask App api.init_app(app) debug_flag = False if app.config['API_MODE'] == 'DEV': print('DEV MODE')
default=1, location='args', help='The page to fetch') parser.add_argument('page_size', type=int, default=20, location='args', help='The page size to fetch') return parser api = UDataApi(apiv1, decorators=[ csrf.exempt, cors.crossdomain(origin='*', credentials=True, headers=PREFLIGHT_HEADERS) ], version='1.0', title='uData API', description='uData API', default='site', default_label='Site global namespace') api.model_reference = api.model( 'ModelReference', { 'class': fields.ClassName(description='The model class', required=True), 'id': fields.String(description='The object identifier', required=True), })
return response def page_parser(self): parser = self.parser() parser.add_argument('page', type=int, default=1, location='args', help='The page to fetch') parser.add_argument('page_size', type=int, default=20, location='args', help='The page size to fetch') return parser api = UDataApi( apiv1, decorators=[csrf.exempt, cors.crossdomain(origin='*', credentials=True, headers=PREFLIGHT_HEADERS ) ], version='1.0', title='uData API', description='uData API', default='site', default_label='Site global namespace' ) api.model_reference = api.model('ModelReference', { 'class': fields.ClassName(description='The model class', required=True), 'id': fields.String(description='The object identifier', required=True), }) @api.representation('application/json')
from flask import current_app from flask_restplus import Api, Resource, cors, fields api = Api( title='OFSearch API', version='1.0', description='Search and consult french training organizations', decorators=[cors.crossdomain(origin='*', credentials=True)], ) parser = api.parser() parser.add_argument('q', type=str, help='The search query', required=True) parser.add_argument('page', type=int, help='Page to display', default=1) parser.add_argument('limit', type=int, help='Max number of results per page', default=20) specialty = api.model('Specialty', { 'code': fields.Integer, 'trainees': fields.Integer, 'hours': fields.Integer, }) organization = api.model( 'Organization', { 'numero_de_da': fields.String, 'form_total': fields.Integer, 'da_siren': fields.String, 'da_no_etab': fields.String, 'da_raison_sociale': fields.String,
from parameters import config authorizations = { 'apiKey': { 'type': 'apiKey', 'in': 'header', 'name': 'x-access-token', }, } app = Flask(__name__) api = Api(app, version = config['api']['version'], title = config['api']['name'], description = config['api']['description'], authorizations=authorizations) app.config["RESTPLUS_JSON"] = {"cls": JSONEncoder} api.decorators = [cors.crossdomain( origin='*', headers=['accept', '*'] )] api.add_namespace(security_resources) api.add_namespace(user_resource) api.add_namespace(heating_resource) api.add_namespace(heating_schedule_resource, '/heating') app.run(host="0.0.0.0")