<s> from __future__ import absolute_import <EOL> from zerver . models import get_client <EOL> from zerver . lib . actions import check_send_message <EOL> from zerver . lib . response import json_success , json_error <EOL> from zerver . decorator import REQ , has_request_variables , api_key_only_webhook_view <EOL> import ujson <EOL> PINGDOM_SUBJECT_TEMPLATE = '<STR_LIT>' <EOL> PINGDOM_MESSAGE_TEMPLATE = '<STR_LIT>' <EOL> PINGDOM_MESSAGE_DESCRIPTION_TEMPLATE = '<STR_LIT>' <EOL> SUPPORTED_CHECK_TYPES = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> @ api_key_only_webhook_view <EOL> @ has_request_variables <EOL> def api_pingdom_webhook ( request , user_profile , stream = REQ ( default = '<STR_LIT>' ) ) : <EOL> payload = ujson . loads ( request . body ) <EOL> check_type = get_check_type ( payload ) <EOL> if check_type in SUPPORTED_CHECK_TYPES : <EOL> subject = get_subject_for_http_request ( payload ) <EOL> body = get_body_for_http_request ( payload ) <EOL> else : <EOL> return json_error ( '<STR_LIT>' . format ( check_type = check_type ) ) <EOL> check_send_message ( user_profile , get_client ( '<STR_LIT>' ) , '<STR_LIT>' , [ stream ] , subject , body ) <EOL> return json_success ( ) <EOL> def get_subject_for_http_request ( payload ) : <EOL> return PINGDOM_SUBJECT_TEMPLATE . format ( name = payload [ '<STR_LIT>' ] ) <EOL> def get_body_for_http_request ( payload ) : <EOL> current_state = payload [ '<STR_LIT>' ] <EOL> previous_state = payload [ '<STR_LIT>' ] <EOL> data = { <EOL> '<STR_LIT>' : payload [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : previous_state , <EOL> '<STR_LIT>' : current_state , <EOL> '<STR_LIT:type>' : get_check_type ( payload ) <EOL> } <EOL> body = PINGDOM_MESSAGE_TEMPLATE . format ( ** data ) <EOL> if current_state == '<STR_LIT>' and previous_state == '<STR_LIT>' : <EOL> description = PINGDOM_MESSAGE_DESCRIPTION_TEMPLATE . format ( description = payload [ '<STR_LIT>' ] ) <EOL> body += '<STR_LIT>' . format ( description = description ) <EOL> return body <EOL> def get_check_type ( payload ) : <EOL> return payload [ '<STR_LIT>' ] </s>
