.. Author: Hossein Safavi

.. STATUS: draft

.. role:: red

.. _api_user:

####################
Public API Reference
####################

This API is meant to be publicly accessible. Calls are authenticated with personal API tokens (currently ``uuid``) which
is checked against a token table in the database.

.. attention:: There are few **known issues** with the current token authentication mechanism:

    1. There is currently no enforced **expiration** for tokens.
    2. There is currently no **throttling** for attempts at token generation.
    3. The password hash is being directly used for authentication.
    4. **Tokens** should be cached in a ``key-value`` store such as Redis.
    5. There is no **early rejection** through signed authorization tokens


In order to run against this API, we recommend the following Python packages be installed: ``requests`` ``passlib`` ``json``


.. http:get:: /api_token?email=(email)&password_hash=(password_hash)&experiment_id=(experiment_id)


   Generates a new API token using the supplied credentials. A user participating in multiple experiments should
   obtain a new token for each experiment.

   :param email: The email address of the user requesting the authentication token.
   :type email: string
   :param password_hash: The hashed password of the user requesting the authentication token (as enccoded by `passlib.sha256` .
   :type password_hash: string
   :param experiment_id: The numeric id of the experiment for which an authentication token is requested
   :type experiment_id: int


   :resheader Content-Type: application/json
   :statuscode 200: no error
   :statuscode 401: incorrect user email, password hash, or experiment_id

   **Example response**:
    .. sourcecode:: http

      HTTP/1.1 200 OK
      Vary: Accept
      Content-Type: application/json

    .. sourcecode:: javascript

        [
            "status": {
                "text": "OK",
                "code": 200
            },
            "content": {
                "token": "abcdefghijklmnopqrstuvwxyz123456"
            }
        ]



.. http:get:: /device_recording/list


   Generates a new API token using the supplied credentials

   :param access_token: The authorized access token obtained through re
   :type access_token: string
   :param device_recording_id: (optional)
   :type device_recording_id: int
   :param participant_id: (optional)
   :type participant_id: int
   :param experiment_id: (optional)
   :type experiment_id: int
   :param device_serial_number: (optional)
   :type device_serial_number: string
   :param device_allocation_id: (optional)
   :type device_allocation_id: int


   :resheader Content-Type: application/json
   :statuscode 200: no error
   :statuscode 401: incorrect user email, password hash, or experiment_id

   **Example response**:
    .. sourcecode:: http

      HTTP/1.1 200 OK
      Vary: Accept
      Content-Type: application/json

    .. sourcecode:: javascript

        [
            "status": {
                "text": "OK",
                "code": 200
            },
            "content": [
                {
                    "participant": {
                        "utc_offset_minutes": null,
                        "company_specific_id": "patient_343424234",
                        "group": 1,
                        "id": 1,
                        "user": 12
                    },
                    "device_type": {
                        "id": 1,
                        "name": "empatica"
                    },
                    "device_recording": {
                        "status": {
                            "_type": "<enum 'RecordingStatusEnum'>",
                            "value": "UPLOADED"
                        },
                        "device_assignment": 9,
                        "box_allocation": 1,
                        "notes": null,
                        "start_time": {
                            "_type": "Timestamp",
                            "value": "2017-01-06 14:15:18.000000"
                        },
                        "applied_offset": 0,
                        "end_time": {
                            "_type": "Timestamp",
                            "value": "2017-01-06 15:31:58.920000"
                        },
                        "id": 7,
                        "requested_offset": 0
                    }
                }
            ]


.. http:get:: /signal_recording/list


   Provides a listing of all signals collected by the experiment filtered by the **optional** parameters.

   :param access_token: The authorized access token obtained through re
   :type access_token: string
   :param device_recording_id: (optional)
   :type device_recording_id: int
   :param participant_id: (optional)
   :type participant_id: int
   :param experiment_id: (optional)
   :type experiment_id: int
   :param device_serial_number: (optional)
   :type device_serial_number: string
   :param device_allocation_id: (optional)
   :type device_allocation_id: int
   :param device_id: (optional)
   :type device_id: int
   :param signal_type_name: (optional) one of ``ecg``, ``ppg``, ``accelerometer``, ``ibi``, ``heartrate``
   :type signal_type_name: string


   :resheader Content-Type: application/json
   :statuscode 200: no error
   :statuscode 401: incorrect user email, password hash, or experiment_id

   **Example response**:
    .. sourcecode:: http

      HTTP/1.1 200 OK
      Vary: Accept
      Content-Type: application/json

    .. sourcecode:: javascript

        [
            "status": {
                "text": "OK",
                "code": 200
            },
            "content": [
                {
                    "signal_recording": {
                        "status": {
                            "_type": "<enum 'RecordingStatusEnum'>",
                            "value": "UPLOADED"
                        },
                        "sampling_rate_override": null,
                        "start_time": {
                            "_type": "Timestamp",
                            "value": "2017-01-06 14:15:18.000000"
                        },
                        "device_signal": 1,
                        "end_time": {
                            "_type": "Timestamp",
                            "value": "2017-01-06 15:31:58.920000"
                        },
                        "id": 8,
                        "device_recording": 7
                    },
                    "device_signal": {
                        "units": "mV",
                        "dimensions": 1,
                        "alias": null,
                        "device_type": 1,
                        "signal_type": 1,
                        "id": 1,
                        "storage_sampling_rate": 100
                    },
                    "participant": {
                        "utc_offset_minutes": null,
                        "company_specific_id": "SeizeIT Test Patient 3",
                        "group": 1,
                        "id": 3,
                        "user": 12
                    },
                    "signal_type": {
                        "type": {
                            "_type": "<enum 'SignalTypeEnum'>",
                            "value": "UNPROCESSED"
                        },
                        "id": 1,
                        "name": "ppg"
                    },
                    "device_recording": {
                        "status": {
                            "_type": "<enum 'RecordingStatusEnum'>",
                            "value": "UPLOADED"
                        },
                        "device_assignment": 9,
                        "box_allocation": 1,
                        "notes": null,
                        "start_time": {
                            "_type": "Timestamp",
                            "value": "2017-01-06 14:15:18.000000"
                        },
                        "applied_offset": 0,
                        "end_time": {
                            "_type": "Timestamp",
                            "value": "2017-01-06 15:31:58.920000"
                        },
                        "id": 7,
                        "requested_offset": 0
                    }
                }
            ]
        ]