# Path: media_nommer/utils/http.py
# class StringProducer(object):
#     """
#     This is useful for twisted.web.client.Agent, which must use a producer
#     to spoon feed the request over a period of time, instead of one big chunk.
# 
#     Check the Twisted documentation for an example of how this fits together::
# 
#         http://twistedmatrix.com/documents/current/web/howto/client.html
#     """
#     implements(IBodyProducer)
# 
#     def __init__(self, body):
#         """
#         :param str body: The body to be sent to the remote server.
#         """
#         self.body = body
#         self.length = len(body)
# 
#     def startProducing(self, consumer):
#         consumer.write(self.body)
#         return succeed(None)
# 
#     def pauseProducing(self):
#         pass
# 
#     def stopProducing(self):
#         pass
# 
# Path: media_nommer/utils/logger.py
# def debug(message):
# def info(message):
# def warning(message):
# def error(message_or_obj=None):


import urllib
from twisted.internet import reactor
from twisted.web.client import Agent
from twisted.web.http_headers import Headers
from media_nommer.utils.http import StringProducer
from media_nommer.utils import logger
    )

def send_notification(job):
    """
    Given an EncodingJob, see if it has a ``notify_url`` set, and dispatch
    a notification to said URL (if set). Don't do any processing of the response.

    :param EncodingJob job: The job whose state has changed.
    """
    if not job.notify_url:
        # No URL to notify, hang it up here.
        return

    job_state_details = job.job_state_details
    if not job_state_details:
        # Make sure we're not passing a 'None' string in like a silly boy.
        job_state_details = ''

    # This will be JSON-serialized and POSTed to the notify_url.
    data = {
        'unique_id': job.unique_id,
        'job_state': job.job_state,
        'job_state_details': job_state_details,
    }

    agent = Agent(reactor)
    headers = Headers({
        'User-Agent': ['media-nommer feederd'],
        'Content-Type': ['application/x-www-form-urlencoded'],
    })

# Path: media_nommer/utils/http.py
# class StringProducer(object):
#     """
#     This is useful for twisted.web.client.Agent, which must use a producer
#     to spoon feed the request over a period of time, instead of one big chunk.
# 
#     Check the Twisted documentation for an example of how this fits together::
# 
#         http://twistedmatrix.com/documents/current/web/howto/client.html
#     """
#     implements(IBodyProducer)
# 
#     def __init__(self, body):
#         """
#         :param str body: The body to be sent to the remote server.
#         """
#         self.body = body
#         self.length = len(body)
# 
#     def startProducing(self, consumer):
#         consumer.write(self.body)
#         return succeed(None)
# 
#     def pauseProducing(self):
#         pass
# 
#     def stopProducing(self):
#         pass
# 
# Path: media_nommer/utils/logger.py
# def debug(message):
# def info(message):
# def warning(message):
# def error(message_or_obj=None):


# Path: media_nommer/feederd/job_state_notifier.py
import urllib
from twisted.internet import reactor
from twisted.web.client import Agent
from twisted.web.http_headers import Headers
from media_nommer.utils.http import StringProducer
from media_nommer.utils import logger

    )

def send_notification(job):
    """
    Given an EncodingJob, see if it has a ``notify_url`` set, and dispatch
    a notification to said URL (if set). Don't do any processing of the response.

    :param EncodingJob job: The job whose state has changed.
    """
    if not job.notify_url:
        # No URL to notify, hang it up here.
        return

    job_state_details = job.job_state_details
    if not job_state_details:
        # Make sure we're not passing a 'None' string in like a silly boy.
        job_state_details = ''

    # This will be JSON-serialized and POSTed to the notify_url.
    data = {
        'unique_id': job.unique_id,
        'job_state': job.job_state,
        'job_state_details': job_state_details,
    }

    agent = Agent(reactor)
    headers = Headers({
        'User-Agent': ['media-nommer feederd'],
        'Content-Type': ['application/x-www-form-urlencoded'],
    })

    body = StringProducer(urllib.urlencode(data)) if data else None