コード例 #1
0
class UnwiredlabsV1ResultSchema(RenamingMappingSchema):

    status = colander.SchemaNode(colander.String())
    message = colander.SchemaNode(colander.String(), missing=None)
    lat = colander.SchemaNode(BoundedFloat(), missing=None)
    lon = colander.SchemaNode(BoundedFloat(), missing=None)
    accuracy = colander.SchemaNode(colander.Float(), missing=None)
    fallback = OptionalNode(colander.String(), missing=None)

    def deserialize(self, data):
        data = super(UnwiredlabsV1ResultSchema, self).deserialize(data)

        # The API always returns 200 Ok responses, but uses the
        # status/message fields to indicate failures.
        status = data.get('status', None)
        if status != 'ok':
            message = data.get('message', None)
            if message == 'No matches found':
                # Fabricate a not found
                return {
                    'accuracy': None,
                    'fallback': None,
                    'lat': None,
                    'lon': None,
                }

            raise colander.Invalid('Error response, message: %s' % message)

        # Check required fields for status==ok responses
        for field in ('lat', 'lon', 'accuracy'):
            if data.get(field, None) is None:
                raise colander.Invalid('Missing required field: %s' % field)

        fallback = data.get('fallback', None)
        if fallback != 'lacf':
            fallback = None

        return {
            'accuracy': data['accuracy'],
            'fallback': fallback,
            'lat': data['lat'],
            'lon': data['lon'],
        }
コード例 #2
0
class UnwiredlabsV1ResultSchema(RenamingMappingSchema):

    status = colander.SchemaNode(colander.String())
    message = colander.SchemaNode(colander.String(), missing=None)
    lat = colander.SchemaNode(BoundedFloat(), missing=None)
    lon = colander.SchemaNode(BoundedFloat(), missing=None)
    accuracy = colander.SchemaNode(colander.Float(), missing=None)
    fallback = OptionalNode(colander.String(), missing=None)

    def deserialize(self, data):
        data = super(UnwiredlabsV1ResultSchema, self).deserialize(data)

        # The API always returns 200 Ok responses, but uses the
        # status/message fields to indicate failures.
        status = data.get("status", None)
        if status != "ok":
            message = data.get("message", None)
            if message == "No matches found":
                # Fabricate a not found
                return {
                    "accuracy": None,
                    "fallback": None,
                    "lat": None,
                    "lon": None
                }

            raise colander.Invalid("Error response, message: %s" % message)

        # Check required fields for status==ok responses
        for field in ("lat", "lon", "accuracy"):
            if data.get(field, None) is None:
                raise colander.Invalid("Missing required field: %s" % field)

        fallback = data.get("fallback", None)
        if fallback != "lacf":
            fallback = None

        return {
            "accuracy": data["accuracy"],
            "fallback": fallback,
            "lat": data["lat"],
            "lon": data["lon"],
        }
コード例 #3
0
    class location(RenamingMappingSchema):  # NOQA

        lat = colander.SchemaNode(BoundedFloat())
        lng = colander.SchemaNode(BoundedFloat(), to_name='lon')
コード例 #4
0
ファイル: fallback.py プロジェクト: ingle/ichnaea
    class location(InternalMappingSchema):  # NOQA

        lat = InternalSchemaNode(BoundedFloat())
        lng = InternalSchemaNode(BoundedFloat(), internal_name='lon')