11

I'm building a Django application that needs to interact with a 3rd party RESTful API, making various GETs, PUTs, etc to that resource. What I'm looking for is a good way to represent that API within Django.

The most obvious, but perhaps less elegant solution seems to be creating a model that has various methods mapping to webservice queries. On the other hand, it seems that using something like a custom DB backend would provide more flexibility and be better integrated into Django's ORM.

Caveat: This is the first real project I've done with Django, so it's possible I'm missing something obvious here.

devights
  • 263
  • 1
  • 4
  • 11

2 Answers2

9

The requests library makes it easy to write a REST API consumer. There is also a Python library called slumber, which is built on top of requests, for the explicit purpose of consuming REST APIs. How well that will work for you likely depends on how RESTful the API really is.

Charl Botha
  • 4,373
  • 34
  • 53
Mark Lavin
  • 24,664
  • 5
  • 76
  • 70
  • `slumber` is very easy, it does not give much for the abstraction layer. But in some cases may be enough. – Tadeck May 14 '12 at 20:57
  • Hmm, it looks like requests doesn't support using cert based authentication. Seems like I'm going to have to write something myself using httplib. – devights May 14 '12 at 22:37
  • Looks like requests now supports cert based authentication? The parameter cert can be a String path to ssl client cert file (.pem), or a Tuple, (‘cert’, ‘key’) pair - available on requests and sessions. – Chris Feb 28 '15 at 11:36
  • doesn't this block the whole process? – bryanph May 27 '15 at 14:19
  • Yes requests does not have built-in support for non-blocking IO. However, there are compatibility layers for `gevent` and `concurrent.futures` http://docs.python-requests.org/en/latest/user/advanced/#blocking-or-non-blocking – Mark Lavin May 27 '15 at 15:04
1

Make the REST calls using the builtin urllib (a bit clunky but functional) and wrap the interface into a class, with a method for each remote call. Your class can then translate to and from native python types. That is what I'd do anyway!

Nick Craig-Wood
  • 52,955
  • 12
  • 126
  • 132