HOWTO: Write Python API wrapper?

14,928

Solution 1

I can't point you to any article on how to do it, but I think there are a few libraries that can be good models on how to design your own.

PyAws for example. I didn't see the source code so I can't tell you how good it is as code example, but the features and the usage examples in their website should be a useful design model

Universal Feed Parser is not a wrapper for a webservice (it's an RSS parser library), but it's a great example of a design that prioritizes usage flexibility and hiding implementation details. I think you can get very good usage ideas for your wrapper there.

Solution 2

My favorite combination is httplib2 (or pycurl for performance) and simplejson. As REST is more "a way of design" then a real "protocol" there is not really a reusable thing (that I know of). On Ruby you have something like ActiveResource. And to be honest, even that would just expose some tables as a webservice, whereas the power of xml/json is that they are more like "views" that can contain multiple objects optimized for your application. I hope this makes sense :-)

Share:
14,928
hanksims
Author by

hanksims

Updated on June 10, 2022

Comments

  • hanksims
    hanksims almost 2 years

    I'd like to write a python library to wrap a REST-style API offered by a particular Web service. Does anyone know of any good learning resources for such work, preferably aimed at intermediate Python programmers?

    I'd like a good article on the subject, but I'd settle for nice, clear code examples.

    CLARIFICATION: What I'm looking to do is write a Python client to interact with a Web service -- something to construct HTTP requests and parse XML/JSON responses, all wrapped up in Python objects.

  • jfs
    jfs about 15 years
    feedparser looks nice, indeed.
  • Asela Liyanage
    Asela Liyanage over 14 years
    Correct, there's nothing reusable, because REST is an architecture.
  • csparpa
    csparpa over 10 years
    To be even more precise, REST is "a way to make web architectures" - aka, a meta-architecture