def _url(self, endpoint=None, id=None, auth=True, params=None, url=None): if url is None: url = self.base_url if url.endswith("/"): url += url[:-1] if endpoint is not None: url += "/" + endpoint if id is not None: url += "/" + http.quote(id) if auth: if params is None: params = {} if self.api_key is not None and self.api_key != "": params["api_key"] = self.api_key args = [] for k, v in params.iteritems(): args.append(k + "=" + http.quote(unicode(v))) if len(args) > 0: if "?" not in url: url += "?" else: url += "&" qs = "&".join(args) url += qs return url
def _url(self, endpoint=None, id=None, auth=True, params=None, url=None): if url is None: url = self.base_url if url.endswith("/"): url += url[:-1] if endpoint is not None: url += "/" + endpoint if id is not None: url += "/" + http.quote(id) if auth: if params is None: params = {} params["api_key"] = self.api_key args = [] for k, v in params.iteritems(): args.append(k + "=" + http.quote(unicode(v))) qs = "?" + "&".join(args) url += qs return url
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 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 doaj_url(self, endpoint=None, type=None, object_id=None, additional_path=None, params=None): """ build the api request url :param endpoint: the endpoint we're sending to. Unless "search" you can probably leave this out :param type: the type we're making the request to; could be application, applications, articles, journals :param object_id: the id of the object; only respected if type is given :param additional_path: additional path elements to add on the end of the constructed url (used mostly for search criteria) :param params: url parameters as a dict - these will be escaped for you, so just pass the values as they are :return: """ url = self.api_base_url if endpoint is not None: url += endpoint + "/" if type is not None: url += type if object_id is not None: url += "/" + object_id if additional_path is not None: if not url.endswith("/"): url += "/" url += additional_path qs = "" if params is not None: for k, v in params.iteritems(): if qs != "": qs += "&" qs += http.quote(k) + "=" + http.quote(str(v)) if qs != "": url += "?" + qs return url