def process_entity(cls, entity): """Turn json entity into a dictionary""" output = entity.copy() # Hold relation objects in specific dict. T1Service instantiates the # correct classes. relations = {} # for legacy/compatibility reasons with existing Entity code. if 'entity_type' in output: output['_type'] = output['entity_type'] for key, val in six.iteritems(output): if type( val ) is dict and 'entity_type' in val: # Get parent entities recursively cls.process_related_entity(relations, val, key) elif type(val) is list and 'entity_type' in next(iter(val), {}): for child in val: cls.process_related_entity(relations, child, key) # this is new as we are potentially returning multiple # currency types, but for now let's grab the first value elif type(val) is list and 'currency_code' in next(iter(val), {}): output[key] = val[0]['value'] if relations: output['relations'] = relations return output
def test_fetch_token_from_explicit_state_and_code(self): responses.add(responses.POST, 'https://api.mediamath.com/oauth2/v1.0/token', body=self.token, content_type='application/json') token = self.t1.fetch_token(state='state', code='code') for field, val in six.iteritems(expected_token): self.assertIn(field, token, 'Expected {} in token. Token received: {}' .format(field, token)) self.assertEqual(val, token[field], 'Bad value in token. Expected: {}, got: {}' .format(val, token[field]))
def test_fetch_token_from_authorization_response_url(self): responses.add(responses.POST, 'https://api.mediamath.com/oauth2/v1.0/token', body=self.token, content_type='application/json') url = 'https://www.mediamath.com/?code=code&state=state' token = self.t1.fetch_token(authorization_response_url=url) for field, val in six.iteritems(expected_token): self.assertIn(field, token, 'Expected {} in token. Token received: {}' .format(field, token)) self.assertEqual(val, token[field], 'Bad value in token. Expected: {}, got: {}' .format(val, token[field]))
def test_fetch_token_from_explicit_state_and_code(self): responses.add(responses.POST, 'https://api.mediamath.com/oauth2/v1.0/token', body=self.token, content_type='application/json') token = self.t1.fetch_token(state='state', code='code') for field, val in six.iteritems(expected_token): self.assertIn( field, token, 'Expected {} in token. Token received: {}'.format( field, token)) self.assertEqual( val, token[field], 'Bad value in token. Expected: {}, got: {}'.format( val, token[field]))
def test_fetch_token_from_authorization_response_url(self): responses.add(responses.POST, 'https://api.mediamath.com/oauth2/v1.0/token', body=self.token, content_type='application/json') url = 'https://www.mediamath.com/?code=code&state=state' token = self.t1.fetch_token(authorization_response_url=url) for field, val in six.iteritems(expected_token): self.assertIn( field, token, 'Expected {} in token. Token received: {}'.format( field, token)) self.assertEqual( val, token[field], 'Bad value in token. Expected: {}, got: {}'.format( val, token[field]))
def process_entity(cls, entity): """Turn json entity into a dictionary""" output = entity.copy() # Hold relation objects in specific dict. T1Service instantiates the # correct classes. relations = {} # for legacy/compatibility reasons with existing Entity code. if 'entity_type' in output: output['_type'] = output['entity_type'] for key, val in six.iteritems(output): if type(val) is dict and 'entity_type' in val: # Get parent entities recursively cls.process_related_entity(relations, val) elif type(val) is list and 'entity_type' in next(iter(val), {}): for child in val: cls.process_related_entity(relations, child) # this is new as we are potentially returning multiple # currency types, but for now let's grab the first value elif type(val) is list and 'currency_code' in next(iter(val), {}): output[key] = val[0]['value'] if relations: output['relations'] = relations return output
def process_entity(self, entity): """Turn json entity into a dictionary""" output = entity.copy() # Hold relation objects in specific dict. T1Service instantiates the # correct classes. relations = {} # for legacy/compatibility reasons with existing Entity code. if 'entity_type' in output: output['_type'] = output['entity_type'] for key, val in six.iteritems(output): # FIXME this isn't the correct thing to do; # however it's linked to an inconsistency in t1 json api - so fix when that's changed. if type(val) == list: # Get parent entities recursively for child in val: ent = self.process_entity(child) if child['rel'] == ent['_type']: relations[child['rel']] = ent else: relations.setdefault(child['rel'], []).append(ent) if relations: output['relations'] = relations return output