def process_requests():
    global account
    global last_processed_request_id

    # process request data
    ubr_list = UBRList()
    ubr_list.deserialize(request.get_json())

    app.logger.debug('Processing received bank requests.')

    # sort request data
    sorted_bank_requests = ubr_list.get_sorted()

    # perform bank requests in order
    for req in sorted_bank_requests:
        if last_processed_request_id < req.id:
            if req.operation == 'CREDIT':
                account.increase_balance(req.amount)
            else:
                account.decrease_balance(req.amount)
            last_processed_request_id = req.id

    # response
    response = Response(code=status.HTTP_200_OK,
                        description='Bankovní operace úspěšně zpracovány.')
    return make_response(response.serialize(), status.HTTP_200_OK)
def shuffle_requests():
    global endpoints

    # process request data
    ubr_list = UBRList()
    ubr_list.deserialize(request.get_json())
    valid_json_data = ubr_list.serialize()

    # init results
    success = []
    fail = []

    app.logger.info('Sending data to all endpoints.')
    # shuffle endpoints
    shuffle(endpoints)
    for endpoint in endpoints:
        # shuffle data
        shuffle(valid_json_data)
        # sent data
        url = "http://{0}:{1}{2}".format(endpoint.host, endpoint.port, endpoint.route)
        try:
            status_code = requests.post(url, json=valid_json_data).status_code
        except requests.exceptions.RequestException:
            status_code = status.HTTP_408_REQUEST_TIMEOUT
        # handle response
        if status_code is status.HTTP_200_OK:
            success.append({'endpoint': url, 'code': status_code})
        else:
            fail.append({'endpoint': url, 'code': status_code})
    app.logger.debug('\n' + json.dumps({'successful': success, 'failed': fail}, indent=4, sort_keys=True))

    # response
    if len(fail) == 0:
        response = Response(
            code=status.HTTP_200_OK,
            description='Bankovní operace úspěšně zpracovány'
        )
        return make_response(response.serialize(), status.HTTP_200_OK)
    else:
        response = Response(
            code=status.HTTP_503_SERVICE_UNAVAILABLE,
            description='Nepodařilo se zpracovat bankovní oprace'
        )
        return make_response(response.serialize(), status.HTTP_503_SERVICE_UNAVAILABLE)
Exemple #3
0
 async def distribute_game(self, to_waiting=False):
     response = Response(action=Actions.UPDATE_GAME.value,
                         body=self.game.to_dict())
     serialized_response = response.serialize()
     if to_waiting:
         tasks = [
             asyncio.ensure_future(ws.send(serialized_response))
             for ws in self._waiting
         ]
     else:
         tasks = [
             asyncio.ensure_future(player.ws.send(serialized_response))
             for player in self._players
         ]
     await asyncio.gather(*tasks)
def handle_generic_error(e):
    response = Response(code=e.code, description=e.description)
    return make_response(response.serialize(), e.code)