Skip to content

Instantly share code, notes, and snippets.

@syshen
Created December 14, 2011 08:01
Show Gist options
  • Save syshen/1475673 to your computer and use it in GitHub Desktop.
Save syshen/1475673 to your computer and use it in GitHub Desktop.
import urllib
import httplib
import json
import time
import zlib
email = 'abcdef@test.com'
password = 'abcdef'
apikey = 'e965434a-3ed5-540a-9ef2-1f8ce1dc60f2'
def api_request(conn, method, api_path, params):
params['apikey'] = apikey
body = urllib.urlencode(params)
headers={}
headers["Content-type"] = "application/x-www-form-urlencoded"
headers["Accept-encoding"] = "gzip"
headers["Content-length"] = len(body)
conn.request(method, api_path, body, headers)
return conn.getresponse()
def api_response_read(api_ret):
accept_encoding = api_ret.getheader('content-encoding')
content_len = int(api_ret.getheader('content-length'))
resp_str = api_ret.read()
if accept_encoding == 'gzip':
decomp_str = zlib.decompress(resp_str, 15+32)
rlen = len(decomp_str)
ratio = content_len * 1.0 / rlen
print "compression ratio: {0}".format(ratio)
return decomp_str
else:
return resp_str
def main():
conn = httplib.HTTPConnection('localhost', 8082)
conn.set_debuglevel(0)
ret = api_request(conn, 'POST', '/v2/auth/login', {'email': email, 'password': password})
resp_str = api_response_read(ret)
login_obj = json.loads(resp_str)
ret = api_request(conn, 'POST', '/v2/users/get', {'session_token': login_obj['session_token'], 'user_id': login_obj['user']['user_id']})
resp_str = api_response_read(ret)
ret = api_request(conn, 'POST', '/v2/previews/get', {'session_token': login_obj['session_token'], 'url': 'http://docs.python.org/library/httplib.html'})
resp_str = api_response_read(ret)
conn.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment