| 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
| 2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. |
| 3 | # |
| 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU Affero General Public License as published by |
| 6 | # the Free Software Foundation, either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU Affero General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU Affero General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | import logging |
| 18 | import json |
| 19 | |
| 20 | from urllib2 import urlopen, Request |
| 21 | from urllib import urlencode |
| 22 | |
| 23 | _log = logging.getLogger(__name__) |
| 24 | |
| 25 | |
| 26 | def create_post_request(url, data, **kw): |
| 27 | ''' |
| 28 | Issue a HTTP POST request. |
| 29 | |
| 30 | Args: |
| 31 | url: The URL to which the POST request should be issued |
| 32 | data: The data to be send in the body of the request |
| 33 | **kw: |
| 34 | data_parser: The parser function that is used to parse the `data` |
| 35 | argument |
| 36 | ''' |
| 37 | data_parser = kw.get('data_parser', urlencode) |
| 38 | headers = kw.get('headers', {}) |
| 39 | |
| 40 | return Request(url, data_parser(data), headers=headers) |
| 41 | |
| 42 | |
| 43 | def json_processing_callback(entry): |
| 44 | ''' |
| 45 | Send an HTTP post to the registered callback url, if any. |
| 46 | ''' |
| 47 | if not entry.processing_metadata: |
| 48 | _log.debug('No processing callback for {0}'.format(entry)) |
| 49 | return |
| 50 | |
| 51 | url = entry.processing_metadata[0].callback_url |
| 52 | |
| 53 | _log.debug('Sending processing callback for {0} ({1})'.format( |
| 54 | entry, |
| 55 | url)) |
| 56 | |
| 57 | headers = { |
| 58 | 'Content-Type': 'application/json'} |
| 59 | |
| 60 | data = { |
| 61 | 'id': entry.id, |
| 62 | 'state': entry.state} |
| 63 | |
| 64 | request = create_post_request( |
| 65 | url, |
| 66 | data, |
| 67 | headers=headers, |
| 68 | data_parser=json.dumps) |
| 69 | |
| 70 | urlopen(request) |
| 71 | _log.debug('Processing callback for {0} sent'.format(entry)) |
| 72 | |
| 73 | return True |