

def get_or_create(self, data):
    descriptor = self.get_descriptor(data)
    urls = self.get_urls(data)
    read_response = self.requester.get(urls['read'])
    if (read_response.status_code != 200):
        message = 'Read error GET ({}): {}\n{}'.format(read_response.status_code, urls['read'], read_response.text)
        raise ValueError(message)
    read_response_data = read_response.json()
    pks = [item['id'] for item in read_response_data]
    if (pks == []):
        create_response = self.requester.post(urls['create'], data)
        if (create_response.status_code != 201):
            message = 'Create error POST ({}): {}\n{}\n{}'.format(create_response.status_code, urls['create'], data, create_response.text)
            raise ValueError(message)
        create_response_data = create_response.json()
        if self.many:
            pks = [response_data['id'] for response_data in create_response_data]
        else:
            pk = create_response_data['id']
        if self.verbose:
            if self.many:
                print('Created {} ({}, pks={})'.format(self.item_name, descriptor, pks))
            else:
                print('Created {} ({}, pk={})'.format(self.item_name, descriptor, pk))
        return (create_response_data, True)
    else:
        if ((len(pks) > 1) and (not self.many)):
            message = 'Found multiple {} instances ({}) for {}'.format(self.item_name, pks, descriptor)
            warnings.warn(message)
        if self.verbose:
            print('Existing {} ({}, pks={})'.format(self.item_name, descriptor, pks))
        return (read_response_data, False)

dataset/ETHPy150Open impactlab/eemeter/eemeter/uploader/uploaders.py BaseUploader.get_or_create/original