<commit_before># Copyright 2016 Team DriverCity. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START app]
import logging

from flasgger import Swagger, swag_from, validate, ValidationError
from flask import Flask,request,jsonify

import parking_area
import payment
from cloud_storage_io import CloudStorageIO
from parking_event_repo import ParkingEventRepository
from occupancy_rates_repo import OccupancyRatesRepository

app = Flask(__name__)
Swagger(app)


@app.route('/api/v1.0/parkingEvent/', methods=['POST'])
@swag_from('swagger_specs/parkingEvent.yml')
def store_parking_event():
    # TODO: logging
    try:
        # validate schema
        validate(request.json, 'ParkingEvent', 'swagger_specs/parkingEvent.yml', root=__file__)

        # validate payment if paid context
        if request.json['parkingContextType'] == 'PAID':
            payment.validate(request.json['paymentMethodInformation']['paymentMethodType'], request.json['paymentMethodInformation'])

        # store valid information
        return ParkingEventRepository().store_parking_event(request.json), 201

    except ValidationError as e:
        return jsonify({ 'errorType': 'SCHEMA_VALIDATION_ERROR', 'content': e }), 400
    except payment.PaymentException as e:
        return jsonify({ 'errorType': 'PAYMENT_ERROR', 'content': e}), 400

@app.route('/tasks/occupancy', methods=['GET'])
def update_occupancy_rates():
    """
    An API for updating the occupancy rates. Used by a cron schedule.
    ---
    tags:
      - Occupancy rates analysis task
    """
    # TODO: logging
    #try:
    counts = ParkingEventRepository().get_occuring_paid_event_counts()
    OccupancyRatesRepository().refresh_occupancies(counts)
    return '', 201
    #except Exception as e:
        # TODO: make fault responding better
    #    return jsonify({'errorType': 'EXCEPTION', 'content': str(e)}), 500


@app.route('/tasks/store', methods=['GET'])
def move_to_long_term_data_store():
    """
    An API for storing the parking events to the long term data store. Used by a cron schedule.
    ---
    tags:
      - Blob storage task
    """
    # TODO: logging
    try:<commit_msg>Fix main.py exception handling<commit_after># Copyright 2016 Team DriverCity. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START app]
import logging

from flasgger import Swagger, swag_from, validate, ValidationError
from flask import Flask,request,jsonify

import parking_area
import payment
from cloud_storage_io import CloudStorageIO
from parking_event_repo import ParkingEventRepository
from occupancy_rates_repo import OccupancyRatesRepository

app = Flask(__name__)
Swagger(app)


@app.route('/api/v1.0/parkingEvent/', methods=['POST'])
@swag_from('swagger_specs/parkingEvent.yml')
def store_parking_event():
    # TODO: logging
    try:
        # validate schema
        validate(request.json, 'ParkingEvent', 'swagger_specs/parkingEvent.yml', root=__file__)

        # validate payment if paid context
        if request.json['parkingContextType'] == 'PAID':
            payment.validate(request.json['paymentMethodInformation']['paymentMethodType'], request.json['paymentMethodInformation'])

        # store valid information
        return ParkingEventRepository().store_parking_event(request.json), 201

    except ValidationError as e:
        return jsonify({ 'errorType': 'SCHEMA_VALIDATION_ERROR', 'content': e }), 400
    except payment.PaymentException as e:
        return jsonify({ 'errorType': 'PAYMENT_ERROR', 'content': e}), 400

@app.route('/tasks/occupancy', methods=['GET'])
def update_occupancy_rates():
    """
    An API for updating the occupancy rates. Used by a cron schedule.
    ---
    tags:
      - Occupancy rates analysis task
    """
    # TODO: logging
    try:
        counts = ParkingEventRepository().get_occuring_paid_event_counts()
        OccupancyRatesRepository().refresh_occupancies(counts)
        return '', 201
    except Exception as e:
        # TODO: make fault responding better
        return jsonify({'errorType': 'EXCEPTION', 'content': str(e)}), 500


@app.route('/tasks/store', methods=['GET'])
def move_to_long_term_data_store():
    """
    An API for storing the parking events to the long term data store. Used by a cron schedule.
    ---
    tags:
      - Blob storage task
    """
    # TODO: logging
    try:
{'fraction_non_alphanumeric': 0.09966530308664931, 'fraction_numerical': 0.009297136481963555, 'mean_word_length': 3.9303391384051327, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3663050', 'n_tokens_mistral': 1554, 'n_tokens_neox': 1471, 'n_words': 492}