Hi Phil,

The best way to solve all these issues is to use mime types to
determine the type of return data.
For example application/vnd.{company name}.{endpoint name}+json;ver=1.
In the above you would replace {company name} with your company or
organization name, and {endpoint name} with something like account,
customer, or whatever the specific endpoint would be.
The +json could also be +api in your case.
The ;ver=1 would indicate the version number.

So why is this better?

   1. You no longer need /v1/ or /v2/ in the URI.
   2. Each endpoint can have different versions independently of any other
   endpoint.
   3. The type of data is defined by the mime type not by changing the URI.
   4. You can send one type of data and receive another type. For example
   using different Accept and Content-Type headers.

This is the best way to do this and solves a lot of issues down the road,
but it does require a bit more code written upfront.

~Carl

On Mon, Jun 21, 2021 at 10:28 AM Phil Gyford <[email protected]> wrote:

> Hi,
>
> I'm trying to set up DRF as a read-only API, and want to require that all
> URLs have either a .api or .json format extension.
>
> I've sort of managed this so far by copying the DefaultRouter class and
> in its get_urls() method changing this call:
>
>     urls = format_suffix_patterns(urls)
>
> to this:
>
>     urls = format_suffix_patterns(
>         urls, suffix_required=True, allowed=["api", "json"]
>     )
>
> This gets me what I need but leaves me with two remaining/resulting issues:
>
> 1. Tbe API Root URL looks like /api/v1/.api or /api/v1/.json. I feel it
> shouldn't have that final slash but, given all non-API URLs on the site
> have an ending slash, and I include my API URL conf with path("api/v1/",
> include("myproject.api.urls", namespace="v1")), I can't see how to fix it.
>
> 2. When I view the Browsable API, using the .api extension URLs, the
> links in the Breadcrumbs do not have the extension, and so clicking them
> 404s. I can't see any way to fix this other than subclassing
> BrowsableAPIRenderer and using a modified
> utils.breadcrumbs.get_breadcrumbs function.
>
> I didn't think this was an unreasonable thing to want – explicit URLs for
> each format – but given the amount of fiddling required to get it
> functioning, maybe this is a dumb idea? Have I missed something? How do you
> set up your URLs and formats?
>
> Many thanks,
> Phil Gyford
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-rest-framework/87e4d9e4-bde5-4285-ae66-dfdc32c3384bn%40googlegroups.com
> <https://groups.google.com/d/msgid/django-rest-framework/87e4d9e4-bde5-4285-ae66-dfdc32c3384bn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


-- 
-------------------------------------------------------------------------------
Carl J. Nobile (Software Engineer)
[email protected]
-------------------------------------------------------------------------------

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-rest-framework/CAGQqDQJN-S_NMAX%3D6e75ax%3DwrswXjY%2B9e0b_KumsiLVP835z-A%40mail.gmail.com.

Reply via email to