1

I am a Stack Overflow for Teams app developer, and given this recent announcement that API v3 is being extended to the public platform, I am hoping we can start asking API v3 questions here.

Given a list of tag names:

tag_names = [
    "tag1",
    "tag2",
    "tag3",
]

I need to get the list of corresponding tagId integer values.

The /tags route makes this a bit cumbersome because:

  • The tag name is always a partialName, thus requiring unwanted matches to be filtered.
  • Only one tag name can be specified per request, thus requiring multiple requests.

This could be simplified if:

  • A non-partial name parameter was supported.
  • The name and partialName parameters were enhanced to support an array of strings.

Has anyone else run into this challenge with API v3?

1 Answer 1

0

Here is what I do today, in case it helps someone:

def get_tags_by_partial_name(partial_name: Optional[str] = None) -> list:
    """Given a partial tag name, return the list of tags that match it."""
    return get_paged(
        url=f"{URL}/tags",
        params={
            "partialName": partial_name,
        },
    )

def get_tags_by_name(tag_names: list[str]) -> list:
    """Given a list of tag names, return a list of tags that exactly match them."""

    tags_by_id = {
        tag["id"]: tag
        for tag_name in tag_names
        for tag in get_tags_by_partial_name(tag_name)
        if tag["name"] in tag_names
    }

    return list(tags_by_id.values())

# Define the names of the tags to retrieve
tag_names = [
    "tag1",
    "tag2",
    "tag3",
]

# Get the tags that exactly match by name
tags = get_tags_by_name(tag_names)

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.