def __init__(self, object, jsonp_padding=None): content = json.dumps(object, ensure_ascii=True) if jsonp_padding: content = '%(func)s(%(obj)s)' % \ {'func': jsonp_padding, 'obj': content} content_type = 'application/json-p' else: content_type = 'application/json' super(JsonResponse, self).__init__( content, content_type=content_type)
def test_set_get_subscriptions(self): """ Tests that an upload subscription is returned back correctly """ # upload a subscription response = self.client.post(self.url, json.dumps(self.action_data), content_type="application/json", **self.extra) self.assertEqual(response.status_code, 200, response.content) # verify that the subscription is returned correctly response = self.client.get(self.url, {'since': '0'}, **self.extra) self.assertEqual(response.status_code, 200, response.content) response_obj = json.loads(response.content) self.assertEqual(self.action_data['add'], response_obj['add']) self.assertEqual([], response_obj.get('remove', []))
def request(self, url, data=None): headers = {'Content-Type': 'application/json'} if url == self.OAUTH_TOKEN_URL: # Inject username and password into the request URL url = utils.url_add_authentication(url, settings.FLATTR_KEY, settings.FLATTR_SECRET) elif self.user.profile.settings.get_setting('flattr_token', ''): headers['Authorization'] = 'Bearer ' + self.user.profile.settings.get_wksetting(FLATTR_TOKEN) if data is not None: data = json.dumps(data) try: response = utils.urlopen(url, headers, data) except urllib2.HTTPError, error: return {'_gpodder_statuscode': error.getcode()}
def test_episode_actions(self): url = reverse(episodes, kwargs={ 'version': '2', 'username': self.user.username, }) # upload actions response = self.client.post(url, json.dumps(self.action_data), content_type="application/json", **self.extra) self.assertEqual(response.status_code, 200, response.content) response = self.client.get(url, {'since': '0'}, **self.extra) self.assertEqual(response.status_code, 200, response.content) response_obj = json.loads(response.content) actions = response_obj['actions'] self.assertTrue(self.compare_action_list(self.action_data, actions))
def test_episode_actions(self): url = reverse(episodes, kwargs=dict(version='2', username=self.user.username)) # upload actions response = self.client.post(url, json.dumps(self.action_data), content_type="application/json", **self.extra) self.assertEqual(response.status_code, 200, response.content) # get all extra = deepcopy(self.extra) extra['since'] = 0 response = self.client.get(url, **extra) self.assertEqual(response.status_code, 200, response.content) response_obj = json.loads(response.content) actions = response_obj['actions'] self.assertTrue(self.compare_action_list(self.action_data, actions))
def dump(self, docs, db): output = sys.stdout boundary = None envelope = write_multipart(output, boundary=boundary) total = len(docs) for n, docid in enumerate(docs): if not docid: continue doc = db.get(docid, attachments=True) attachments = doc.pop('_attachments', {}) jsondoc = json.dumps(doc) if attachments: parts = envelope.open({ 'Content-ID': doc['_id'], 'ETag': '"%s"' % doc['_rev'] }) parts.add('application/json', jsondoc) for name, info in attachments.items(): content_type = info.get('content_type') if content_type is None: # CouchDB < 0.8 content_type = info.get('content-type') parts.add(content_type, b64decode(info['data']), { 'Content-ID': name }) parts.close() else: envelope.add('application/json', jsondoc, { 'Content-ID': doc['_id'], 'ETag': '"%s"' % doc['_rev'] }) progress(n+1, total, docid, stream=sys.stderr) envelope.close()