Example #1
0
class SquareAction(Action):
    """
    This is an example of a simple action which takes a single input argument - a number to square - and returns the
    square of it.
    """

    # The SOA library enables validation of both requests and responses using the "conformity" library. Simple schemas
    # can be specified inline like this; more complex ones should live in a separate schemas/ package (see other
    # examples).
    request_schema = fields.Dictionary({
        # Request bodies are always dictionaries. Here we say we want a dict with exactly one input - a float called
        # "number"
        'number': fields.Float(),
    })

    response_schema = fields.Dictionary({
        'square': fields.Float(),
    })

    def run(self, request):
        """
        When an action is run, the request is validated, and then passed to you here in the run() method, where you can
        perform the business logic.
        """
        # Request body is a dictionary, so it's easy to pull out fields.
        square = request.body['number'] ** 2
        # Return the result
        return {
            'square': square,
        }
 def test_schema_correct(self):
     assert SettingsOneThree.schema == {
         'foo': fields.UnicodeString(),
         'bar': fields.Boolean(),
         'baz': fields.List(fields.Float()),
         'qux': fields.Float(),
     }
 def test_schema_correct(self):
     assert SettingsThreeTwoOne.schema == {
         'foo': fields.UnicodeString(),
         'bar': fields.Integer(),
         'baz': fields.List(fields.Float()),
         'qux': fields.Float(),
     }
class SettingsThree(Settings):
    schema = {
        'baz': fields.List(fields.Float()),
        'qux': fields.Float(),
    }  # type: SettingsSchema

    defaults = {
        'qux': 1.234,
    }  # type: SettingsData
class SettingsToTest(settings.Settings):
    schema: settings.SettingsSchema = {
        'one': fields.Dictionary({
            'a': fields.ClassConfigurationSchema(base_class=ClassUsingAttrs27HintsToTest, description='Nifty schema.'),
            'b': fields.PythonPath(value_schema=fields.UnicodeString(), description='Must be a path, yo.'),
            'c': fields.TypeReference(base_classes=ClassHoldingSigsToTest, description='Refer to that thing!'),
        }),
        'two': fields.SchemalessDictionary(key_type=fields.UnicodeString(), value_type=fields.Boolean()),
        'three': fields.List(fields.Integer()),
        'four': fields.Nullable(fields.Set(fields.ByteString())),
        'five': fields.Any(fields.Integer(), fields.Float()),
        'six': fields.ObjectInstance(valid_type=ClassUsingAttrs27HintsToTest, description='Y u no instance?'),
        'seven': fields.Polymorph(
            'thing',
            {
                'thing1': fields.Dictionary({'z': fields.Boolean()}, allow_extra_keys=True),
                'thing2': fields.Dictionary({'y': fields.Boolean()}, allow_extra_keys=True, optional_keys=('y', )),
            },
        ),
    }

    defaults: settings.SettingsData = {
        'one': {
            'b': 'foo.bar:Class',
        },
        'three': [1, 5, 7],
    }
 def test_schema_correct(self):
     assert SettingsOneTwoThreeWithOverrides.schema == {
         'foo': fields.UnicodeString(),
         'bar': fields.Boolean(),
         'baz': fields.ByteString(),
         'qux': fields.Float(),
     }
 def test_schema_correct(self):
     assert SettingsOneTwoThree.schema == {
         'foo':
         fields.UnicodeString(),
         'bar':
         fields.Boolean(),
         'baz':
         fields.Dictionary(
             {
                 'inner_foo':
                 fields.UnicodeString(),
                 'inner_bar':
                 fields.Boolean(),
                 'inner_baz':
                 fields.List(fields.Integer()),
                 'inner_qux':
                 fields.Dictionary(
                     {
                         'most_inner_foo': fields.Boolean(),
                         'most_inner_bar': fields.UnicodeString(),
                     }, ),
             }, ),
         'qux':
         fields.Float(),
     }
 def test_schema_correct(self):
     assert SettingsFour.schema == {
         'baz': fields.List(fields.Float()),
         'qux': fields.Decimal(),
         'new': fields.ByteString(),
         'old': fields.UnicodeString(),
     }
Example #9
0
class TypesEchoAction(Action):
    request_schema = fields.Dictionary(
        {
            'an_int': fields.Integer(),
            'a_float': fields.Float(),
            'a_bool': fields.Boolean(),
            'a_bytes': fields.ByteString(),
            'a_string': fields.UnicodeString(),
            'a_datetime': fields.DateTime(),
            'a_date': fields.Date(),
            'a_time': fields.Time(),
            'a_list': fields.List(fields.Anything(), max_length=0),
            'a_dict': fields.Nullable(fields.Dictionary({})),
        },
        optional_keys=(
            'an_int', 'a_float', 'a_bool', 'a_bytes', 'a_string', 'a_datetime', 'a_date', 'a_time', 'a_list', 'a_dict',
        ),
    )

    response_schema = fields.Dictionary(
        {'r_{}'.format(k): v for k, v in six.iteritems(request_schema.contents)},
        optional_keys=('r_{}'.format(k) for k in request_schema.optional_keys),
    )

    def run(self, request):
        return {'r_{}'.format(k): v for k, v in six.iteritems(request.body)}
Example #10
0
class WalkAction(Action):
    request_schema = fields.Dictionary({'value': fields.Any(fields.Integer(), fields.Float())})

    response_schema = request_schema

    add = 1

    def run(self, request):
        return {'value': request.body['value'] + self.add}
class CallServiceAction(Action):
    """
    This is an example of a simple action which chains down to call another service; in this case, ourselves, which
    is unusual, but it's an easier example than creating another service.
    """

    response_schema = fields.Dictionary({
        'square': fields.Float(),
    })

    def run(self, request):
        """
        When an action is run, the request is validated, and then passed to you here in the run() method, where you can
        perform the business logic.
        """
        # The client, which lets us call other services, is always available on the request object if any client routing
        # settings are configured in the service settings. In this case, the service is calling itself, which can only
        # work if there are two or more server processes running.
        result = request.client.call_action('example', 'square', body={'number': 42})

        return {
            'square': result.body['square'],
        }
 def test_schema_correct(self):
     assert SettingsThree.schema == {
         'baz': fields.List(fields.Float()),
         'qux': fields.Float(),
     }
Example #13
0
MAX_GIG_E_MTU_BYTES = 9000
MAX_FAST_E_MTU_BYTES = 1518
MAX_IPV4_PAYLOAD_SIZE_BYTES = MAX_IPV4_PACKET_SIZE_BYTES - IP_HEADER_BYTES - UDP_HEADER_BYTES
MAX_GIG_E_PAYLOAD_SIZE_BYTES = MAX_GIG_E_MTU_BYTES - IP_HEADER_BYTES - UDP_HEADER_BYTES
MAX_FAST_E_PAYLOAD_SIZE_BYTES = MAX_FAST_E_MTU_BYTES - IP_HEADER_BYTES - UDP_HEADER_BYTES


@fields.ClassConfigurationSchema.provider(fields.Dictionary(
    {
        'host': fields.UnicodeString(description='The host name or IP address on which the Statsd server is listening'),
        'port': fields.Integer(description='The port number on which the Statsd server is listening'),
        'maximum_packet_size': fields.Integer(
            description='The maximum packet size to send (packets will be fragmented above this limit), defaults to '
                        '65000 bytes.',
        ),
        'network_timeout': fields.Any(fields.Float(gt=0.0), fields.Integer(gt=0), description='The network timeout'),
    },
    optional_keys=('maximum_packet_size', 'network_timeout'),
))
class StatsdPublisher(MetricsPublisher):
    """
    A publisher that emits UDP metrics packets to a Statsd consumer over a network connection.

    For Statsd metric type suffixes, see https://github.com/etsy/statsd/blob/master/docs/metric_types.md.
    """

    METRIC_TYPE_COUNTER = b'c'
    METRIC_TYPE_GAUGE = b'g'
    METRIC_TYPE_HISTOGRAM = b'ms'
    METRIC_TYPE_TIMER = b'ms'
Example #14
0
from pymetrics.instruments import (
    Counter,
    Gauge,
    Histogram,
    Metric,
    Tag,
    Timer,
    TimerResolution,
)
from pymetrics.publishers.statsd import StatsdPublisher

__all__ = ('DogStatsdPublisher', )

_datadog_tags_value_type = fields.Nullable(
    fields.Any(fields.UnicodeString(), fields.ByteString(), fields.Integer(),
               fields.Float(), fields.Boolean()), )


@fields.ClassConfigurationSchema.provider(
    fields.Dictionary(
        {
            'host':
            fields.UnicodeString(
                description=
                'The host name or IP address on which the Dogstatsd server is listening',
            ),
            'port':
            fields.Integer(
                description=
                'The port number on which the Dogstatsd server is listening'),
            'maximum_packet_size':