Skip to content

Update CoMapeo fetch script to use new endpoints, including presetRef#175

Merged
rudokemper merged 11 commits intomainfrom
comapeo-refactor
Nov 6, 2025
Merged

Update CoMapeo fetch script to use new endpoints, including presetRef#175
rudokemper merged 11 commits intomainfrom
comapeo-refactor

Conversation

@rudokemper
Copy link
Member

@rudokemper rudokemper commented Nov 6, 2025

Goal

Closes #174.

Before syncing this, we will need to do https://github.com/ConservationMetrics/gc-programs/issues/114.

What I changed and why

  • Updated SAMPLE_OBSERVATIONS, and added SAMPLE_PRESETS for TDD; these were retrieved from a real CoMapeo cloud server running the latest version.
  • Use the latest endpoint routes
  • Include metadata fields now returned by /observation route
  • For each observation, fetch and extract preset data using a new helper function fetch_preset. This enables us to finally add, among other things, the category name, which has been requested by virtually all of our users. (Another nice one: the hash for color, so we can use the same colors e.g. in GC Explorer as what users saw on their CoMapeo map for each category)

What I'm not doing here

LLM use disclosure

Tests generated and improved by Claude Sonnet 4.5 in Cursor, with a strong amount of hand-holding.

```

### `GET /projects/abc123/observations`
### `GET /projects/abc123/observation`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and presets, is there a reason we use singular and not plural?

The reason to (in theory) use plural is to allow a hypothetical endpoint that lists all observations (or all presets):

/projects/abc123/observations
/projects/abc123/presets

Also for consistency with "projects" in the same URL.

But I will not press this point if you prefer to leave it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this is not our code, this is defined upstream isnt it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. See digidem/comapeo-cloud#61 (review)

They have changed some of the routes to singular, but not all (though it is their intention).

url = f"{server_url}/projects/{project_id}/preset/{preset_doc_id}"
headers = {
"Authorization": f"Bearer {access_token}",
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not a big deal yet so maybe we can sleep on this suggestion for now...

A common pattern is to not pass nitty-gritty details for the request like access_token around to different functions, but instead to instantiate a requests.Session() one time and pass that around.

So higher up in the stack (transform_comapeo_observations or even whatever calls it) would do this:

session = requests.Session()
session.headers.update({
    "Authorization": f"Bearer {access_token}"
})

# ... 

fetch_preset(..., session)

And then here:

def fetch_preset(server_url, session, project_id, preset_doc_id):
    try: 
        response = session.get(url)
        response.raise_for_status()
        # etc
    except ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion, thank you. I implemented this.

@rudokemper rudokemper merged commit 9285ca7 into main Nov 6, 2025
1 check passed
@rudokemper rudokemper deleted the comapeo-refactor branch November 6, 2025 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[comapeo] Extend fetch script to enrich data with preset metadata (name, color, icon, terms, ...)

2 participants