views:

59

answers:

4

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.

+1  A: 

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.

The MYYN
Thank you for helping me figure out that I should look up REST best practices!
Pranab
+1  A: 

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.

bmargulies
+1  A: 

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.

Frank Krueger
+1  A: 

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.

Can Berk Güder
Am I correct in thinking that explicitly redirecting 4 URLs is computationally cheaper than using a Regex-type RewriteCond, or am I over-optimizing?
Pranab
If there are only 4 URLs, you're probably correct. However, the .htaccess example I posted doesn't use regexes.
Can Berk Güder
Redirecting stuff through index.php just means shifting the logic there, and one more HTTP redirect. It's a perfectly practical and acceptable solution, but I was (pedantically, perhaps) wondering about the least resource-intensive.
Pranab
In this method, you don't redirect at all. You're just making index.php handle most requests. Rewrites aren't redirects. If you only have 4 URLs that will never change, going with index files in 4 directories might be easier, but you're trying to build an API, which will probably have more than 4 URLs, and parameters.
Can Berk Güder