def validate(self, notification, file_handle=None): # turn the notification into a json string data = None if isinstance(notification, models.IncomingNotification): data = notification.json() else: data = json.dumps(notification) # get the url that we are going to send to url = self._url("validate") resp = None if file_handle is None: # if there is no file handle supplied, send the metadata-only notification resp = http.post(url, data=data, headers={"Content-Type" : "application/json"}) else: # otherwise send both parts as a multipart message files = [ ("metadata", ("metadata.json", data, "application/json")), ("content", ("content.zip", file_handle, "application/zip")) ] resp = http.post(url, files=files) 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 ValidationException(resp.json().get("error")) return True
def create_notification(self, notification, file_handle=None): # turn the notification into a json string data = None if isinstance(notification, models.IncomingNotification): data = notification.json() else: data = json.dumps(notification) # get the url that we are going to send to url = self._url("notification") # 2016-06-20 TD : switch SSL verification off verify = False resp = None if file_handle is None: # if there is no file handle supplied, send the metadata-only notification resp = http.post(url, data=data, headers={"Content-Type": "application/json"}, verify=verify) else: # otherwise send both parts as a multipart message files = [("metadata", ("metadata.json", data, "application/json")), ("content", ("content.zip", file_handle, "application/zip"))] resp = http.post(url, files=files, verify=verify) 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 ValidationException(resp.json().get("error")) # extract the useful information from the acceptance response acc = resp.json() id = acc.get("id") loc = acc.get("location") return id, loc
def _query(self, batch, retries=10): data = json.dumps(batch) resp = http.post(self.lookup_url, retries=retries, headers={'Accept':'application/json'}, data=data) if resp is None: return None elif resp.status_code == requests.codes.ok: return resp.json() else: resp.raise_for_status()
def create_notification(self, notification, file_handle=None): # turn the notification into a json string data = None if isinstance(notification, models.IncomingNotification): data = notification.json() else: data = json.dumps(notification) # get the url that we are going to send to url = self._url("notification") # 2016-06-20 TD : switch SSL verification off verify = False resp = None if file_handle is None: # if there is no file handle supplied, send the metadata-only notification resp = http.post(url, data=data, headers={"Content-Type" : "application/json"}, verify=verify) else: # otherwise send both parts as a multipart message files = [ ("metadata", ("metadata.json", data, "application/json")), ("content", ("content.zip", file_handle, "application/zip")) ] resp = http.post(url, files=files, verify=verify) 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 ValidationException(resp.json().get("error")) # extract the useful information from the acceptance response acc = resp.json() id = acc.get("id") loc = acc.get("location") return id, loc
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 validate(self, notification, file_handle=None): # turn the notification into a json string data = None if isinstance(notification, models.IncomingNotification): data = notification.json() else: data = json.dumps(notification) # get the url that we are going to send to url = self._url("validate") resp = None if file_handle is None: # if there is no file handle supplied, send the metadata-only notification resp = http.post(url, data=data, headers={"Content-Type": "application/json"}) else: # otherwise send both parts as a multipart message files = [("metadata", ("metadata.json", data, "application/json")), ("content", ("content.zip", file_handle, "application/zip"))] resp = http.post(url, files=files) 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 ValidationException(resp.json().get("error")) return True
def create_article(self, article): # support either the article object or the dict representation article_data = article if isinstance(article, models.Article): article_data = article.data url = self.doaj_url(type="articles", params={"api_key" : self.api_key}) resp = http.post(url, data=json.dumps(article_data), headers={"Content-Type" : "application/json"}, retry_codes=DOAJ_RETRY_CODES) if resp.status_code == 400: raise DOAJException("Bad request against DOAJ API: '{x}'".format(x=resp.json().get("error", "no error provided"))) elif resp.status_code == 403: raise DOAJException("Forbidden action - your API key was incorrect, or you are trying to add an article with an ISSN you don't own") elif resp.status_code == 401: raise DOAJException("Authentication failed, your API key was probably wrong") j = resp.json() return j.get("id"), j.get("location")