Skip to content
Snippets Groups Projects
Select Git revision
  • a6825a1dfd5cdc787f10d924f1e59e24fec9a544
  • develop default
  • master protected
  • inventoryProvider-functional
  • inventoryProvider-morework2
  • circuit-service-details-fix
  • lookup-SPECTRUM-SCHF-ports
  • inventoryProvider-1267-cleanup
  • inventoryProvider-moreWork
  • feature/DBOARD3-958
  • release/0.110
  • fix-uuid-validation-error
  • docker-poc
  • 0.160
  • 0.159
  • 0.158
  • 0.157
  • 0.156
  • 0.155
  • 0.154
  • 0.153
  • 0.152
  • 0.151
  • 0.150
  • 0.149
  • 0.148
  • 0.147
  • 0.146
  • 0.145
  • 0.144
  • 0.143
  • 0.142
  • 0.141
33 results

install-sdist-to-test.py

Blame
  • api.py 1.98 KiB
    """
    API Endpoints
    =========================
    
    .. contents:: :local:
    
    /api/things
    ---------------------
    
    .. autofunction:: compendium_v2.routes.api.things
    
    """
    import binascii
    import hashlib
    import random
    import time
    
    from flask import Blueprint, jsonify
    from compendium_v2.routes import common
    
    routes = Blueprint("compendium-v2-api", __name__)
    
    
    THING_LIST_SCHEMA = {
        '$schema': 'http://json-schema.org/draft-07/schema#',
    
        'definitions': {
            'thing': {
                'type': 'object',
                'properties': {
                    'id': {'type': 'string'},
                    'time': {'type': 'number'},
                    'state': {'type': 'boolean'},
                    'data1': {'type': 'string'},
                    'data2': {'type': 'string'},
                    'data3': {'type': 'string'}
                },
                'required': ['id', 'time', 'state', 'data1', 'data2', 'data3'],
                'additionalProperties': False
            }
        },
    
        'type': 'array',
        'items': {'$ref': '#/definitions/thing'}
    }
    
    
    @routes.after_request
    def after_request(resp):
        return common.after_request(resp)
    
    
    @routes.route("/things", methods=['GET', 'POST'])
    @common.require_accepts_json
    def things():
        """
        handler for /api/things requests
    
        response will be formatted as:
    
        .. asjson::
            compendium_v2.routes.api.THING_LIST_SCHEMA
    
        :return:
        """
    
        def _hash(s, length):
            m = hashlib.sha256()
            m.update(s.encode('utf-8'))
            digest = binascii.b2a_hex(m.digest()).decode('utf-8')
            return digest[-length:].upper()
    
        def _make_thing(idx):
            six_months = 24 * 3600 * 180
            return {
                'id': _hash(f'id-{idx}', 4),
                'time': int(time.time() + random.randint(-six_months, six_months)),
                'state': bool(idx % 2),
                'data1': _hash(f'data1-{idx}', 2),
                'data2': _hash(f'data2-{idx}', 8),
                'data3': _hash(f'data3-{idx}', 32)
            }
    
        response = map(_make_thing, range(20))
        return jsonify(list(response))