def encode(self, data): """ Serializes ``data`` into a raw string. """ # not all data keys are json serializable data_copy = {} for key in data: try: json.dumps(data[key]) # check if serializable data_copy[key] = data[key] except TypeError: pass return base64.b64encode(zlib.compress(json.dumps(data_copy).encode('utf8')))
def refresh_event(owner, zone): remainder_count = 500 alarm_list = RiskVulneraModel.get_all_risk_vulnera(zone) alarm_list = filter(lambda x: check_last_time(x.gen_time), alarm_list) for alarm in alarm_list: alarm.is_deleted = True alarm.save() while remainder_count != 0: payload = { 'action': 'SafedogQueryAlarm', 'owner': owner, 'record_count': 500, 'zone': zone, 'region': 'bj' } resp = api.get(payload) logger.info("SafedogQueryAlarm->"+json.dumps(resp)) data = resp['data']['ret_set'] if data: data = data[0] else: break remainder_count = data['count'] if len(data['list']) == 0: break alarm_list = data['list'] for alarm in alarm_list: process_alarm_info(owner, zone, alarm) return []
def send(self, **data): """ Sends the message to the server. If ``servers`` was passed into the constructor, this will serialize the data and pipe it to each server using ``send_remote()``. Otherwise, this will communicate with ``sentry.models.GroupedMessage`` directly. """ message = base64.b64encode(json.dumps(data).encode('zlib')) for url in self.servers: timestamp = time.time() signature = get_signature(message, timestamp, self.secret_key or self.key) headers = { 'X-Sentry-Auth': get_auth_header(signature, timestamp, 'raven/%s' % (raven.VERSION,), self.public_key), 'Content-Type': 'application/octet-stream', } try: self.send_remote(url=url, data=message, headers=headers) except urllib2.HTTPError, e: body = e.read() self.logger.error('Unable to reach Sentry log server: %s (url: %%s, body: %%s)' % (e,), url, body, exc_info=True, extra={'data': {'body': body, 'remote_url': url}}) self.logger.log(data.pop('level', None) or logging.ERROR, data.pop('message', None)) except urllib2.URLError, e: self.logger.error('Unable to reach Sentry log server: %s (url: %%s)' % (e,), url, exc_info=True, extra={'data': {'remote_url': url}}) self.logger.log(data.pop('level', None) or logging.ERROR, data.pop('message', None))
def test_unknown_type(self): class Unknown(object): def __repr__(self): return "Unknown object" obj = Unknown() assert json.dumps(obj) == '"Unknown object"'
def test_unknown_type(self): class Unknown(object): def __repr__(self): return 'Unknown object' obj = Unknown() assert json.dumps(obj) == '"Unknown object"'
def test_custom_transport(self): c = Client(dsn="mock://*****:*****@localhost:8143/1") data = dict(a=42, b=55, c=list(range(50))) c.send(**data) mock_cls = c._transport_cache['mock://*****:*****@localhost:8143/1'].get_transport() expected_message = zlib.decompress(c.encode(data)) actual_message = zlib.decompress(mock_cls._data) # These loads()/dumps() pairs order the dict keys before comparing the string. # See GH504 self.assertEqual( json.dumps(json.loads(expected_message.decode('utf-8')), sort_keys=True), json.dumps(json.loads(actual_message.decode('utf-8')), sort_keys=True) )
def api_wrapper(*args, **kwargs): # ---------MOCK IMPLEMENTATION--------- posts = get_posts_with_more_positive_reactions() data = json.dumps(posts) return HttpResponse(data, status=200) """
def api_wrapper(*args, **kwargs): # ---------MOCK IMPLEMENTATION--------- reactions_count = get_total_reaction_count() data = json.dumps(reactions_count) return HttpResponse(data, status=200) """
def test_datetime(self): res = datetime.datetime(day=1, month=1, year=2011, hour=1, minute=1, second=1) assert json.dumps(res) == '"2011-01-01T01:01:01Z"'
def api_wrapper(*args, **kwargs): user = kwargs['user'] post_ids_list = get_posts_reacted_by_user( user_id=user.id ) posts_ids_list_json = json.dumps(post_ids_list) response = HttpResponse(posts_ids_list_json, status=200) return response
def test_recurse_exception(self): class NonAsciiRepr(object): def __repr__(self): return six.b('中文') x = [NonAsciiRepr()] result = transform(x, max_depth=1) self.assertEqual(json.dumps(result), six.b('["<class \'tests.utils.encoding.tests.NonAsciiRepr\'>"]'))
def test_datetime(self): res = datetime.datetime(day=1, month=1, year=2011, hour=1, minute=1, second=1) self.assertEquals(json.dumps(res), '"2011-01-01T01:01:01.000000Z"')
def api_wrapper(*args, **kwargs): post_id = kwargs['post_id'] try: dict_of_metrics = get_reaction_metrics(post_id=post_id) except InvalidPostException: raise BadRequest(*INVALID_POST) data = json.dumps({'reaction_metrics': dict_of_metrics}) response = HttpResponse(data, status=201) return response
def api_wrapper(*args, **kwargs): user = kwargs['user'] try: posts_list = get_user_posts(user_id=user.id) except: return HttpResponse('HOLA!..........') posts_list_json = json.dumps(posts_list) response = HttpResponse(posts_list_json, status=200) return response
def test_unknown_type_with_repr_error(self): class Unknown(object): def __repr__(self): raise Exception obj = Unknown() s = json.dumps(obj) assert isinstance(s, str) assert 'Unknown object at 0x' in s
def test_custom_transport(self): c = Client(dsn="mock://*****:*****@localhost:8143/1") data = dict(a=42, b=55, c=list(range(50))) c.send(**data) expected_message = zlib.decompress(base64.b64decode(c.encode(data))) self.assertIn('mock://localhost:8143/api/1/store/', Client._registry._transports) mock_cls = Client._registry._transports['mock://localhost:8143/api/1/store/'] actual_message = zlib.decompress(base64.b64decode(mock_cls._data)) # These loads()/dumps() pairs order the dict keys before comparing the string. # See GH504 self.assertEqual( json.dumps(json.loads(expected_message.decode('utf-8')), sort_keys=True), json.dumps(json.loads(actual_message.decode('utf-8')), sort_keys=True) )
def api_wrapper(*args, **kwargs): # ---------MOCK IMPLEMENTATION--------- storage = StateStorageImplementation() presenter = PresenterImplementation() interactor = GetDailyCumulativeReport(storage=storage, presenter=presenter) daily_cumulative_report = interactor.get_daily_cumulative_report() data = json.dumps(daily_cumulative_report) return HttpResponse(data, status=200)
def api_wrapper(*args, **kwargs): comment_id = kwargs['comment_id'] try: replies_details_list = get_replies_for_comment(comment_id=comment_id) except InvalidCommentException: raise BadRequest(*INVALID_COMMENT) replies_details_list_json = json.dumps(replies_details_list) response = HttpResponse(replies_details_list_json, status=200) return response
def _convert_to_json(sentry_data): """Tries to convert data to json using Raven's json serialiser. Everything in data should be serializable except possibly the contents of sentry_data['sentry.interfaces.Exception']. If a serialisation error occurs, a new attempt is made without the exception info. If that also doesn't work, then an empty JSON string '{}' is returned.""" try: return json.dumps(sentry_data) except TypeError, e: # try again without exception info if record.exc_info: sentry_data.pop(SENTRY_INTERFACES_EXCEPTION) try: return json.dumps(sentry_data) except TypeError, e: pass
def api_wrapper(*args, **kwargs): # ---------MOCK IMPLEMENTATION--------- user = kwargs['user'] user_reacted_posts = get_posts_reacted_by_user(user_id=user.id) data = json.dumps(user_reacted_posts) return HttpResponse(data, status=200) """
def api_wrapper(*args, **kwargs): offset = kwargs['request_query_params']['offset'] limit = kwargs['request_query_params']['limit'] user = kwargs['user'] user_posts_details_dict = get_user_posts(user_id=user.id, offset=offset, limit=limit) user_posts_details_dict_json = json.dumps(user_posts_details_dict) response = HttpResponse(user_posts_details_dict_json, status=200) return response
def api_wrapper(*args, **kwargs): post_id = kwargs['post_id'] try: post_reaction_deatails = get_reactions_to_post(post_id=post_id) except InvalidPostException: raise BadRequest(*INVALID_POST) post_reaction_deatails_json = json.dumps(post_reaction_deatails) response = HttpResponse(post_reaction_deatails_json, status=200) return response
def api_wrapper(*args, **kwargs): post_id = kwargs['post_id'] try: metrics_dict = get_reaction_metrics(post_id=post_id) except InvalidPostException: raise BadRequest(*INVALID_POST) metrics_dict_json = json.dumps(metrics_dict) response = HttpResponse(metrics_dict_json, status=200) return response
def api_wrapper(*args, **kwargs): # ---------MOCK IMPLEMENTATION--------- storage = StateStorageImplementation() presenter = PresenterImplementation() interactor = GetStateWiseDailyCasesReport(storage=storage, presenter=presenter) response = interactor.get_state_wise_daily_cases_report() data = json.dumps(response) return HttpResponse(data, status=200)
def test_custom_transport(self): c = Client(dsn="mock://*****:*****@localhost:8143/1") data = dict(a=42, b=55, c=list(range(50))) c.send(**data) expected_message = zlib.decompress(base64.b64decode(c.encode(data))) self.assertIn('mock://localhost:8143/api/1/store/', Client._registry._transports) mock_cls = Client._registry._transports[ 'mock://localhost:8143/api/1/store/'] actual_message = zlib.decompress(base64.b64decode(mock_cls._data)) # These loads()/dumps() pairs order the dict keys before comparing the string. # See GH504 self.assertEqual( json.dumps(json.loads(expected_message.decode('utf-8')), sort_keys=True), json.dumps(json.loads(actual_message.decode('utf-8')), sort_keys=True))
def api_wrapper(*args, **kwargs): user = kwargs['user'] request_data = kwargs['request_data'] post_content = request_data['content'] is_post_content_invalid = not post_content if is_post_content_invalid: raise BadRequest(*INVALID_POST_CONTENT) post = create_post(user_id=user.id, post_content=post_content) data = json.dumps({'post_id': post.id}) response = HttpResponse(data, status=201) return response
def api_wrapper(*args, **kwargs): # ---------MOCK IMPLEMENTATION--------- comment_id = kwargs['comment_id'] try: comment_replies = get_replies_for_comment(comment_id=comment_id) except InvalidCommentException: raise NotFound(*INVALID_COMMENT_ID) else: data = json.dumps(comment_replies) response = HttpResponse(data, status=200) return response """
def api_wrapper(*args, **kwargs): # ---------MOCK IMPLEMENTATION--------- post_id = kwargs['post_id'] try: user_reaction = get_reactions_to_post(post_id=post_id) except InvalidPostException: raise NotFound(*INVALID_POST_ID) else: data = json.dumps(user_reaction) response = HttpResponse(data, status=200) return response """
def api_wrapper(*args, **kwargs): # ---------MOCK IMPLEMENTATION--------- user = kwargs['user'] offset = kwargs['request_query_params']['offset'] limit = kwargs['request_query_params']['limit'] user_post_details = get_user_posts(user_id=user.id, offset=offset, limit=limit) data = json.dumps(user_post_details) return HttpResponse(data, status=200) """
def test_all_together(self): fs_key = frozenset([dir]) d = { (dir, ): frozenset([2 + 1j]), dir: collections.defaultdict(set), fs_key: [2 + 1j], } # Python 2/3 use different repr formats. assert json.dumps(d, sort_keys=True) == \ '{' \ '"(<built-in function dir>,)": ["(2+1j)"], ' \ '"<built-in function dir>": {}, ' \ '"%s": ["(2+1j)"]' \ '}' % repr(fs_key)
def api_wrapper(*args, **kwargs): # ---------MOCK IMPLEMENTATION--------- post_id = kwargs['post_id'] try: post_details = get_post( post_id=post_id ) except InvalidPostException: raise NotFound(*INVALID_POST_ID) else: data = json.dumps(post_details) return HttpResponse(data, status=200) """
def api_wrapper(*args, **kwargs): user = kwargs['user'] request_data = kwargs['request_data'] post_content = request_data['content'] try: post_id = create_post( user_id = user.id, post_content = post_content ) except InvalidPostContent: raise BadRequest(*INVALID_POST_CONTENT) data = json.dumps ({"post_id": post_id}) response = HttpResponse(data, status=200) return response
def test_sqs_transport(sqs_queue): sqs = boto3.client("sqs", region_name="us-east-1") c = Client( dsn="mock://*****:*****@localhost:8143/1" "?sqs_region=us-east-1&sqs_account=123456789012&sqs_name=sentry-queue", transport=SQSTransport) data = dict(a=42, b=55, c=list(range(50))) expected_message = zlib.decompress(c.encode(data)) c.send(**data) transport = c._transport_cache[ "mock://*****:*****@localhost:8143/1" "?sqs_region=us-east-1&sqs_account=123456789012" "&sqs_name=sentry-queue"].get_transport() assert transport.sqs_account == "123456789012" assert transport.sqs_name == "sentry-queue" assert type(transport.sqs_client).__name__ == type(sqs).__name__ assert transport.queue_url == "https://queue.amazonaws.com/123456789012/sentry-queue" # Check SQS for the message that was sent over: messages = sqs.receive_message(QueueUrl=transport.queue_url)["Messages"] assert len(messages) == 1 body = json.loads(messages[0]["Body"]) assert body["url"] == "mock://localhost:8143/api/1/store/" assert "sentry_secret=some_password" in body["headers"]["X-Sentry-Auth"] decoded_data = base64.b64decode(body["data"]) assert json.dumps(json.loads(expected_message.decode('utf-8')), sort_keys=True) == \ json.dumps(c.decode(decoded_data), sort_keys=True) # noqa
def api_wrapper(*args, **kwargs): # ---------MOCK IMPLEMENTATION--------- storage = StateStorageImplementation() presenter = PresenterImplementation() interactor = GetReportOFStateForDate(storage=storage, presenter=presenter) request_data = kwargs['request_data'] query = kwargs['request_query_params'] date = query['date'] try: response = interactor.get_report_of_state_for_date(date=date) except InvalidDate: raise NotFound(*INVALID_DATE) data = json.dumps(response) return HttpResponse(data, status=200)
def send(self, **data): """ Sends the message to the server. If ``servers`` was passed into the constructor, this will serialize the data and pipe it to each server using ``send_remote()``. Otherwise, this will communicate with ``sentry.models.GroupedMessage`` directly. """ message = base64.b64encode(json.dumps(data).encode('zlib')) for url in self.servers: timestamp = time.time() signature = get_signature(message, timestamp, self.secret_key or self.key) headers = { 'X-Sentry-Auth': get_auth_header(signature, timestamp, 'raven/%s' % (raven.VERSION,), self.public_key), 'Content-Type': 'application/octet-stream', } self.send_remote(url=url, data=message, headers=headers)
def test_scrub_sensitive_post(self): config = self.config def view(request): self.request = request raise ExpectedException() config.add_view(view, name='', renderer='string') app = self._makeApp() with mock.patch.object(pyramid_crow.Client, 'send') as mock_send: self.assertRaises(ExpectedException, app.post, '/', params=(('password', 'ohno'),)) sentry_data = mock_send.call_args[1] dumped_data = json.dumps(sentry_data) self.assertNotIn('ohno', dumped_data)
def send(self, **kwargs): """ Sends the message to the server. If ``servers`` was passed into the constructor, this will serialize the data and pipe it to each server using ``send_remote()``. Otherwise, this will communicate with ``sentry.models.GroupedMessage`` directly. """ message = base64.b64encode(json.dumps(kwargs).encode("zlib")) for url in self.servers: timestamp = time.time() signature = get_signature(self.key, message, timestamp) headers = { "Authorization": get_auth_header( signature, timestamp, "%s/%s" % (self.__class__.__name__, raven.VERSION) ), "Content-Type": "application/octet-stream", } try: self.send_remote(url=url, data=message, headers=headers) except urllib2.HTTPError, e: body = e.read() logger.error( "Unable to reach Sentry log server: %s (url: %%s, body: %%s)" % (e,), url, body, exc_info=True, extra={"data": {"body": body, "remote_url": url}}, ) logger.log(kwargs.pop("level", None) or logging.ERROR, kwargs.pop("message", None)) except urllib2.URLError, e: logger.error( "Unable to reach Sentry log server: %s (url: %%s)" % (e,), url, exc_info=True, extra={"data": {"remote_url": url}}, ) logger.log(kwargs.pop("level", None) or logging.ERROR, kwargs.pop("message", None))
def encode(self, data): dumped = json.dumps(data, cls=TornadoJSONEncoder).encode('utf-8') return base64.b64encode(zlib.compress(dumped))
def test_set(self): res = set(["foo", "bar"]) assert json.dumps(res) in ('["foo", "bar"]', '["bar", "foo"]')
def test_frozenset(self): res = frozenset(['foo', 'bar']) self.assertEquals(json.dumps(res), '["foo", "bar"]')
def test_decimal(self): d = {"decimal": Decimal("123.45")} assert json.dumps(d) == '{"decimal": "Decimal(\'123.45\')"}'
def test_uuid(self): res = uuid.uuid4() json.dumps(res) == '"%s"' % res.hex
def encode(self, data): """ Serializes ``data`` into a raw string. """ return base64.b64encode(json.dumps(data).encode('zlib'))
def test_uuid(self): res = uuid.uuid4() self.assertEquals(json.dumps(res), '"%s"' % res.hex)
def test_frozenset(self): res = frozenset(['foo', 'bar']) assert json.dumps(res) in ('["foo", "bar"]', '["bar", "foo"]')
def encode(self, data): """ Serializes ``data`` into a raw string. """ return zlib.compress(json.dumps(data).encode('utf8'))
def encode(self, data): """ Serializes ``data`` into a raw string. """ return base64.b64encode(zlib.compress(json.dumps(data).encode("utf8")))