示例#1
0
def save(tango_addr):
    """Endpoint to map a Tango device address to the sender's IP address
    ---
    tags:
      - save
    parameters:
      - name: tango_addr
        in: path
        type: string
        required: true
    responses:
        200:
            description: Device address successfully saved.
            type: string
        500:
            description: Internal server error
            type: string
    """

    ip_addr = request.remote_addr
    existing_results = DbHelper.get(quote(tango_addr))
    if len(existing_results) == 0:
        if DbHelper.add(datetime.datetime.now(), quote(tango_addr),
                        ip_addr) == -1:
            return "Failed"
    else:
        if DbHelper.update(quote(tango_addr), datetime.datetime.now()) == -1:
            return "Failed"

    return "Successfully saved device address"
示例#2
0
def get(tango_addr):
    """Endpoint to get the IP Address of a saved Tango Device
    ---
    tags:
      - get_addr
    parameters:
      - name: tango_addr
        in: path
        type: string
        required: true
    responses:
      200:
        description: JSON with the Device address, IP Address and timestamp when the device was added
        content:
            'application/json':
                schema:
                    type: object
                    properties:
                        tango_addr:
                            type: string
                            description: Tango Device Address
                        ip_addr:
                            type: string
                            description: Device IP Address
                        timestamp:
                            type: string
                            description: Time when the device was added to the database
                    example:
                        tango_addr: 'test/device/10'
                        ip_addr: '127.0.0.1'
                        timestamp: 'Sun, 17 Sep 2017 16:04:39 GMT'

    """

    results = DbHelper.get(quote(tango_addr))
    results = list(results)

    if len(results) == 0:
        return "Does not exist"

    return jsonify([{
        'tango_addr': tango_addr,
        'ip_addr': e[2],
        'timestamp': e[0]
    } for e in results])