Esempio n. 1
0
def get_taxon_with_ancestors(taxon_id: int) -> list[dict]:
    """Get a taxon with all its parents"""
    logger.info(f'API: Fetching parents of taxon {taxon_id}')
    results = get_taxa_by_id(taxon_id).get('results', [])
    if not results:
        logger.info(f'API: taxon {taxon_id} not found')
        return []

    taxon = results[0]
    logger.info(f'API: {len(taxon["ancestors"])} parent taxa found')
    return taxon['ancestors'] + [taxon]
Esempio n. 2
0
def test_get_taxa_by_id(requests_mock):
    taxon_id = 70118
    requests_mock.get(
        f'{API_V1_BASE_URL}/taxa/{taxon_id}',
        json=load_sample_data('get_taxa_by_id.json'),
        status_code=200,
    )

    response = get_taxa_by_id(taxon_id)
    result = response['results'][0]
    assert response['total_results'] == len(response['results']) == 1
    assert result['id'] == taxon_id
    assert result['name'] == 'Nicrophorus vespilloides'
    assert result['rank'] == 'species'
    assert result['is_active'] is True
    assert len(result['ancestors']) == 12
Esempio n. 3
0
def get_observation_from_metadata(metadata) -> tuple[dict, dict]:
    if not metadata.observation_id:
        logger.info('API: No observation ID specified')
        return None, None

    observation = get_observation(metadata.observation_id)
    taxon = None
    taxon_id = observation.get('taxon', {}).get('id')

    # Handle observation with no taxon ID (e.g., not yet identified)
    if taxon_id:
        taxon = get_taxa_by_id(taxon_id).get('results', [None])[0]
        logger.info(
            f'API: Found observation {metadata.observation_id} and taxon {taxon_id}'
        )
    else:
        logger.warning(
            f'API: Observation {metadata.observation_id} is unidentified')

    return taxon, observation
Esempio n. 4
0
    def from_id(cls, id: int) -> 'Taxon':
        """Lookup and create a new Taxon object by ID"""
        from pyinaturalist.v1 import get_taxa_by_id

        r = get_taxa_by_id(id)
        return cls.from_json(r['results'][0])
Esempio n. 5
0
def test_get_taxa_by_id__invalid_inputs(taxon_id):
    with pytest.raises(ValueError):
        get_taxa_by_id(taxon_id)