Question

I'm planning an API for my own use, in Python. Perhaps someday I will make it freely accessible, but now I'm only planning to use it on my hobby site. At this stage I need some advice on how to set up the URLs, for files that receive both GET and POST requests.

Suppose one of my files is called function_A.py and used in this fashion:

www.example.com/api/function_A.py?a=something&k=other+thing

My question is, how does one set up 'pretty' URLs in an API, in the least resource-intensive way?

I see most APIs have a typical URL format like http://www.example.com/api/read instead of http://www.example.com/api/read.py

My options are probably limited to mod_rewrite v/s urls.py using Django - or is there another simpler option?

This is for the backend/API and I'd rather keep overhead to a minimum. I only want to handle 4 URLs in this fashion, and do not want to have a regex occur every time the URL is called.

Was it helpful?

Solution

The MYYN is correct. Using a framework such as Ruby on Rails or Django is the easiest way to create a RESTful API.

However, if you don't want to use a framework, you can do the same thing using mod_rewrite in Apache. In fact, this is precisely what most frameworks/applications running on Apache do.

For example, Wordpress uses an .htaccess file like this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

This directs all requests to index.php, where they can be parsed and processed.

Frameworks like RoR or Django do practically the same thing: all requests are redirected to a single file/class/function.

OTHER TIPS

My guess is, that web service APIs with pretty URLs (aka REST) are built via some kind of webframework (Werkzeug, web.py, django, pylons, cherrypy, plain wsgi, ...).

The module you are referring to takes care of url matching/handling to determine the correct route for a request.

From the documentation of the latter:

Routes tackles an interesting problem that comes up frequently in web development, how do you map URLs to your application’s actions? That is, how do you say that this should be accessed as /blog/2008/01/08, and /login should do that? Many web frameworks have a fixed dispatching system; e.g., /A/B/C means to read file C in directory B, or to call method C of class B in module A.B. These work fine until you need to refactor your code and realize that moving a method changes its public URL and invalidates users’ bookmarks. Likewise, if you want to reorganize your URLs and make a section into a subsection, you have to change your carefully-tested logic code.

Yes, .htaccess is the way that you can map from a pretty, external, URL in Apache Server to some grubby URL that your service platform accepts.

Some complex platforms do have means of producing nicer URLs as well.

There's no magic. You just so happen to be using a web server that was designed with file-based URLs in mind.

Most sites don't bother with file-based URLs anymore and instead do their own dispatch. This is what Django does, this is what ASP.NET MVC does.

What you need to lookup is mod_rewrite for Apache. It will allow you to map non file-based URLs to your Python files.

Alternatively, look at using a Python web framework that already does this. Web.py and Django are nice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top