Skip to content

Instantly share code, notes, and snippets.

@clasense4
Last active December 19, 2020 06:12
Show Gist options
  • Save clasense4/714f830d8c817d0298322bfdaee2d480 to your computer and use it in GitHub Desktop.
Save clasense4/714f830d8c817d0298322bfdaee2d480 to your computer and use it in GitHub Desktop.
How to reverse URL Path into registered endpoint
# MAPPING created using DRF function
# https://github.com/encode/django-rest-framework/blob/master/rest_framework/schemas/generators.py#L72
# from rest_framework.schemas.generators import EndpointEnumerator
# EndpointEnumerator().get_api_endpoints()
# Input
MAPPING = {
"urn:app:api": [
"/api/v2/user/{id}",
"/api/v2/followers/{user_id}",
],
}
user_path = '/api/v2/user/e6e3ba83-9add-493e-8992-38151894a62d'
followers_path = '/api/v2/followers/e6e3ba83-9add-493e-8992-38151894a62d'
undefined_path = '/api/v2/undefined/e6e3ba83-9add-493e-8992-38151894a62d'
# Expected function and output
endpoint_validate("urn:app:api", user_path) # True
endpoint_validate("urn:app:api", followers_path) # True
endpoint_validate("urn:app:api", undefined_path) # False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment