Пример #1
0
def set_criteria_by_ticketkey_host():
    
    #
    # [Validation]
    #    1. must be content-type is applicaiton/json
    #    2. JSON must be parsed successfully
    #    3. JSON must has TicketKey and Host
    #    4. save to DB
    #    5. purge cache
    #    6. enqueue
    #
    
    # 1.
    if request.headers['Content-Type'] != 'application/json':
        current_app.logger.warn("received Content-Type %s" % request.headers['Content-Type'])
        return response.bad_request("please send application/json")

    # 2.
    # http://flask.pocoo.org/docs/0.10/api/#flask.Request.get_json
    try:
        data = request.get_json()
    except Exception as ex:
        current_app.logger.warn("couldn't parse JSON %s" % ex)
        return response.bad_request("invalid JSON format")

    # 3.
    required_key = ['TicketKey', 'Host']
    for key in required_key:
        if key not in data:
            return response.bad_request("{key} is not found".format(key=key))

    # 4.
    table = DataTable(region_name=current_app.config['DYNAMODB_REGION'],
                    table_name=current_app.config['CRITERIA_TABLE'],
                    logger=current_app.logger)
    result = table.save(data)

    # 5.
    current_app.cache.delete('criteria.all')
    current_app.cache.delete("criteria.ticket_key.%s" % data['TicketKey'])
    current_app.cache.delete("criteria.ticket_key.%s.host.%s" % (data['TicketKey'], data['Host']))
 

    # 6.
    queue = Queue(region_name=current_app.config['SQS_REGIOM'],
                  queue_name=current_app.config['SQS_NAME'],
                  logger=current_app.logger)
    queue.push({key: data[key] for key in required_key})

    return response.created()
Пример #2
0
def set_criteria_by_ticketkey_host(ticket_key, host):
    
    #
    # [Validation]
    #    1. must be content-type is applicaiton/json
    #    2. JSON must be parsed successfully
    #    3. validate JSON string
    #    4. save to DB
    #    5. purge cache
    #    6. enqueue
    #
    
    # 1.
    if request.headers['Content-Type'] != 'application/json':
        current_app.logger.warn("received Content-Type %s" % request.headers['Content-Type'])
        return response.bad_request("please send application/json")

    # 2.
    # http://flask.pocoo.org/docs/0.10/api/#flask.Request.get_json
    try:
        data = request.get_json()
    except Exception as ex:
        current_app.logger.warn("couldn't parse JSON %s" % ex)
        return response.bad_request("invalid JSON format")

    # 3.
    try:
        criteria = CriteriaData(**data).get_json()
    except Exception as ex:
        current_app.logger.error(ex)
        return response.bad_request(ex.message)

    # 4.
    table = DataTable(region_name=current_app.config['DYNAMODB_REGION'],
                      table_name=current_app.config['CRITERIA_TABLE'],
                      logger=current_app.logger)
    result = table.save(criteria, TicketKey=ticket_key, Host=host)

    # 5.
    cache.delete_memoized(get_all_criteria)
    cache.delete_memoized(get_criteria_by_ticketkey, ticket_key)
    cache.delete_memoized(get_criteria_by_ticketkey_host, ticket_key, host)

    # 6.
    queue = Queue(region_name=current_app.config['SQS_REGION'],
                  queue_name=current_app.config['SQS_NAME'],
                  logger=current_app.logger)
    queue.push({'TicketKey': ticket_key, 'Host': host})

    return response.created()