def test_split_lists(test_sequence, test_length, expected): result = _split_lists(test_sequence, test_length) assert expected == result
def get_author_putcodes(orcid, oauth_token): """Get put-codes for author works pushed by INSPIRE. Args: orcid (string): author's ORCID oauth_token (string): author's OAUTH token Returns: List[Tuple[string, string]]: list of tuples of the form (recid, put_code) with results. """ def timed_read_record_member(orcid, request_type, oauth_token, accept_type, put_code=None): with log_time_context('Request for %s for %s' % (request_type, orcid), LOGGER): return api.read_record_member( orcid, 'works', oauth_token, accept_type=accept_type, put_code=put_code, ) api = _get_api() # This reads the record _summary_ (no URLs attached): user_works = timed_read_record_member( orcid, 'works', oauth_token, accept_type='application/orcid+json', ) put_codes = [] for summary in chain(*get_value(user_works, 'group.work-summary', [])): source_client_id_dict = get_value(summary, 'source.source-client-id') put_code = summary.get('put-code') # Filter out records that are not ours and those with no put-code (unlikely) if source_client_id_dict and put_code: if source_client_id_dict.get('path') == app.config[ 'ORCID_APP_CREDENTIALS']['consumer_key']: put_codes.append(str(put_code)) # We can batch requests for _detailed_ records for maximum # `WORKS_BULK_QUERY_LIMIT` put-codes at once: detailed_works = [] for put_code_batch in _split_lists(put_codes, WORKS_BULK_QUERY_LIMIT): batch = timed_read_record_member( orcid, 'works', oauth_token, accept_type='application/orcid+json', put_code=put_code_batch, ) detailed_works.extend(batch['bulk']) # Now that we have all of the detailed records, we extract recids. # If it's not possible (and it should always be), we put the put-code in # `errors` author_putcodes = [] errors = [] for item in detailed_works: put_code = get_value(item, 'work.put-code') if not put_code: continue try: url = get_value(item, 'work.url.value') match = RECID_FROM_INSPIRE_URL.match(url) recid = match.group(1) except (TypeError, AttributeError): errors.append(str(put_code)) continue author_putcodes.append((recid, str(put_code))) if errors: LOGGER.error('Failed to match putcodes %s from %s to HEP records.', ', '.join(errors), orcid) return author_putcodes
def get_author_putcodes(orcid, oauth_token): """Get put-codes for author works pushed by INSPIRE. Args: orcid (string): author's ORCID oauth_token (string): author's OAUTH token Returns: List[Tuple[string, string]]: list of tuples of the form (recid, put_code) with results. """ api = _get_api() # This reads the record _summary_ (no URLs attached): user_works = api.read_record_member( orcid, 'works', oauth_token, accept_type='application/orcid+json', ) put_codes = [] for summary in chain(*get_value(user_works, 'group.work-summary', [])): source_client_id_dict = get_value(summary, 'source.source-client-id') put_code = summary.get('put-code') # Filter out records that are not ours and those with no put-code (unlikely) if source_client_id_dict and put_code: if source_client_id_dict.get('path') == app.config['ORCID_APP_CREDENTIALS']['consumer_key']: put_codes.append(str(put_code)) # We can batch requests for _detailed_ records for maximum # `WORKS_BULK_QUERY_LIMIT` put-codes at once: detailed_works = [] for put_code_batch in _split_lists(put_codes, WORKS_BULK_QUERY_LIMIT): batch = api.read_record_member( orcid, 'works', oauth_token, accept_type='application/orcid+json', put_code=put_code_batch, ) detailed_works.extend(batch['bulk']) # Now that we have all of the detailed records, we extract recids. # If it's not possible (and it should always be), we put the put-code in # `errors` author_putcodes = [] errors = [] for item in detailed_works: put_code = get_value(item, 'work.put-code') if not put_code: continue try: url = get_value(item, 'work.url.value') match = RECID_FROM_INSPIRE_URL.match(url) recid = match.group(1) except (TypeError, AttributeError): errors.append(str(put_code)) continue author_putcodes.append( (recid, str(put_code)) ) if errors: LOGGER.error( 'Failed to match putcodes {} from {} to HEP records.'.format( ', '.join(errors), orcid ) ) return author_putcodes