<s> from simplejson import loads <EOL> from turbulenz_local . lib . exceptions import ApiException , BadRequest <EOL> from turbulenz_local . lib . servicestatus import ServiceStatus <EOL> from turbulenz_local . decorators import secure_post <EOL> from turbulenz_local . controllers import BaseController <EOL> def _validate_event ( event_key , event_value ) : <EOL> try : <EOL> event_key = str ( event_key ) <EOL> except ValueError : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not event_key : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if isinstance ( event_value , ( str , unicode ) ) : <EOL> try : <EOL> event_value = loads ( event_value ) <EOL> except ValueError : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not isinstance ( event_value , list ) : <EOL> try : <EOL> event_value = float ( event_value ) <EOL> except ( TypeError , ValueError ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> else : <EOL> try : <EOL> for index , value in enumerate ( event_value ) : <EOL> event_value [ index ] = float ( value ) <EOL> except ( TypeError , ValueError ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return event_key , event_value <EOL> class CustommetricsController ( BaseController ) : <EOL> custommetrics_service = ServiceStatus . check_status_decorator ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> @ custommetrics_service <EOL> @ secure_post <EOL> def add_event ( cls , slug , params = None ) : <EOL> try : <EOL> session = cls . game_session_list . get_session ( params [ '<STR_LIT>' ] ) <EOL> except ( KeyError , TypeError ) : <EOL> raise BadRequest ( '<STR_LIT>' ) <EOL> game = session . game <EOL> if game is None : <EOL> raise ApiException ( '<STR_LIT>' ) <EOL> if slug != game . slug : <EOL> raise BadRequest ( '<STR_LIT>' ) <EOL> try : <EOL> event_key = params [ '<STR_LIT:key>' ] <EOL> except ( TypeError , KeyError ) : <EOL> raise BadRequest ( '<STR_LIT>' ) <EOL> try : <EOL> event_value = params [ '<STR_LIT:value>' ] <EOL> except ( TypeError , KeyError ) : <EOL> raise BadRequest ( '<STR_LIT>' ) <EOL> del params [ '<STR_LIT:value>' ] <EOL> try : <EOL> event_key , event_value = _validate_event ( event_key , event_value ) <EOL> except ValueError as e : <EOL> raise BadRequest ( e . message ) <EOL> return { '<STR_LIT>' : True , '<STR_LIT:data>' : { '<STR_LIT>' : '<STR_LIT>' + str ( event_value ) + '<STR_LIT>' + event_key + '<STR_LIT>' '<STR_LIT>' } } <EOL> @ classmethod <EOL> @ custommetrics_service <EOL> @ secure_post <EOL> def add_event_batch ( cls , slug , params = None ) : <EOL> try : <EOL> session = cls . game_session_list . get_session ( params [ '<STR_LIT>' ] ) <EOL> except ( KeyError , TypeError ) : <EOL> raise BadRequest ( '<STR_LIT>' ) <EOL> game = session . game <EOL> if game is None : <EOL> raise ApiException ( '<STR_LIT>' ) <EOL> if slug != game . slug : <EOL> raise BadRequest ( '<STR_LIT>' ) <EOL> try : <EOL> event_batch = params [ '<STR_LIT>' ] <EOL> except ( TypeError , KeyError ) : <EOL> raise BadRequest ( '<STR_LIT>' ) <EOL> del params [ '<STR_LIT>' ] <EOL> if not isinstance ( event_batch , list ) : <EOL> raise BadRequest ( '<STR_LIT>' ) <EOL> for event in event_batch : <EOL> try : <EOL> event_key = event [ '<STR_LIT:key>' ] <EOL> except ( TypeError , KeyError ) : <EOL> raise BadRequest ( '<STR_LIT>' ) <EOL> try : <EOL> event_value = event [ '<STR_LIT:value>' ] <EOL> except ( TypeError , KeyError ) : <EOL> raise BadRequest ( '<STR_LIT>' ) <EOL> try : <EOL> event_key , event_value = _validate_event ( event_key , event_value ) <EOL> except ValueError as e : <EOL> raise BadRequest ( e . message ) <EOL> try : <EOL> event_time = float ( event [ '<STR_LIT>' ] ) <EOL> except ( TypeError , KeyError ) : <EOL> raise BadRequest ( '<STR_LIT>' ) <EOL> except ValueError : <EOL> raise BadRequest ( '<STR_LIT>' ) <EOL> if event_time > <NUM_LIT:0> : <EOL> raise BadRequest ( '<STR_LIT>' ) <EOL> return { '<STR_LIT>' : True , '<STR_LIT:data>' : { '<STR_LIT>' : '<STR_LIT>' '<STR_LIT>' % <EOL> len ( event_batch ) } } </s>
