Exemple #1
0
def handler(event, context):
    """Handler for the /verify endpoint.

        ===
        GET
        ===

        Verify a JWT token.

        :param event['request']['token']: Requesting client's JWT token.
        :type event['request']['token']: string
        :returns verified: A boolean value.  "true" if the JWT token is valid.
        :rtype: string

        :Example:

        curl -X GET -H 'Authorization: Bearer <JWT_TOKEN>' -H 'Cache-Control: no-cache' 'https://<server_addr>/verify'

        :raises UnableToPerformOperationError: If an invalid http_method is used to call the endpoint handler.

    """
    if 'request' not in event:
        raise UnableToPerformOperationError

    payload = login_check(event)

    http_method = event['request']['http_method']

    if http_method == 'GET':
        return "true"

    raise UnableToPerformOperationError
Exemple #2
0
def handler(event, context):
    """Handler for the /my_motors/{motor_id} endpoint.

        ======
        DELETE
        ======

        Remove a motor from the user's motor collection.

        :param event['motor_id']: Motor id.
        :param event['request']['token']: Requesting client's JWT token.
        :type event['motor_id']: string
        :type event['request']['token']: string

        :Example:

        curl -X DELETE -H 'Accept: application/json' -H 'Authorization: Bearer <JWT_TOKEN>' 'http://<server_addr>/my_motors/<motor_id>'

        :raises UnableToPerformOperationError: If an invalid http_method is used to call the endpoint handler.

    """
    if 'request' not in event:
        raise UnableToPerformOperationError

    payload = login_check(event)

    http_method = event['request']['http_method']

    if http_method == 'DELETE':
        user_model.del_motor_from_user_collection(event['motor_id'], users_table, payload)

        return None

    raise UnableToPerformOperationError
Exemple #3
0
def handler(event, context):
    """Handler for the /my_motors endpoint.

        ===
        GET
        ===

        Get a JSON object of all of the motors in the user collection.

        :param event['request']['token']: Requesting client's JWT token.
        :type event['request']['token']: string
        :rtype: JSON

        :Example:

        curl -X GET -H 'Accept: application/json' -H 'Authorization: Bearer <JWT_TOKEN>' 'http://<server_addr>/my_motors

        ===
        PUT
        ===

        Add a motor to the user collection.

        :param event['motor']: Motor to add to the collection.
        :param event['request']['token']: Requesting client's JWT token.
        :type event['motor']: dict
        :type event['request']['token']: string

        :Example:

        curl -X PUT -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer <JWT_TOKEN>' -d '{
          "motor_id": "<string>"
        }' 'http://<server_addr>/my_motors'

        :raises UnableToPerformOperationError: If an invalid http_method is used to call the endpoint handler.

    """
    if 'request' not in event:
        raise UnableToPerformOperationError

    payload = login_check(event)

    http_method = event['request']['http_method']

    if http_method == 'GET':
        user = user_model.get_one(payload['sub'], users_table)

        return user['my_motors']

    elif http_method == 'PUT':
        user_model.add_motor_to_user_collection(event['motor'], event['delay'],
                                                users_table, payload)

        return None

    raise UnableToPerformOperationError
Exemple #4
0
def handler(event, context):
    """Handler for the /my_motors endpoint.

        ===
        GET
        ===

        Get a JSON object of all of the motors in the user collection.

        :param event['request']['token']: Requesting client's JWT token.
        :type event['request']['token']: string
        :rtype: JSON

        :Example:

        curl -X GET -H 'Accept: application/json' -H 'Authorization: Bearer <JWT_TOKEN>' 'http://<server_addr>/my_motors

        ===
        PUT
        ===

        Add a motor to the user collection.

        :param event['motor']: Motor to add to the collection.
        :param event['request']['token']: Requesting client's JWT token.
        :type event['motor']: dict
        :type event['request']['token']: string

        :Example:

        curl -X PUT -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer <JWT_TOKEN>' -d '{
          "motor_id": "<string>"
        }' 'http://<server_addr>/my_motors'

        :raises UnableToPerformOperationError: If an invalid http_method is used to call the endpoint handler.

    """
    if 'request' not in event:
        raise UnableToPerformOperationError

    payload = login_check(event)

    http_method = event['request']['http_method']

    if http_method == 'GET':
        user = user_model.get_one(payload['sub'], users_table)

        return user['my_motors']

    elif http_method == 'PUT':
        user_model.add_motor_to_user_collection(event['motor'], event['delay'], users_table, payload)

        return None

    raise UnableToPerformOperationError
Exemple #5
0
def handler(event, context):
    """Handler for the /motors/diameter/{diameter} endpoint.

        ===
        GET
        ===

        Get more information about motors given the motor diameter.

        :param event['diameter']: Motor Diameter.
        :param event['request']['token']: Requesting client's JWT token.
        :type event['diameter']: string
        :type event['request']['token']: string
        :returns motors: Motor objects.
        :rtype: JSON

        :Example:

        curl -X GET -H 'Accept: application/json' -H 'Authorization: Bearer <JWT_TOKEN>' 'http://<server_addr>/motors/diameter/<diameter>'

        :raises UnableToPerformOperationError: If an invalid http_method is used to call the endpoint handler.

    """
    if 'request' not in event:
        raise UnableToPerformOperationError

    payload = login_check(event)

    http_method = event['request']['http_method']

    if http_method == 'GET':
        url = "http://www.thrustcurve.org/servlets/search"
        request_data = '<search-request>' \
                       '    <diameter>' + event['diameter'] + '</diameter>' \
                       '    <max-results>2000</max-results>' \
                       '</search-request>'

        rawmotors = xmltodict.parse(requests.post(url, data=request_data).text)

        allmotors = {}

        for motor in rawmotors['search-response']['results']['result']:
            if motor['diameter'] not in allmotors:
                allmotors[motor['diameter']] = []

            allmotors[motor['diameter']].append(motor)

        return allmotors

    raise UnableToPerformOperationError
Exemple #6
0
def handler(event, context):
    """Handler for the /motors endpoint.

        ===
        GET
        ===

        Get more information about all the motors in the database.

        :param event['request']['token']: Requesting client's JWT token.
        :type event['request']['token']: string
        :returns motor: Motor object.
        :rtype: JSON

        :Example:

        curl -X GET -H 'Accept: application/json' -H 'Authorization: Bearer <JWT_TOKEN>' 'http://<server_addr>/motors'

        :raises UnableToPerformOperationError: If an invalid http_method is used to call the endpoint handler.

    """
    if 'request' not in event:
        raise UnableToPerformOperationError

    payload = login_check(event)

    http_method = event['request']['http_method']

    if http_method == 'GET':
        url = "http://www.thrustcurve.org/servlets/search"
        request_data = '<search-request>' \
                       '    <availability>all</availability>' \
                       '    <max-results>2000</max-results>' \
                       '</search-request>'

        rawmotors = xmltodict.parse(requests.post(url, data=request_data).text)

        allmotors = {}

        for motor in rawmotors['search-response']['results']['result']:
            if motor['diameter'] not in allmotors:
                allmotors[motor['diameter']] = []

            allmotors[motor['diameter']].append(motor)

        return allmotors

    raise UnableToPerformOperationError
Exemple #7
0
def handler(event, context):
    """Handler for the /motors/{motor_id} endpoint.

        ===
        GET
        ===

        Get more information about a motor given the motor_id.

        :param event['motor_id']: Motor id.
        :param event['request']['token']: Requesting client's JWT token.
        :type event['motor_id']: string
        :type event['request']['token']: string
        :returns motor: Motor object.
        :rtype: JSON

        :Example:

        curl -X GET -H 'Accept: application/json' -H 'Authorization: Bearer <JWT_TOKEN>' 'http://<server_addr>/motors/<motor_id>'

        :raises UnableToPerformOperationError: If an invalid http_method is used to call the endpoint handler.

    """
    if 'request' not in event:
        raise UnableToPerformOperationError

    payload = login_check(event)

    http_method = event['request']['http_method']

    if http_method == 'GET':
        url = "http://www.thrustcurve.org/servlets/search"
        request_data = '<search-request>' \
                       '    <availability>all</availability>' \
                       '    <max-results>2000</max-results>' \
                       '</search-request>'

        rawmotors = xmltodict.parse(requests.post(url, data=request_data).text)

        for motor in rawmotors['search-response']['results']['result']:
            if motor['motor-id'] == event['motor_id']:
                return motor

    raise UnableToPerformOperationError
Exemple #8
0
def handler(event, context):
    """Handler for the /rockets endpoint.

        ===
        GET
        ===

        Get a JSON object of all users.

        :param event['request']['token']: Requesting client's JWT token.
        :type event['request']['token']: string
        :returns rockets: A list of the rockets to be returned.
        :rtype: JSON

        :Example:

        curl -X GET -H 'Authorization: Bearer <JWT_TOKEN>' -H 'Cache-Control: no-cache' 'https://<server_addr>/rockets'


        ====
        POST
        ====

        Create a rocket model using HTTP POST.

        Parameters are given in the JSON object of the http POST call.

        :param event['rocket_data']: Data of the rocket model to create.
        :param event['request']['token']: Requesting client's JWT token.
        :type event['rocket_data']: string
        :type event['request']['token']: string

        :Example:

        curl -X POST -H 'Authorization: Bearer <JWT_TOKEN>'
            -H 'Content-Type: application/json'
            -H 'Accept: application/json'
            -d '{
              "rocket_data": <dict>
            }' 'http://<server_addr>/rockets'

        :raises UnableToPerformOperationError: If an invalid http_method is used to call the endpoint handler.

    """
    if 'request' not in event:
        raise UnableToPerformOperationError

    payload = login_check(event)

    http_method = event['request']['http_method']

    if http_method == 'GET':
        rockets = rocket_model.get_all(rockets_table, payload)

        return rockets

    elif http_method == 'POST':
        event.pop("request", None)
        rocket_id = rocket_model.create(event, rockets_table, payload)

        return {'rocket_id': rocket_id}

    raise UnableToPerformOperationError
Exemple #9
0
def handler(event, context):
    """Handler for the /flights/{flight_id} endpoint.

        ===
        GET
        ===

        Get a JSON object of a flight given a flight id.

        :param event['flight_id']: Flight id.
        :param event['request']['token']: Requesting client's JWT token.
        :type event['flight_id']: string
        :type event['request']['token']: string
        :returns flight: Flight object.
        :rtype: JSON

        :Example:

        curl -X GET -H 'Accept: application/json' -H 'Authorization: Bearer <JWT_TOKEN>' 'http://<server_addr>/flights/<flight_id>'

        ===
        PUT
        ===

        Update a flight given its flight id.

        :param event['flight_id']: Flight id.
        :param event['model_id']: ID of rocket model to attach to flight.
        :param event['flight_data']: Flight data about flight.
        :param event['request']['token']: Requesting client's JWT token.
        :type event['flight_id']: string
        :type event['model_id']: string, optional
        :type event['flight_data']: dict, optional
        :type event['request']['token']: string

        :Example:

        curl -X PUT -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer <JWT_TOKEN>' -d '{
          "flight_id": "<string>"
        }' 'http://<server_addr>/flights/<flight_id>'

        ======
        DELETE
        ======

        Delete a flight given a flight id.

        :param event['flight_id']: Flight id.
        :param event['request']['token']: Requesting client's JWT token.
        :type event['flight_id']: string
        :type event['request']['token']: string

        :Example:

        curl -X DELETE -H 'Accept: application/json' -H 'Authorization: Bearer <JWT_TOKEN>' 'http://<server_addr>/flights/<flight_id>'

        :raises UnableToPerformOperationError: If an invalid http_method is used to call the endpoint handler.

    """
    if 'request' not in event:
        raise UnableToPerformOperationError

    payload = login_check(event)

    http_method = event['request']['http_method']

    if http_method == 'GET':
        flight = flight_model.get_one(event['flight_id'], flights_table, payload)

        return flight

    elif http_method == 'PUT':
        flight_model.update(event['flight_id'], event, flights_table)

        return None

    elif http_method == 'DELETE':
        flight_model.delete(event['flight_id'], flights_table)

        return None

    raise UnableToPerformOperationError
Exemple #10
0
def handler(event, context):
    """Handler for the /flights/{flight_id} endpoint.

        ===
        GET
        ===

        Get a JSON object of a flight given a flight id.

        :param event['flight_id']: Flight id.
        :param event['request']['token']: Requesting client's JWT token.
        :type event['flight_id']: string
        :type event['request']['token']: string
        :returns flight: Flight object.
        :rtype: JSON

        :Example:

        curl -X GET -H 'Accept: application/json' -H 'Authorization: Bearer <JWT_TOKEN>' 'http://<server_addr>/flights/<flight_id>'

        ===
        PUT
        ===

        Update a flight given its flight id.

        :param event['flight_id']: Flight id.
        :param event['model_id']: ID of rocket model to attach to flight.
        :param event['flight_data']: Flight data about flight.
        :param event['request']['token']: Requesting client's JWT token.
        :type event['flight_id']: string
        :type event['model_id']: string, optional
        :type event['flight_data']: dict, optional
        :type event['request']['token']: string

        :Example:

        curl -X PUT -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer <JWT_TOKEN>' -d '{
          "flight_id": "<string>"
        }' 'http://<server_addr>/flights/<flight_id>'

        ======
        DELETE
        ======

        Delete a flight given a flight id.

        :param event['flight_id']: Flight id.
        :param event['request']['token']: Requesting client's JWT token.
        :type event['flight_id']: string
        :type event['request']['token']: string

        :Example:

        curl -X DELETE -H 'Accept: application/json' -H 'Authorization: Bearer <JWT_TOKEN>' 'http://<server_addr>/flights/<flight_id>'

        :raises UnableToPerformOperationError: If an invalid http_method is used to call the endpoint handler.

    """
    if 'request' not in event:
        raise UnableToPerformOperationError

    payload = login_check(event)

    http_method = event['request']['http_method']

    if http_method == 'GET':
        flight = flight_model.get_one(event['flight_id'], flights_table,
                                      payload)

        return flight

    elif http_method == 'PUT':
        flight_model.update(event['flight_id'], event, flights_table)

        return None

    elif http_method == 'DELETE':
        flight_model.delete(event['flight_id'], flights_table)

        return None

    raise UnableToPerformOperationError
Exemple #11
0
def handler(event, context):
    """Handler for the /flights endpoint.

        ===
        GET
        ===

        Get a JSON object of all flights.

        :param event['request']['token']: Requesting client's JWT token.
        :type event['request']['token']: string
        :returns flights: A list of the flights to be returned.
        :rtype: JSON

        :Example:

        curl -X GET -H 'Authorization: Bearer <JWT_TOKEN>' -H 'Cache-Control: no-cache' 'https://<server_addr>/flights'


        ====
        POST
        ====

        Create a flight using HTTP POST.

        Parameters are given in the JSON object of the http POST call.

        :param event['model_id']: ID of rocket model to attach to flight.
        :param event['motor_data']: Motor data about flight.
        :param event['flight_data']: Flight data about flight.
        :param event['request']['token']: Requesting client's JWT token.
        :type event['flight_id']: string
        :type event['model_id']: string, optional
        :type event['motor_data']: dict, optional
        :type event['flight_data']: dict, optional
        :type event['request']['token']: string

        :Example:

        curl -X POST -H 'Authorization: Bearer <JWT_TOKEN>'
            -H 'Content-Type: application/json'
            -H 'Accept: application/json'
            -d '{
              "name": "<string>"
            }' 'http://<server_addr>/flights'

        :raises UnableToPerformOperationError: If an invalid http_method is used to call the endpoint handler.

    """
    if 'request' not in event:
        raise UnableToPerformOperationError

    payload = login_check(event)

    http_method = event['request']['http_method']

    if http_method == 'GET':
        flights = flight_model.get_all(flights_table, payload)

        return flights

    elif http_method == 'POST':
        event.pop("request", None)
        flight_id = flight_model.create(event, flights_table, payload)

        return {'flight_id': flight_id}

    raise UnableToPerformOperationError
Exemple #12
0
def handler(event, context):
    """Handler for the /settings endpoint.

        ===
        GET
        ===

        Get a JSON object of the current user settings.

        :param event['request']['token']: Requesting client's JWT token.
        :type event['request']['token']: string
        :returns settings: A json object of the current user settings.
        :rtype: JSON

        :Example:

        curl -X GET -H 'Authorization: Bearer <JWT_TOKEN>' -H 'Cache-Control: no-cache' 'https://<server_addr>/settings'

        ===
        PUT
        ===

        Updates user settings using HTTP PUT.

        Parameters are given in the JSON object of the http PUT call.

        :param event['settings']: User settings to update.
        :param event['request']['token']: Requesting client's JWT token.
        :type event['settings']: string
        :type event['request']['token']: string

        :Example:

        curl -X POST -H 'Authorization: Bearer <JWT_TOKEN>'
            -H 'Content-Type: application/json'
            -H 'Accept: application/json'
            -d '{
              "settings": "<string>"
            }' 'http://<server_addr>/settings'

        :raises UnableToPerformOperationError: If an invalid http_method is used to call the endpoint handler.

    """
    if 'request' not in event:
        raise UnableToPerformOperationError

    payload = login_check(event)

    http_method = event['request']['http_method']

    if http_method == 'GET':
        settings = user_model.get_user_settings(users_table, payload)

        return settings

    elif http_method == 'PUT':
        user_model.set_user_settings(event['settings'], users_table, payload)

        return None

    raise UnableToPerformOperationError