def results(self, with_row_id=False, raw=False, perfect_dict=None): results = self.response.content if perfect_dict is None: perfect_dict = self.table.connection.perfect_dict if results and results["Row"]: for item in results["Row"]: if raw: yield json_decode(item) yield self.table.__class__._extract_row_data( json_decode(item), perfect_dict=perfect_dict, with_row_id=with_row_id )
def results(self, with_row_id=False, raw=False, perfect_dict=None): results = self.response.content if perfect_dict is None: perfect_dict = self.table.connection.perfect_dict if results and results['Row']: for item in results['Row']: if raw: yield json_decode(item) yield self.table.__class__._extract_row_data( json_decode(item), perfect_dict=perfect_dict, with_row_id=with_row_id)
def __test_02_decode_data(self, test_encoded_data): """ Test decode data. """ res = json_decode(test_encoded_data) # , keys_to_bypass_decoding=['timestamp'] self.assertTrue(len(res) == len(test_encoded_data)) return res
def __test_01_encode_data(self, test_clean_data): """ Test encode data. """ res = json_decode(test_clean_data, decoder=base64.encodestring) self.assertTrue(len(res) == len(test_clean_data)) return res
def get_response(self): """ :return starbase.client.transport.HttpResponse: """ response_content = None response_raw = self.response if not self.fail_silently: response_raw.raise_for_status() if self.__connection.content_type == MEDIA_TYPE_JSON: try: response_content = self.response.json() except ValueError as e: response_content = None except Exception as e: pass else: raise NotImplementedError("Connection type {0} is " "not implemented.".format( self.__connection.content_type)) if self.decode_content and self.response.ok: # Make sure OK is ok. response_content = json_decode(response_content) return HttpResponse(response_content, response_raw)
def get_response(self): """ :return starbase.client.transport.HttpResponse: """ response_content = None response_raw = self.response if not self.fail_silently: response_raw.raise_for_status() if self.__connection.content_type == MEDIA_TYPE_JSON: try: response_content = self.response.json() except ValueError as e: response_content = None except Exception as e: pass else: raise NotImplementedError("Connection type {0} is not implemented.".format( self.__connection.content_type )) if self.decode_content and self.response.ok: # Make sure OK is ok. response_content = json_decode(response_content) return HttpResponse(response_content, response_raw)
def __test_02_decode_data(self, test_encoded_data): """ Test decode data. """ res = json_decode( test_encoded_data) # , keys_to_bypass_decoding=['timestamp'] self.assertTrue(len(res) == len(test_encoded_data)) return res
def get_content(self, decode_content=False, keys_to_bypass_decoding=[], keys_to_skip=[]): """ Gets response content. :param bool decode_content: If set to True, content is decoded with default decoder, having the empty keys ignored. :param list|tuple|set keys_to_bypass_decoding: List of keys to bypass decoding. :param list|tuple|set keys_to_skip: List of keys to ignore (won't be in the resulted content). :return str: """ if self.code == 200: # TODO - is it? if decode_content: return json_decode( self.content, keys_to_bypass_decoding = keys_to_bypass_decoding, keys_to_skip = keys_to_skip ) else: return self.content
test_clean_data_3 = { 'Cell': [{ '@column': 'sensor:id', '$': '345' }, { '@column': 'sensor:unit_of_measure', '$': 'dB' }, { '@column': 'machine:id', '$': '123' }, { '@column': 'component:id', '$': '234' }, { '@column': 'sensor:measurement', '$': '123456' }], '@key': 'row_a1ed8110-dbca-4093-a447-69157b38dca5' } print_("Encoding data\n================================\n") print_(json_decode(test_clean_data_1, decoder=base64.encodestring), '\n') print_(json_decode(test_clean_data_2, decoder=base64.encodestring), '\n') print_(json_decode(test_clean_data_3, decoder=base64.encodestring), '\n') print_("Decoding data\n================================\n") print_(json_decode(test_encoded_data_1), '\n') print_(json_decode(test_encoded_data_2), '\n') # , keys_to_bypass_decoding=['timestamp']
{'@column': 'sensor:measurement', '$': '123456'} ], '@key': 'row_a1ed8110-dbca-4093-a447-69157b38dca5'} } test_clean_data_2 = { 'Row': { 'Cell': {'@column': 'machine:id', '$': '0'}, '@key': 'row_e6f61c2f-c30a-4b38-8b68-451c538a0a5b0' } } test_clean_data_3 = { 'Cell': [ {'@column': 'sensor:id', '$': '345'}, {'@column': 'sensor:unit_of_measure', '$': 'dB'}, {'@column': 'machine:id', '$': '123'}, {'@column': 'component:id', '$': '234'}, {'@column': 'sensor:measurement', '$': '123456'}], '@key': 'row_a1ed8110-dbca-4093-a447-69157b38dca5' } print_("Encoding data\n================================\n") print_(json_decode(test_clean_data_1, decoder=base64.encodestring), '\n') print_(json_decode(test_clean_data_2, decoder=base64.encodestring), '\n') print_(json_decode(test_clean_data_3, decoder=base64.encodestring), '\n') print_("Decoding data\n================================\n") print_(json_decode(test_encoded_data_1), '\n') print_(json_decode(test_encoded_data_2), '\n') # , keys_to_bypass_decoding=['timestamp']