comparison whoisi_cache.py @ 7:98f1da3b585c

added WhoisiServer class and tests
author Atul Varma <varmaa@toolness.com>
date Thu, 31 Dec 2009 13:33:28 -0800
parents 5a9e578c80fa
children 7b6bec689805
comparison
equal deleted inserted replaced
6:5a9e578c80fa 7:98f1da3b585c
1 import logging
2 import urllib
3 import urllib2
4
5 try:
6 import json
7 except ImportError:
8 import simplejson as json
9
1 MAX_PEOPLE_REQ_SIZE = 100 10 MAX_PEOPLE_REQ_SIZE = 100
11 DEFAULT_URL = "http://whoisi.com/"
2 12
3 def split_seq(seq, size): 13 def split_seq(seq, size):
4 """ 14 """
5 Split up the given sequence into pieces of the given size. 15 Split up the given sequence into pieces of the given size.
6 16
7 Taken from http://code.activestate.com/recipes/425044/. 17 Taken from http://code.activestate.com/recipes/425044/.
8 """ 18 """
9 19
10 return [seq[i:i+size] for i in range(0, len(seq), size)] 20 return [seq[i:i+size] for i in range(0, len(seq), size)]
21
22 class WhoisiServer(object):
23 def __init__(self, url=DEFAULT_URL, urlopen=urllib2.urlopen):
24 self._urlopen = urlopen
25 self.url = url
26
27 def _call_api_method(self, name, query_args):
28 full_url = "%sapi/%s?%s" % (self.url, name,
29 urllib.urlencode(query_args))
30 logging.debug('retrieving %s' % full_url)
31 return json.loads(self._urlopen(full_url))
32
33 def get_person(self, app, person):
34 return self._call_api_method('getPerson',
35 [('app', app),
36 ('person', str(person))])
37
38 def get_people(self, app, first, last):
39 return self._call_api_method('getPeople',
40 [('app', app),
41 ('first', str(first)),
42 ('last', str(last))])
43
44 def get_max_person_id(self, app):
45 return self._call_api_method('getMaxPersonID',
46 [('app', app)])
11 47
12 class WhoisiCache(object): 48 class WhoisiCache(object):
13 APP_NAME = "whoisi-cache" 49 APP_NAME = "whoisi-cache"
14 50
15 def __init__(self, server, storage, batch_size=MAX_PEOPLE_REQ_SIZE): 51 def __init__(self, server, storage, batch_size=MAX_PEOPLE_REQ_SIZE):