示例#1
0
    def __init__(self,
                 connection='amqp:///',
                 name=None,
                 logger=None,
                 limit=None):
        """Initialization of Client instance

        :param connection: connection for broker
        :type connection: str, None, kombu.connections.Connection, dict
        """

        self.connection = self._get_connection(connection)
        self.exchanges = {}

        if name is None:
            try:
                name = '<client: {}>'.format(self.connection.as_uri())
            except:  # pragma: no cover
                # Errors with filesystem transport
                name = '<client: {}>'.format(self.connection.transport_cls)

        if logger is None:
            logger = get_logger(__name__)

        self.logger = InstanceLogger(self, logger)

        self.name = name
        self.logger.debug('%s built', self.name)

        if limit is None:
            # Set limit as global kombu limit.
            limit = pools.get_limit()
        self.limit = limit
        self.connections = pools.Connections(self.limit)
示例#2
0
    def __init__(self, connection='amqp:///', name=None, logger=None, limit=None):
        """Initialization of Client instance

        :param connection: connection for broker
        :type connection: str, None, kombu.connections.Connection, dict
        """

        self.connection = self._get_connection(connection)
        self.exchanges = {}

        if name is None:
            try:
                name = '<client: {}>'.format(self.connection.as_uri())
            except:  # pragma: no cover
                # Errors with filesystem transport
                name = '<client: {}>'.format(self.connection.transport_cls)

        if logger is None:
            logger = get_logger(__name__)

        self.logger = InstanceLogger(self, logger)

        self.name = name
        self.logger.debug('%s built', self.name)

        if limit is None:
            # Set limit as global kombu limit.
            limit = pools.get_limit()
        self.limit = limit
        self.connections = pools.Connections(self.limit)
示例#3
0
    def __init__(self, endpoint, ok_statuses=None, to_none_statuses=None,
                 empty_to_none=True, close_slash=True,
                 logger=None, name=None, keep_blank_values=True):
        """Create a client

        :param endpoint: str, ex. http://localhost:5000 or http://localhost:5000/api/
        :param ok_statuses: default - (200, 201, 202, ), status codes for "ok"
        :param to_none_statuses: statuses, for generate None as response, default - (404, )
        :param empty_to_none: boolean, default - True, if True - empty response will be generate None response (empty str, empty list, empty dict)
        :param close_slash: boolean, url += '/', if url.endswith != '/', default - True
        :param logger: logger instance
        :param name: name for client
        :type name: str
        """
        if name is None:
            name = '<client: {}>'.format(endpoint)

        if logger is None:
            logger = get_logger(__name__)

        self.logger = InstanceLogger(self, logger)
        if endpoint.endswith('/'):
            endpoint = endpoint[:-1]
        if ok_statuses is not None:
            self.ok_statuses = ok_statuses
        if to_none_statuses is not None:
            self.to_none_statuses = to_none_statuses
        self.empty_to_none = empty_to_none
        self.close_slash = close_slash
        parsed_url = urlparse.urlparse(endpoint)
        endpoint = self.get_endpoint_from_parsed_url(parsed_url)
        self.keep_blank_values = keep_blank_values
        self.endpoint = endpoint
        self.path = parsed_url.path
        self.query = urlparse.parse_qs(parsed_url.query,
                                       keep_blank_values=self.keep_blank_values)
        self.fragment = parsed_url.fragment
        self.params = parsed_url.params
        self.name = name
        self.logger.debug(
            'Client built, endpoint: "%s", path: "%s", query: %s, params: %s, fragment: %s',
            self.endpoint, self.path,
            self.query, self.params, self.fragment)
示例#4
0
    def __init__(self, connection='amqp:///', name=None, logger=None):
        """Initialization of Client instance

        :param connection: connection for broker
        :type connection: str, None, kombu.connections.Connection, dict
        """

        self.connection = self._get_connection(connection)
        self.exchanges = {}

        if name is None:
            name = '<client: {}>'.format(self.connection.as_uri())

        if logger is None:
            logger = get_logger(__name__)

        self.logger = InstanceLogger(self, logger)

        self.name = name
        self.logger.debug('Client built for connection: %s', self.connection.as_uri())
示例#5
0
    def test_client(self):
        from microservices.http.client import Client, ResponseError

        valid_method = 'GET'
        valid_url = 'http://*****:*****@endpoint:8000/test/1/'
        valid_json = None

        def test_request(instance, method, url, **kwargs):
            self.assertEqual(method, valid_method)
            self.assertEqual(url, valid_url)
            self.assertEqual(kwargs.get('json', None), valid_json)

        patch_requests(
            MockRequest(handler=test_request, _content=b'{"response": "tested"}',
                        status_code=200))

        client = Client('http://*****:*****@endpoint:8000/test/1')
        response = client.get()

        self.assertEqual(response, {'response': 'tested'})
        response = client.get(response_key='response')
        self.assertEqual(response, 'tested')
        self.assertEqual(client.endpoint, 'http://*****:*****@endpoint:8000')
        self.assertEqual(client.path, '/test/1')

        client = Client('http://*****:*****@endpoint:8000/test/1/')

        valid_url = 'http://*****:*****@endpoint:8000/test/1/jopa/'
        client.get('jopa', response_key='response')

        valid_json = {'test': 'tested'}
        valid_method = 'POST'
        valid_url = 'http://*****:*****@endpoint:8000/test/1/jopa/?test=tested'
        client.post('jopa', query={'test': 'tested'}, response_key='response',
                    data=valid_json)
        client.post('jopa', query={'test': 'tested'}, key='response',
                    data=valid_json)

        jopa = client.resource('jopa')
        jopa.post(query={'test': 'tested'}, key='response', data=valid_json)

        valid_url = 'http://*****:*****@endpoint:8000/test/1/jopa/1/2/3/?test=tested'
        jopa.post('1', '2', '3', query={'test': 'tested'}, key='response',
                  data=valid_json)
        jopa.post('1/2', '3', query={'test': 'tested'}, key='response',
                  data=valid_json)
        jopa.post('1/2', '3/', query={'test': 'tested'}, key='response',
                  data=valid_json)
        jopa1 = jopa.resource('1')
        jopa1.post('2', '3', query={'test': 'tested'}, key='response',
                   data=valid_json)

        client = Client('http://endpoint/', ok_statuses=(200, 202,),
                        to_none_statuses=(404,))
        valid_url = 'http://endpoint/'
        valid_json = None
        valid_method = 'GET'
        patch_requests(
            MockRequest(handler=test_request, _content=b'{"response": "tested"}',
                        status_code=404))
        client.get(key='response')
        client.get(key='response_no')
        patch_requests(
            MockRequest(handler=test_request, _content=b'{"response": "tested"}',
                        status_code=200))
        client.get(key='response')
        self.assertRaises(ResponseError, client.get, key='response_no')
        patch_requests(
            MockRequest(handler=test_request, _content=b'{"response": "tested"}',
                        status_code=500))
        self.assertRaises(ResponseError, client.get, key='response')

        patch_requests(
            MockRequest(handler=test_request, _content=b'{"response": ""}',
                        status_code=200))
        response = client.get(key='response')
        self.assertEqual(response, None)

        client = Client('http://endpoint/', empty_to_none=False)
        response = client.get(key='response')
        self.assertEqual(response, "")

        patch_requests(
            MockRequest(handler=test_request, _content=b'bad', status_code=200))
        self.assertRaises(ResponseError, client.get, key='response')

        patch_requests(MockRequest(handler=test_request, _content=b'["tested"]',
                                   status_code=200))
        response = client.get()
        self.assertEqual(response, ['tested'])

        from microservices.utils import get_logger

        logger = get_logger('test', 'jopa')

        self.assertEqual(logger.name, 'jopa.test')

        logger = get_logger('test', 'jopa', '_')

        self.assertEqual(logger.name, 'jopa_test')
示例#6
0
import socket

from kombu import Connection, Queue, Consumer
from kombu.utils import nested
from kombu.exceptions import MessageStateError
import six

from microservices.utils import get_logger
from microservices.helpers.logs import InstanceLogger

_logger = get_logger(__name__)

@six.python_2_unicode_compatible
class Rule(object):
    """Rule"""

    def __init__(self, name, handler, logger, **options):
        """Initialization

        :param name: name of queue
        :param handler: handle for queue
        """
        self.handler = handler
        self.name = name
        self.options = options
        self.logger = InstanceLogger(self, logger)
        self._name = '<queue: {}>'.format(self.name)

    def __str__(self):
        return self._name
示例#7
0
import json

import six
from flask import current_app, request
from flask.json import JSONEncoder
from flask_api.renderers import BrowsableAPIRenderer, JSONRenderer

from microservices.http.helpers import url_resource, get_url_rule, \
    get_rule_resource
from microservices.utils import get_logger

logger = get_logger('Microservices renderers')


class SchemaRenderer(object):
    def __init__(self, options, resource, data, browser=False):
        self._schema = None
        self.options = options
        self.resource = resource
        self.data = data
        self.browser = browser

    @property
    def schema(self):
        if self._schema is None:
            self._schema = self._get_schema()
        return self._schema

    def _get_schema(self):
        schema = self.resource['schema']
        if self.browser:
from microservices.http.client import Client
from microservices.utils import set_logging, get_logger

set_logging(level='INFO')
logger = get_logger('microservices client')

client = Client(
    'http://localhost:5000/',
)

logger.info(client.get(key='response'))
logger.info(client.post(data={'test': 'tested'}))

second_resource = client.resource('second')

logger.info(second_resource.get(key='response'))
logger.info(second_resource.post(data={'test': 'tested'}))

logger.info(second_resource.get('test', key='response'))
logger.info(second_resource.post('test'))

one_two_resource = second_resource.resource('one', '2')
logger.info(one_two_resource.get(key='response'))
logger.info(one_two_resource.post(data={'test': 'tested'}))
示例#9
0
import json


from flask import request
from flask_api.renderers import JSONRenderer, BrowsableAPIRenderer
from flask.json import JSONEncoder
from flask import url_for
from werkzeug.routing import BuildError
from microservices.utils import get_logger
import six

logger = get_logger('Microservices renderers')

class MicroserviceRendererMixin(object):
    def pre_render(self, data, media_type, browser=False, **options):

        rule = None
        url_rule = request.url_rule
        if url_rule is not None:
            rule = url_rule.rule
        if rule is None:
            return data

        app = options.get('app', None)

        if app is None:
            return data

        if rule in app.resources:
            resource = app.resources[rule]
        else:
import gevent.pywsgi
gevent.pywsgi.MAX_REQUEST_LINE = 10000000000
from microservices.http.service import Microservice
from microservices.http.runners import gevent_run
from microservices.utils import set_logging, get_logger
from flask import request

set_logging()
logger = get_logger(__name__)

service = Microservice(__name__)

@service.route('/', methods=['POST', 'GET'])
def handle():

    return 'ok', 200

gevent_run(service, 5000, address='localhost', log=logger, error_log=logger)
示例#11
0
from microservices.queues.service import Microservice
from microservices.queues.client import Client
from microservices.utils import set_logging, get_logger

set_logging()
logger = get_logger('basic')

app = Microservice()


@app.queue('hello_world')
def hello_world(data, context):
    logger.info('Data: %s', data)


client = Client()
client.declare_exchange('input', queues=[('hello_world', 'world')])
hello_world_e = client.exchange('input', routing_key='world')
hello_world_e.publish('Hello, world!')
hello_world_e.publish('2')
hello_world_e.publish('3')

app.run()
示例#12
0
from kombu import Connection
from kombu.pools import connections
from kombu import Exchange, Queue
from microservices.utils import get_logger
from microservices.helpers.logs import InstanceLogger
import six

_logger = get_logger(__file__)


class _exchange(object):
    """Exchange helper"""

    def __init__(self, client, name, routing_key=None, logger=None):
        """Initialization

        :param client: instance of client
        :type client: Client
        :param name: name of exchange
        :type name: str
        :param routing_key: routing key to queue
        :type routing_key: str or None
        """
        self.client = client
        self.name = name
        self.routing_key = routing_key
        if logger is None:
            logger = _logger
        self.logger = logger
        self.logger.debug('Exchange "%s" built, routing_key: %s', self.name,
                          self.routing_key if not self.routing_key is None else '')
示例#13
0
from kombu import Connection
from kombu import Exchange, Queue, pools
from microservices.utils import get_logger
from microservices.helpers.logs import InstanceLogger
import six

_logger = get_logger(__file__)


class _exchange(object):
    """Exchange helper"""
    def __init__(self, client, name, routing_key=None, logger=None):
        """Initialization

        :param client: instance of client
        :type client: Client
        :param name: name of exchange
        :type name: str
        :param routing_key: routing key to queue
        :type routing_key: str or None
        """
        self.client = client
        self.name = name
        self.routing_key = routing_key
        if logger is None:
            logger = _logger  # pragma: no cover
        self.logger = logger
        self.logger.debug(
            'Exchange "%s" built, routing_key: %s', self.name,
            self.routing_key if not self.routing_key is None else '')
示例#14
0
    def test_client(self):
        from microservices.http.client import Client, ResponseError

        valid_method = 'GET'
        valid_url = 'http://*****:*****@endpoint:8000/test/1/'
        valid_json = None

        def test_request(instance, method, url, **kwargs):
            self.assertEqual(method, valid_method)
            self.assertEqual(url, valid_url)
            self.assertEqual(kwargs.get('json', None), valid_json)

        patch_requests(
            MockRequest(handler=test_request,
                        _content=b'{"response": "tested"}',
                        status_code=200))

        client = Client('http://*****:*****@endpoint:8000/test/1')
        response = client.get()

        self.assertEqual(response, {'response': 'tested'})
        response = client.get(response_key='response')
        self.assertEqual(response, 'tested')
        self.assertEqual(client.endpoint, 'http://*****:*****@endpoint:8000')
        self.assertEqual(client.path, '/test/1')

        client = Client('http://*****:*****@endpoint:8000/test/1/')

        valid_url = 'http://*****:*****@endpoint:8000/test/1/jopa/'
        client.get('jopa', response_key='response')

        valid_json = {'test': 'tested'}
        valid_method = 'POST'
        valid_url = 'http://*****:*****@endpoint:8000/test/1/jopa/?test=tested'
        client.post('jopa',
                    query={'test': 'tested'},
                    response_key='response',
                    data=valid_json)
        client.post('jopa',
                    query={'test': 'tested'},
                    key='response',
                    data=valid_json)

        jopa = client.resource('jopa')
        jopa.post(query={'test': 'tested'}, key='response', data=valid_json)

        valid_url = 'http://*****:*****@endpoint:8000/test/1/jopa/1/2/3/?test=tested'
        jopa.post('1',
                  '2',
                  '3',
                  query={'test': 'tested'},
                  key='response',
                  data=valid_json)
        jopa.post('1/2',
                  '3',
                  query={'test': 'tested'},
                  key='response',
                  data=valid_json)
        jopa.post('1/2',
                  '3/',
                  query={'test': 'tested'},
                  key='response',
                  data=valid_json)
        jopa1 = jopa.resource('1')
        jopa1.post('2',
                   '3',
                   query={'test': 'tested'},
                   key='response',
                   data=valid_json)

        client = Client('http://endpoint/',
                        ok_statuses=(
                            200,
                            202,
                        ),
                        to_none_statuses=(404, ))
        valid_url = 'http://endpoint/'
        valid_json = None
        valid_method = 'GET'
        patch_requests(
            MockRequest(handler=test_request,
                        _content=b'{"response": "tested"}',
                        status_code=404))
        client.get(key='response')
        client.get(key='response_no')
        patch_requests(
            MockRequest(handler=test_request,
                        _content=b'{"response": "tested"}',
                        status_code=200))
        client.get(key='response')
        self.assertRaises(ResponseError, client.get, key='response_no')
        patch_requests(
            MockRequest(handler=test_request,
                        _content=b'{"response": "tested"}',
                        status_code=500))
        self.assertRaises(ResponseError, client.get, key='response')

        patch_requests(
            MockRequest(handler=test_request,
                        _content=b'{"response": ""}',
                        status_code=200))
        response = client.get(key='response')
        self.assertEqual(response, None)

        client = Client('http://endpoint/', empty_to_none=False)
        response = client.get(key='response')
        self.assertEqual(response, "")

        patch_requests(
            MockRequest(handler=test_request, _content=b'bad',
                        status_code=200))
        self.assertRaises(ResponseError, client.get, key='response')

        patch_requests(
            MockRequest(handler=test_request,
                        _content=b'["tested"]',
                        status_code=200))
        response = client.get()
        self.assertEqual(response, ['tested'])

        from microservices.utils import get_logger

        logger = get_logger('test', 'jopa')

        self.assertEqual(logger.name, 'jopa.test')

        logger = get_logger('test', 'jopa', '_')

        self.assertEqual(logger.name, 'jopa_test')
示例#15
0
import gevent.monkey

gevent.monkey.patch_all()

from microservices.queues.service import Microservice
from microservices.queues.client import Client
from microservices.utils import set_logging, get_logger
from microservices.queues.runners import gevent_run
from microservices.http.client import Client as HTTPClient
import time
import logging

set_logging('INFO')
logger = get_logger(__name__)
mlo = get_logger('microservices.http.client')
mlo.setLevel(logging.ERROR)

service = Microservice()
timeout = 10
http_client = HTTPClient('http://*****:*****@service.queue('test')
def handle(payload, context):
    global timeout
    logger.info('Start handling: %s, timeout=%s', payload, timeout)
    start_time = time.time()
    a = 0
    while time.time() - start_time < timeout:
示例#16
0
import gevent.monkey
from microservices.utils import set_logging, get_logger

set_logging()
logger = get_logger('Gevent Service Queue')

gevent.monkey.patch_all()

from microservices.queues.runners import gevent_run
from microservices.queues.service import Microservice
from microservices.queues.client import Client


app1 = Microservice()
app2 = Microservice()
app3 = Microservice()
app4 = Microservice()

@app1.queue('gevent_basic1')
@app2.queue('gevent_basic2')
@app3.queue('gevent_basic3')
def gevent_basic(payload, context):
    logger.info(
        'Payload: %s, queue name: %s',
        payload,
        context.rule.name,
    )

gevent_run(app1, start=False, monkey_patch=False, debug=True)
gevent_run(app2, start=False, monkey_patch=False, debug=True)
gevent_run(app3, start=False, monkey_patch=False, debug=True)
import gevent.monkey
from microservices.utils import set_logging, get_logger

set_logging()
logger = get_logger('Gevent Service Queue')

gevent.monkey.patch_all()

from microservices.queues.runners import gevent_run
from microservices.queues.service import Microservice
from microservices.queues.client import Client

app1 = Microservice()
app2 = Microservice()
app3 = Microservice()
app4 = Microservice()


@app1.queue('gevent_basic1')
@app2.queue('gevent_basic2')
@app3.queue('gevent_basic3')
def gevent_basic(payload, context):
    logger.info(
        'Payload: %s, queue name: %s',
        payload,
        context.rule.name,
    )


gevent_run(app1, start=False, monkey_patch=False, debug=True)
gevent_run(app2, start=False, monkey_patch=False, debug=True)