Commit bdc7651b authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Update InvenioStore to use the modules requests instead of urllib.

parent e4010f30
......@@ -5,8 +5,8 @@
import httplib
import json
import re
import requests
import time
import urllib
from exception import CdsException
......@@ -50,11 +50,19 @@ class InvenioStore(object):
self._url = None
self._try = 0
def interogate(self, url):
# start a session, a persistent connection with the server
self._session = requests.Session()
def __del__(self):
# close the session
self._session.close()
def interogate(self, url, params=None):
"""Interrogate the store using the *URL*.
Args:
url (unicode): URL string
params (dict): parameters to be send with the URL
Returns:
unicode: the HTTP response
......@@ -67,10 +75,9 @@ class InvenioStore(object):
self._url = url
self._try += 1
fi = urllib.urlopen(url)
code = fi.getcode()
data = fi.read()
r = self._session.get(url, params=params)
code = r.status_code
data = r.content
# the server is busy or return error wait one minute an retry.
# the number of trial is limited to 5
......@@ -166,10 +173,9 @@ class InvenioStore(object):
self._try = 0
kwargs["jrec"] += N_IDS
params = urllib.urlencode(kwargs)
url = "http://%s/search?%s" % (self._host, params)
url = "http://%s/search" % self._host
rep = self.interogate(url)
rep = self.interogate(url, params=kwargs)
# check that the list of ids is well form
# [1291068, 1352722, 1376692, 1454870, 1492807] or [1493820] or []
......@@ -397,10 +403,9 @@ class InvenioStore(object):
kwargs["action_search"] = "Search"
params = urllib.urlencode(kwargs)
url = "http://%s/search?%s" % (self._host, params)
url = "http://%s/search" % self._host
return self.interogate(url)
return self.interogate(url, params=kwargs)
def search_year(self, collection, year, of="xm", rg=10, so="d"):
"""Search records for given *collection* and for a given *year*.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment