"""Webhooks API for Mailgun based email"""

import json
import datetime
import pytz

from pyramid.view import (
    view_config,
)
from pyramid.response import (
    Response,
)
from pyramid_mailer import get_mailer
from ..model import (
    StatusUpdate,
)
from ..email_processing import (
    send_confirm,
)

import logging
log = logging.getLogger(__name__)


def includeme(config):
    config.add_route('mailgun_receiving', '/receive')
    config.scan()


@view_config(route_name='mailgun_receiving', request_method='HEAD')
def webhook_allowed(request):
    return Response(status=200)


@view_config(route_name='mailgun_receiving',
             request_method='POST',
             renderer='json')
def receive_email(request):
    mailgun_events = request.params.dict_of_lists()
    email_headers = {}
    email_header_thing = json.loads(
        request.params.getall('message-headers')[0]
    )
    for i in email_header_thing:
        email_headers[i[0]] = i[1]

    mailgun_events["parsed_message_id"] = email_headers.get(
        'Message-Id'
    ).strip('>').strip('<')

    mailer = get_mailer(request)
    updates = process_inbound_email(mailer, mailgun_events)

    return list(updates)


def process_inbound_email(mailer, email_json):
    timestamp = datetime.datetime.fromtimestamp(
        float(email_json['timestamp'][0]), tz=pytz.UTC
    )
    author = email_json['sender'][0]
    text = email_json['body-plain'][0]
    html = email_json['body-html'][0]
    subject = email_json['subject'][0]
    message_id = email_json["parsed_message_id"]
    update = StatusUpdate.from_email(author, timestamp, text, html)
    send_confirm(mailer,
                 update.user,
                 reply_to_id=message_id,
                 reply_to_subject=subject,
                 )
    yield update

@@ -744,16 +744,421 @@
 events =
+ parse_mailgun_event(request)%0A    mailer = get_mailer(request)%0A    updates = process_inbound_email(mailer, mailgun_events)%0A%0A    return list(updates)%0A%0A%0Adef parse_mailgun_event(request):%0A    %22%22%22%0A        Mailgun returns a WebOb multiNestedDict pile of goo.%0A        Take each key/value and crate a corresponding key/value%0A        Get Message-Id out of headers for use later.%0A    %22%22%22%0A    parsed_mailgun_event =
  request
@@ -1208,34 +1208,30 @@
  %7B%7D%0A    
-e
 mail
+gun
 _header
-_thing
  = json.
@@ -1291,24 +1291,62 @@
 ')%5B0%5D%0A    )%0A
+    # iterate to parse out message_id%0A
     for i in
@@ -1350,26 +1350,22 @@
  in 
-e
 mail
+gun
 _header
-_thing
 :%0A  
@@ -1397,21 +1397,27 @@
  = i%5B1%5D%0A
-%0A
     
+parsed_
 mailgun_
@@ -1421,17 +1421,16 @@
 un_event
-s
 %5B%22parsed
@@ -1522,122 +1522,35 @@
     
-mailer = get_mailer(request)%0A    updates = process_inbound_email(mailer, mailgun_events)%0A%0A    return list(updates)
+return parsed_mailgun_event
 %0A%0A%0Ad

Break out mailgun event parsing into function.