def get_notification(self, notification_id=None, location=None): # get the url that we are going to send to if notification_id is not None: url = self._url("notification", id=notification_id) elif location is not None: url = location else: raise JPERException("You must supply either the notification_id or the location") # get the response object resp = http.get(url) if resp is None: raise JPERConnectionException("Unable to communicate with the JPER API") if resp.status_code == 404: return None if resp.status_code != 200: raise JPERException("Received unexpected status code from {y}: {x}".format(x=resp.status_code, y=url)) j = resp.json() if "provider" in j: return models.ProviderOutgoingNotification(j) else: return models.OutgoingNotification(j)
def string_search(self, type, query_string, page=1, page_size=10, sort_by=None, sort_dir=None): # check this search is against an allowed type if type not in self.SEARCH_TYPES: raise DOAJException("Type {x} is not a supported search type".format(x=type)) # construct the url parameters params = {"page" : page, "pageSize" : page_size} if sort_by is not None: sort = sort_by if sort_dir is not None: sort += ":" + sort_dir params["sort"] = sort url = self.doaj_url("search", type, additional_path=http.quote(query_string), params=params) print url resp = http.get(url, retry_codes=DOAJ_RETRY_CODES) j = resp.json() klazz = self.CLASSMAP.get(type) if klazz is None: raise DOAJException("Type {x} does not have a class representation in the client".format(x=type)) obs = [klazz(r) for r in j.get("results", [])] return obs
def field_search(cls, field, value, fuzzy=False, page=1): wrap = "\"" if not fuzzy else "" quoted = quote(value, safe="/") qpage = quote(str(page)) if quoted is None or qpage is None: raise EuropePMCException(None, "unable to url escape the string") url = app.config.get( "EPMC_REST_API" ) + "search/query=" + field + ":" + wrap + quoted + wrap url += "&resultType=core&format=json&page=" + qpage app.logger.debug("Requesting EPMC metadata from " + url) resp = http.get(url) if resp is None: raise EuropePMCException( message="could not get a response from EPMC") if resp.status_code != 200: raise EuropePMCException(resp) try: j = resp.json() except: raise EuropePMCException( message="could not decode JSON from EPMC response") results = [ EPMCMetadata(r) for r in j.get("resultList", {}).get("result", []) ] return results
def query(cls, query_string, page=1, page_size=25): quoted = quote(query_string, safe="/") qpage = quote(str(page)) qsize = quote(str(page_size)) if qsize is None or qpage is None or quoted is None: raise EuropePMCException(None, "unable to url escape the string") url = app.config.get("EPMC_REST_API") + "search/query=" + query_string url += "&resulttype=core&format=json&page=" + qpage + "&pageSize=" + qsize app.logger.debug("Requesting EPMC metadata from " + url) resp = http.get(url) if resp is None: raise EuropePMCException( message="could not get a response from EPMC") if resp.status_code != 200: raise EuropePMCException(resp) try: j = resp.json() except: raise EuropePMCException( message="could not decode JSON from EPMC response") results = [ models.EPMCMetadata(r) for r in j.get("resultList", {}).get("result", []) ] return results
def get_notification(self, notification_id=None, location=None): # get the url that we are going to send to if notification_id is not None: url = self._url("notification", id=notification_id) elif location is not None: url = location else: raise JPERException( "You must supply either the notification_id or the location") # get the response object resp = http.get(url) if resp is None: raise JPERConnectionException( "Unable to communicate with the JPER API") if resp.status_code == 404: return None if resp.status_code != 200: raise JPERException("Received unexpected status code: {x}".format( x=resp.status_code)) j = resp.json() if "provider" in j: return models.ProviderOutgoingNotification(j) else: return models.OutgoingNotification(j)
def fulltext(cls, pmcid): url = app.config.get("EPMC_REST_API") + pmcid + "/fullTextXML" app.logger.debug("Searching for Fulltext at " + url) resp = http.get(url) if resp is None: raise EuropePMCException(message="could not get a response for fulltext from EPMC") if resp.status_code != 200: raise EuropePMCException(resp) return EPMCFullText(resp.text)
def get_by_issn(self, issn): url = self.base_url + "?issn=" + http.quote(issn) app.logger.info("Looking up ISSN in Romeo with URL {x}".format(x=url)) resp = http.get(url) if resp is None or resp.status_code != 200: app.logger.info("Unable to retrieve {x} from Romeo".format(x=issn)) raise RomeoClientException("Unable to get by issn") xml = xmlutil.fromstring(resp.text) return SearchResult(xml)
def list_notifications(self, since, page=None, page_size=None, repository_id=None): # check that the since date is valid, and get it into the right format if not hasattr(since, "strftime"): since = dates.parse(since) since = since.strftime("%Y-%m-%dT%H:%M:%SZ") # make the url params into an object params = {"since": since} if page is not None: try: params["page"] = str(page) except: raise JPERException( "Unable to convert page argument to string") if page_size is not None: try: params["pageSize"] = str(page_size) except: raise JPERException( "Unable to convert page_size argument to string") # get the url, which may contain the repository id if it is not None url = self._url("routed", id=repository_id, params=params) # 2016-06-20 TD : switch SSL verification off verify = False # get the response object resp = http.get(url, verify=verify) # check for errors or problems with the response if resp is None: raise JPERConnectionException( "Unable to communicate with the JPER API") if resp.status_code == 401: raise JPERAuthException( "Could not authenticate with JPER with your API key") if resp.status_code == 400: raise JPERException(resp.json().get("error")) if resp.status_code != 200: raise JPERException( "Received unexpected status code from {y}: {x} ".format( x=resp.status_code, y=url)) # create the notification list object j = resp.json() return models.NotificationList(j)
def request(self, uri, method, headers=None, payload=None): # Note that body can be file-like resp = None if method == "GET": resp = http.get(uri, headers=headers, auth=self.auth) elif method == "POST": resp = http.post(uri, headers=headers, data=payload, auth=self.auth) elif method == "PUT": resp = http.put(uri, headers=headers, data=payload, auth=self.auth) elif method == "DELETE": resp = http.delete(uri, headers=headers, auth=self.auth) if resp is None: return None, None return OctopusHttpResponse(resp), resp.text
def request(self, uri, method, headers=None, payload=None): # Note that body can be file-like resp = None if method == "GET": resp = http.get(uri, headers=headers, auth=self.auth) elif method == "POST": resp = http.post(uri, headers=headers, data=payload, auth=self.auth) elif method == "PUT": resp = http.put(uri, headers=headers, data=payload, auth=self.auth) elif method == "DELETE": resp = http.delete(uri, headers=headers, auth=self.auth) if resp is None: return OctopusHttpResponse(), u"" return OctopusHttpResponse(resp), resp.text
def list_notifications(self, since, page=None, page_size=None, repository_id=None): # check that the since date is valid, and get it into the right format if not hasattr(since, "strftime"): since = dates.parse(since) since = since.strftime("%Y-%m-%dT%H:%M:%SZ") # make the url params into an object params = {"since" : since} if page is not None: try: params["page"] = str(page) except: raise JPERException("Unable to convert page argument to string") if page_size is not None: try: params["pageSize"] = str(page_size) except: raise JPERException("Unable to convert page_size argument to string") # get the url, which may contain the repository id if it is not None url = self._url("routed", id=repository_id, params=params) # 2016-06-20 TD : switch SSL verification off verify = False # get the response object resp = http.get(url, verify=verify) # check for errors or problems with the response if resp is None: raise JPERConnectionException("Unable to communicate with the JPER API") if resp.status_code == 401: raise JPERAuthException("Could not authenticate with JPER with your API key") if resp.status_code == 400: raise JPERException(resp.json().get("error")) if resp.status_code != 200: raise JPERException("Received unexpected status code from {y}: {x} ".format(x=resp.status_code, y=url)) # create the notification list object j = resp.json() return models.NotificationList(j)
def field_search(cls, field, value, fuzzy=False, page=1): wrap = '"' if not fuzzy else "" quoted = quote(value, safe="/") qpage = quote(str(page)) if quoted is None or qpage is None: raise EuropePMCException(None, "unable to url escape the string") url = app.config.get("EPMC_REST_API") + "search/query=" + field + ":" + wrap + quoted + wrap url += "&resultType=core&format=json&page=" + qpage app.logger.debug("Requesting EPMC metadata from " + url) resp = http.get(url) if resp is None: raise EuropePMCException(message="could not get a response from EPMC") if resp.status_code != 200: raise EuropePMCException(resp) try: j = resp.json() except: raise EuropePMCException(message="could not decode JSON from EPMC response") results = [models.EPMCMetadata(r) for r in j.get("resultList", {}).get("result", [])] return results