<commit_before>
import sys
import json
import urllib2


from alerta.common import log as logging
from alerta.common import config
from alerta.common.utils import DateEncoder

Version = '2.0.1'

LOG = logging.getLogger(__name__)
CONF = config.CONF

_API_TIMEOUT = 2  # seconds


class ApiClient(object):

    def send(self, msg, host=None, port=None, endpoint=None):

        self.api_host = host or CONF.api_host
        self.api_port = port or CONF.api_port
        self.api_endpoint = endpoint or CONF.api_endpoint

        LOG.debug('header = %s', msg.get_header())
        LOG.debug('message = %s', msg.get_body())

        if msg.get_type().endswith('Alert'):
            api_url = 'http://%s:%s%s/alerts/alert.json' % (self.api_host, self.api_port, self.api_endpoint)
        elif msg.get_type() == 'Heartbeat':
            api_url = 'http://%s:%s%s/heartbeats/heartbeat.json' % (self.api_host, self.api_port, self.api_endpoint)
        else:
            LOG.error('Message type %s not supported by this API endpoint.', msg.get_type())
            return

        post = json.dumps(msg.get_body(), cls=DateEncoder)
        headers = {'Content-Type': 'application/json'}

        request = urllib2.Request(api_url, headers=headers)
        request.add_data(post)
        LOG.debug('url=%s, data=%s, headers=%s', request.get_full_url(), request.data, request.headers)

        LOG.debug('Sending alert to API endpoint...')
        try:
            response = urllib2.urlopen(request, post, _API_TIMEOUT)
        except ValueError, e:
            LOG.error('Could not send alert to API endpoint %s : %s', api_url, e)
            print >>sys.stderr, 'Could not send alert to API endpoint %s : status=%s' % (api_url, e)
            sys.exit(1)
        except urllib2.URLError, e:
            if hasattr(e, 'reason'):
                error = str(e.reason)
            elif hasattr(e, 'code'):
                error = e.code
            else:<commit_msg>use standard defaults and output dry run as curl command<commit_after>
import sys
import json
import urllib2


from alerta.common import log as logging
from alerta.common import config
from alerta.common.utils import DateEncoder

Version = '2.0.1'

LOG = logging.getLogger(__name__)
CONF = config.CONF

_API_TIMEOUT = 2  # seconds


class ApiClient(object):

    def send(self, msg, host=None, port=None, endpoint=None, dry_run=False):

        self.api_host = host or CONF.api_host
        self.api_port = port or CONF.api_port
        self.api_endpoint = endpoint or CONF.api_endpoint

        LOG.debug('header = %s', msg.get_header())
        LOG.debug('message = %s', msg.get_body())

        if msg.get_type().endswith('Alert'):
            api_url = 'http://%s:%s%s/alerts/alert.json' % (self.api_host, self.api_port, self.api_endpoint)
        elif msg.get_type() == 'Heartbeat':
            api_url = 'http://%s:%s%s/heartbeats/heartbeat.json' % (self.api_host, self.api_port, self.api_endpoint)
        else:
            LOG.error('Message type %s not supported by this API endpoint.', msg.get_type())
            return

        post = json.dumps(msg.get_body(), cls=DateEncoder)
        headers = {'Content-Type': 'application/json'}

        request = urllib2.Request(api_url, headers=headers)
        request.add_data(post)
        LOG.debug('url=%s, data=%s, headers=%s', request.get_full_url(), request.data, request.headers)

        if dry_run:
            print "curl '%s' -H 'Content-Type: application/json' -d '%s'" % (api_url, post)
            return

        LOG.debug('Sending alert to API endpoint...')
        try:
            response = urllib2.urlopen(request, post, _API_TIMEOUT)
        except ValueError, e:
            LOG.error('Could not send alert to API endpoint %s : %s', api_url, e)
            print >>sys.stderr, 'Could not send alert to API endpoint %s : status=%s' % (api_url, e)
            sys.exit(1)
        except urllib2.URLError, e:
            if hasattr(e, 'reason'):
                error = str(e.reason)
            elif hasattr(e, 'code'):
                error = e.code
            else:
{'fraction_non_alphanumeric': 0.12744140625, 'fraction_numerical': 0.00439453125, 'mean_word_length': 2.5017094017094017, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5069526', 'n_tokens_mistral': 1310, 'n_tokens_neox': 1287, 'n_words': 324}