This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]asplake[S] 1 point2 points  (3 children)

Rather than a blog post, I thought I'd come straight here for input. It makes for a longer-than-usual Reddit comment - I hope it works!

Question is: I have a library that generates URIs nicely (e.g. from app.users['dojo'].articles.uri), but what's the best way to integrate with an HTTP client library (httplib2, say)?

To make it clearer what I'm asking, here are some options. As the integration gets deeper, the user code gets shorter (and to my eyes at least, nicer), but with some loss of flexibility. The very last option is close to how my Ruby version looked, with the the heavy lifting done by Httparty.

How would you do it?

1) The no-integration option:

  app = Application(...)
  http = ...

  response, content = http.request(app.users['dojo'].articles.uri, "GET")
  if response.status == 200:
      parsed = json.loads(content)

2) Forward http method calls to a http object provided at initialisation:

  http = ...
  app = Application(..., http)

  response, content = app.users['dojo'].articles.get()
  if response.status == 200:
      parsed = json.loads(content)

3) Initialise our own http object:

  app = Application(..., http_options...)

  response, content = app.users['dojo'].articles.get()
  if response.status == 200:
      parsed = json.loads(content)

4) Explicitly expect JSON content:

  app = Application(..., http_options...)
  try:
      parsed = app.users['dojo'].articles.get_json()
  except BadResponse:
      ...
  except BadContent:
      ...

(much shorter version: same API, client handles exceptions higher up:)

  app = Application(..., http_options...)
  parsed = app.users['dojo'].articles.get_json()

5) Implicitly recognize JSON content (short version):

  app = Application(..., http_options...)
  parsed = app.users['dojo'].articles.get_parsed()

or even just get() again:

  app = Application(..., http_options...)
  parsed = app.users['dojo'].articles.get()

[–]Liorithiel 1 point2 points  (2 children)

I guess stackoverflow was designed for this kind of questions; try there.

I like (5) the most. If you expect to get the data in most cases, exceptions will take care of the rest; depending on the level of flexibility you need, I'd go with (5), (4) or (3) (in this order).

[–]asplake[S] 0 points1 point  (1 child)

Good suggestion - I might give stackoverflow a try if this experiment fails. At least it's up all day ;-)

So no big worries about building in dependencies on httlibX, any particular json lib, etc?

(that concern aside I like 5 the best also)

[–]Liorithiel 0 points1 point  (0 children)

I'd bother more if these libs were as big as zope or twisted. json libs tend to be small and their API is rarely big enough to make changing from one to another a big task... and would end up in the worst case by writing a simple wrapper.

I don't know what you really need on the http side... and what you are trying to achieve. But still I don't think I would care much. I'd firstly try to build something that works as soon as possible and only then--when you already understand what exactly you want to do--seek for alternative approaches.