def jdumps(obj, encoding='utf-8'): # Do 'serialize' test at some point for other classes global cjson if cjson: return cjson.encode(obj) else: return json.dumps(obj, encoding=encoding)
def json(f, *args, **kwargs): """JSON Flask Response Decorator.""" data = f(*args, **kwargs) # we already have a formatted response, move along if isinstance(data, Response): return data dump = omnijson.dumps(data) r = app.make_response(dump) r.headers['Content-Type'] = 'application/json' return r
def record_trigger(self, client, post_mock, response_status=201, check_post_call=True): action = 'viewed' obj = 'blog post' actor_id = uuid.uuid4() identities = { 'email': ['*****@*****.**', '*****@*****.**'], 'user_id': 42, } attributes = { 'tags': ['high-risk', 'big-spender'], 'plan': 'basic', } variables = { 'tags': ['sales', 'promo'], } response_mock = Mock() response_mock.status_code = response_status post_mock.return_value = response_mock client.record_trigger( actor_id=actor_id, action=action, obj=obj, identities=identities, attributes=attributes, variables=variables, ) if check_post_call: expected_post_data = json.dumps({ 'action': action, 'object': obj, 'actor_id': str(actor_id), 'captured_identities': identities, 'captured_attributes': attributes, 'variables': variables, }) expected_headers = { 'Content-Type': 'application/json', 'X-FL-API-KEY': str(client.api_key), } post_mock.assertCalledWith( 'https://api.focuslab.io/api/v1/triggers/', data=expected_post_data, headers=expected_headers, )
def record_trigger(self, actor_id, action, obj, identities=None, attributes=None, variables=None): url = self.get_url("trigger") headers = self.get_headers() data = self.get_trigger_data( actor_id=actor_id, action=action, obj=obj, identities=identities, attributes=attributes, variables=variables ) response = requests.post(url, data=json.dumps(data), headers=headers) status = response.status_code if status != 201: if status in (401, 403): raise BadAPIKey elif status == 400: raise BadRequest elif status == 404: raise ResourceNotFound elif status == 500: raise ServerError else: raise UnknownError
#!/usr/bin/env python # -*- coding: utf-8 -*- import omnijson as json a = json.loads('{"yo":"dawg"}') print a print json.dumps(a) json.loads('{"yo": dawg"}')
def test_dump_json(self): a = json.dumps(self._good_json_result) self.assertEqual(a, self._good_json_string)
def __call__(self): """Return object's attribute dictionary in JSON form.""" return json.dumps(self.__dict__)
def test_lookup_server_nickname(self): self.response.content = json.dumps([{'href': '/naruto'}]) href = righteous.api._lookup_server(None, 'naruto') assert_equal(href, '/naruto')
"""Specialised serialisation for certain types""" if isinstance(v, UUID): return v.urn[9:] elif isinstance(v, dict): ret={} for item in v: ret[item]=FixupTypes(v[item]) return ret elif isinstance(v, list): for i in xrange(0, len(v)): v[i]=FixupTypes(v[i]) return v return v render_xml = lambda **args: XMLise(FixupTypes(args)) render_json = lambda **args: json.dumps(FixupTypes(args)) render_html = lambda **args: u'<html><body><pre>%s</pre></body></html>'%escape(XMLise(FixupTypes(args))) render_txt = lambda **args: repr(FixupTypes(args)) urls = ( '/', 'index', '/apilist', 'apilist', '/open(.*)', 'open', '/cancel/(.+)', 'cancel', '/(.+)', 'catchall', ) app = web.application(urls, locals()) session = web.session.Session(app, web.session.DiskStore('bexmlsrv_sessions'), initializer={'uri': None}) sessionToParserInfo = {} class ParserInfo: """Keeps session to parser info"""