from hashlib import md5

from aiohttp import ClientSession

from app.models import Call
from app.settings import TELEPHONY_URL, ENVIRONMENT
from app.utils.logging import get_logger

logger = get_logger('root')

try:
    import ujson as json
except ImportError:
    import json


async def send_event(call: Call):
    if ENVIRONMENT == 'test' or not TELEPHONY_URL:
        return

    data = {
        'id': f'{call.id}',
        'call_type': call.call_type,
        'state': call.state,
        'sign': call.sign,
    }
    async with ClientSession(json_serialize=json.dumps) as session:
        async with session.post(TELEPHONY_URL, json=data) as resp:
            if resp.status >= 400:
                logger.warning(f'Telephony event is not send. status: {resp.status}')
import hashlib
from flask import request, abort

from app.config import config
from app.http.controllers.base import (
    BaseController,
    RequestType,
    invalid_return
)

from app.utils.time import get_current_timestamp
from app.utils.encryption import md5
from app.utils.exceptions import HTTPException
from app.utils.logging import get_logger

logger = get_logger('Controller.Protected.Base')


class BaseProtectedController(BaseController):
    def _route(self, request_type, action, **kwargs):
        if action is not None and action.startswith('_'):
            abort(403)

        if action is None:
            action = 'index'

        if request_type == RequestType.GET:
            route_action = action
        else:
            route_action = '_{}_{}'.format(request_type, action)
import copy
import hashlib
from flask import request, abort

from app.config import config
from app.http.controllers.base import (BaseController, RequestType,
                                       invalid_return)

from app.utils.time import get_current_timestamp
from app.utils.encryption import md5
from app.utils.exceptions import HTTPException
from app.utils.logging import get_logger

logger = get_logger('Controller.Protected.Base')


class BaseProtectedController(BaseController):
    def _route(self, request_type, action, **kwargs):
        if action is not None and action.startswith('_'):
            abort(403)

        if action is None:
            action = 'index'

        if request_type == RequestType.GET:
            route_action = action
        else:
            route_action = '_{}_{}'.format(request_type, action)

        m = getattr(self, route_action)
        try:
Example #4
0
import os
from urllib.request import url2pathname

from SPARQLWrapper.SPARQLExceptions import QueryBadFormed
from flask import Flask, jsonify, request, g

from app.apiparams.mapping import map_param_values_to_given_definitions
from app.clients.elastic.client import ESClient
from app.clients.sparql.client import SPARQLClient
from app.utils import constants
from app.utils import logging
from exceptions.clientexceptions import NoResultsFoundError, InvalidClientName
from exceptions.helpers import log_last_exception
from exceptions.queryexceptions import InvalidInputQuery

logger = logging.get_logger(__name__)

PORT = int(os.getenv("PORT", constants.DEFAULT_HTTP_PORT))
DB_ENDPOINT = os.getenv('DB_ENDPOINT', constants.DEFAULT_DB_ENDPOINT)
DB_USER = os.getenv('DB_USER')
DB_PASS = os.getenv('DB_PASS')
DB_CLIENT = os.getenv('DB_CLIENT', constants.DEFAULT_DB_CLIENT)
logger.info(
    f'Using credentials:\n endpoint: {DB_ENDPOINT}\n user: {DB_USER}\n pass: {DB_PASS}'
)

db_client_classes = {'stardog': SPARQLClient, 'elasticsearch': ESClient}

app = Flask(__name__)

from aio_pika import IncomingMessage

from app.event_handlers import process_event
from app.settings import MQ_EVENTS_QUEUE_NAME, EVENT_CAPTURE
from app.misc.types import FSEvent
from .base import BaseQueueService
from app.utils.logging import get_logger, capture_message

logger = get_logger('root')
mq_log = get_logger('amqp')


class AMQPService(BaseQueueService):
    queue_name = MQ_EVENTS_QUEUE_NAME

    async def process(self, message: IncomingMessage):
        async with message.process():
            try:
                if EVENT_CAPTURE:
                    await capture_message(message.body.decode())

                event = FSEvent(message.body)
                await process_event(event, self.rabbit_mq)
            except Exception as e:
                logger.warning(f'Exception on process event: {e}')
                mq_log.warning(message.body.decode())