Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added call to json.loads to convert JSON string returned by API to Py…

…thon object. This needs to be fixed further though to return an XML doc if the format is 'xml'.
  • Loading branch information...
commit fd572c47eb81978a8584168b5b7023e7b0f1f9d4 1 parent 49bce69
Colin Pollock colinpollock authored
Showing with 18 additions and 4 deletions.
  1. +18 −4 wordnik/wordnik.py
22 wordnik/wordnik.py
View
@@ -6,15 +6,25 @@
"""
import helpers
-import httplib, json, os, urllib, urllib2
+import httplib
+try:
+ import simplejson as json
+except ImportError:
+ import json
+import os
+import urllib
+import urllib2
from optparse import OptionParser
from xml.etree import ElementTree
from pprint import pprint
+from sys import stdout, stderr
DEFAULT_HOST = "api.wordnik.com"
DEFAULT_URI = "/v4"
DEFAULT_URL = "http://" + DEFAULT_HOST + DEFAULT_URI
-DEFAULT_FORMAT = "json"
+FORMAT_JSON = "json"
+FORMAT_XML = "xml"
+DEFAULT_FORMAT = FORMAT_JSON
class RestfulError(Exception):
"""Raised when response from REST API indicates an error has occurred."""
@@ -187,6 +197,7 @@ def authenticate(self, username, password):
def _do_http(uri, headers, body=None, method="GET", beta=False):
"""This wraps the HTTP call. This may get factored out in the future."""
if body:
+ #TODO: What's this mean? Should content ytpe always be json?
headers.update( {"Content-Type": "application/json"})
full_uri = DEFAULT_URI + uri
conn = httplib.HTTPConnection(DEFAULT_HOST)
@@ -195,7 +206,10 @@ def _do_http(uri, headers, body=None, method="GET", beta=False):
conn.request(method, full_uri, body, headers)
response = conn.getresponse()
if response.status == httplib.OK:
- return response.read()
+ text = response.read()
+ #TODO: use ElementTree.XML() instead of json.loads() if format is
+ # XML. But where is that passed in? I can't get to the
+ return json.loads(text)
else:
- print "{0}: {1}".format(response.status, response.reason)
+ print >> stderr, "{0}: {1}".format(response.status, response.reason)
return None
Please sign in to comment.
Something went wrong with that request. Please try again.