async def handler(cls, request, index='', path='', token=None): uri = f'{cls.__host__}/{index}/{path}' # Elasticsearch collapses // to / headers = { 'Accept': 'application/json', 'Content-Type': 'application/json' } async with aiohttp.ClientSession() as session: async with session.request(request.method, uri, data=request.body, headers=headers) as response: try: return json(await response.json(), headers={ 'Access-Control-Allow-Origin': CORS.get_origins() }) except aiohttp.ContentTypeError: return text(await response.text(), headers={ 'Access-Control-Allow-Origin': CORS.get_origins() })
def test_set_origins(self): CORS.set_origins('*') self.assertEqual(CORS.get_origins(), '*')
def test_get_origins(self): self.assertEqual(CORS.get_origins(), '')
import os from sanic import Sanic from sugar_api import CORS, Redis from sugar_odm import MongoDB CORS.set_origins('*') server = Sanic('application-name') @server.listener('before_server_start') async def before_server_start(app, loop): MongoDB.set_event_loop(loop) await Redis.set_event_loop(loop) Redis.default_connection(host=os.getenv('REDIS_URI', 'redis://localhost'), minsize=os.getenv('REDIS_POOL_MIN', 5), maxsize=os.getenv('REDIS_POOL_MAX', 10)) @server.listener('before_server_stop') async def before_server_stop(app, loop): MongoDB.close() await Redis.close()