Skip to content

Commit

Permalink
Made dataset and table args object attributes instead of passing them…
Browse files Browse the repository at this point in the history
… around.
  • Loading branch information
mattdeboard committed Jul 23, 2011
1 parent 489d3cc commit c626bfa
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions python_usdol.py
Expand Up @@ -45,23 +45,21 @@ def _get_timestamp(self):
return (t, t.isoformat()+'Z') return (t, t.isoformat()+'Z')


def _get_message(self): def _get_message(self):
baseurl = '/%s/%s/' % (API_VER, self.dataset)
if self.table != '$metadata':
baseurl += self.table
date_time, timestamp = self._get_timestamp() date_time, timestamp = self._get_timestamp()
header_dict = {"Timestamp": timestamp, "ApiKey": self.token} header_dict = {"Timestamp": timestamp, "ApiKey": self.token}
return (header_dict, '%s&%s' % (self.baseurl, return (header_dict, '%s&%s' % (.baseurl, self._urlencode(header_dict)))
self._urlencode(header_dict)))


def _get_header(self): def _get_header(self):
d, message = self._get_message() d, message = self._get_message()
h = hmac.new(self.secret, message, hashlib.sha1) h = hmac.new(self.secret, message, hashlib.sha1)
d['Signature'] = h.hexdigest() d['Signature'] = h.hexdigest()
return self._urlencode(d) return self._urlencode(d)


def _get_request(self, ds, table, fmt='json'): def _get_request(self, fmt='json'):
url_args = [USDOL_URL, API_VER, d] url_args = [USDOL_URL, API_VER, self.dataset, self.table]
t = '$metadata'
if t:
t = table
url_args.append(t)
header = self._get_header() header = self._get_header()
qs = url_args.join('/') qs = url_args.join('/')
# url = urlparse.urljoin(USDOL_URL, qs) # url = urlparse.urljoin(USDOL_URL, qs)
Expand All @@ -76,13 +74,15 @@ def fetch_data(self, dataset, table='$metadata', fmt='json'):
'fmt' is json by default. Valid choices are 'xml' and 'json'. 'fmt' is json by default. Valid choices are 'xml' and 'json'.
''' '''
self.dataset = dataset
self.table = table
enc_opts = ['json', 'xml'] enc_opts = ['json', 'xml']
if fmt not in enc_opts: if fmt not in enc_opts:
raise AttributeError("Valid format choices are: json, xml") raise AttributeError("Valid format choices are: json, xml")
if table == '$metadata' and fmt != 'xml': if table == '$metadata' and fmt != 'xml':
fmt = 'xml' fmt = 'xml'


urlstr = self._get_request(dataset, table, fmt) urlstr = self._get_request(fmt)
data = urllib2.urlopen(urlstr) data = urllib2.urlopen(urlstr)
if fmt == 'json': if fmt == 'json':
ret = json.loads(data.read()) ret = json.loads(data.read())
Expand Down

0 comments on commit c626bfa

Please sign in to comment.