marco@16: #!/usr/bin/env python marco@16: # -*- coding: utf-8 -*- marco@16: marco@16: """ marco@16: This module provides the 'Connection' class, a SWORD2 client. marco@16: marco@16: #BETASWORD2URL marco@16: See http://sword-app.svn.sourceforge.net/viewvc/sword-app/spec/trunk/SWORDProfile.html?revision=HEAD for information marco@16: about the SWORD2 AtomPub profile. marco@16: marco@16: """ marco@16: from sword2_logging import logging marco@16: conn_l = logging.getLogger(__name__) marco@16: marco@16: from utils import Timer, NS, get_md5, create_multipart_related marco@16: marco@16: from transaction_history import Transaction_History marco@16: from service_document import ServiceDocument marco@16: from deposit_receipt import Deposit_Receipt marco@16: from error_document import Error_Document marco@16: from collection import Sword_Statement marco@16: from exceptions import * marco@16: marco@16: from compatible_libs import etree marco@16: marco@16: import httplib2 marco@16: marco@16: class Connection(object): marco@16: """ marco@16: `Connection` - SWORD2 client marco@16: marco@16: This connection is predicated on having a Service Document (SD), preferably by an instance being constructed with marco@16: the Service Document IRI (SD-IRI) which can dereference to the XML document itself. marco@16: marco@16: marco@16: Contructor parameters: marco@16: marco@16: There are a number of flags that can be set when getting an instance of this class that affect the behaviour marco@16: of the client. See the help for `self.__init__` for more details. marco@16: marco@16: Example usage: marco@16: marco@16: >>> from sword2 import Connection marco@16: >>> conn = Connection("http://example.org/service-doc") # An SD-IRI is required. marco@16: marco@16: marco@16: marco@16: marco@16: # Get, validate and parse the document at the SD_IRI: marco@16: >>> conn.get_service_document() marco@16: marco@16: # Load a Service Document from a string: marco@16: >>> conn.load_service_document(xml_service_doc) marco@16: 2011-05-30 01:06:13,251 - sword2.service_document - INFO - Initial SWORD2 validation checks on service document - Valid document? True marco@16: marco@16: # View transaction history (if enabled) marco@16: >>> print conn.history.to_pretty_json() marco@16: [ marco@16: { marco@16: "sd_iri": "http://example.org/service-doc", marco@16: "timestamp": "2011-05-30T01:05:54.071042", marco@16: "on_behalf_of": null, marco@16: "type": "init", marco@16: "user_name": null marco@16: }, marco@16: { marco@16: "IRI": "http://example.org/service-doc", marco@16: "valid": true, marco@16: "sword_version": "2.0", marco@16: "duration": 0.0029349327087402344, marco@16: "timestamp": "2011-05-30T01:06:13.253907", marco@16: "workspaces_found": [ marco@16: "Main Site", marco@16: "Sub-site" marco@16: ], marco@16: "type": "SD Parse", marco@16: "maxUploadSize": 16777216 marco@16: } marco@16: ] marco@16: marco@16: # Start a connection and do not maintain a transaction history marco@16: # Useful for bulk-testing where the history might grow exponentially marco@16: >>> conn = Connection(...... , keep_history=False, ....) marco@16: marco@16: # Initialise a connection and get the document at the SD IRI: marco@16: # (Uses the Simple Sword Server as an endpoint - sss.py marco@16: marco@16: >>> from sword2 import Connection marco@16: >>> c = Connection("http://localhost:8080/sd-uri", download_service_document=True) marco@16: 2011-05-30 02:04:24,179 - sword2.connection - INFO - keep_history=True--> This instance will keep a JSON-compatible transaction log of all (SWORD/APP) activities in 'self.history' marco@16: 2011-05-30 02:04:24,215 - sword2.connection - INFO - Received a document for http://localhost:8080/sd-uri marco@16: 2011-05-30 02:04:24,216 - sword2.service_document - INFO - Initial SWORD2 validation checks on service document - Valid document? True marco@16: >>> print c.history marco@16: -------------------- marco@16: Type: 'init' [2011-05-30T02:04:24.180182] marco@16: Data: marco@16: user_name: None marco@16: on_behalf_of: None marco@16: sd_iri: http://localhost:8080/sd-uri marco@16: -------------------- marco@16: Type: 'SD_IRI GET' [2011-05-30T02:04:24.215661] marco@16: Data: marco@16: sd_iri: http://localhost:8080/sd-uri marco@16: response: {'status': '200', 'content-location': 'http://localhost:8080/sd-uri', 'transfer-encoding': 'chunked', 'server': 'CherryPy/3.1.2 WSGI Server', 'date': 'Mon, 30 May 2011 01:04:24 GMT', 'content-type': 'text/xml'} marco@16: process_duration: 0.0354170799255 marco@16: -------------------- marco@16: Type: 'SD Parse' [2011-05-30T02:04:24.220798] marco@16: Data: marco@16: maxUploadSize: 16777216 marco@16: sd_iri: http://localhost:8080/sd-uri marco@16: valid: True marco@16: sword_version: 2.0 marco@16: workspaces_found: ['Main Site'] marco@16: process_duration: 0.00482511520386 marco@16: marco@16: Please see the testsuite for this class for more examples of the sorts of transactions that can be done. (tests/test_connection*.py) marco@16: """ marco@16: marco@16: def __init__(self, service_document_iri, marco@16: user_name=None, marco@16: user_pass=None, marco@16: on_behalf_of=None, marco@16: download_service_document = False, # Don't automagically GET the SD_IRI by default marco@16: keep_history=True, marco@16: cache_deposit_receipts=True, marco@16: honour_receipts=True, marco@16: error_response_raises_exceptions=True): marco@16: """ marco@16: Creates a new Connection object. marco@16: marco@16: Parameters: marco@16: marco@16: Connection(service_document_iri, <--- REQUIRED - use a dummy string here if the SD is local only. marco@16: marco@16: # OPTIONAL parameters (default values are shown below) marco@16: marco@16: # Authentication parameters: (can use any method that `httplib2` provides) marco@16: marco@16: user_name=None, marco@16: user_pass=None, marco@16: marco@16: # Set the SWORD2 On Behalf Of value here, for it to be included as part of every transaction marco@16: # Can be passed to every transaction method (update resource, delete deposit, etc) otherwise marco@16: marco@16: on_behalf_of=None, marco@16: marco@16: ## Behaviour Flags marco@16: # Try to GET the service document from the provided SD-IRI in `service_document_iri` if True marco@16: marco@16: download_service_document = False, # Don't automagically GET the SD_IRI by default marco@16: marco@16: # Keep a history of all transactions made with the SWORD2 Server marco@16: # Records details like the response headers, sent headers, times taken and so forth marco@16: # Kept in a `sword2.transaction_history:Transaction_History` object but can be treated like an ordinary `list` marco@16: keep_history=True, marco@16: marco@16: # Keep a cache of all deposit receipt responses from the server and provide an 'index' to these `sword2.Deposit_Receipt` objects marco@16: # by Edit-IRI, Content-IRI and Sword-Edit-IRI. (ie given an Edit-IRI, find the deposit receipt for the last received response containing marco@16: # that IRI. marco@16: # If the following flag, `honour_receipts` is set to True, packaging checks and other limits set in these receipts will be marco@16: # honoured. marco@16: # For example, a request for an item with an invalid packaging type will never reach the server, but throw an exception. marco@16: marco@16: cache_deposit_receipts=True, marco@16: marco@16: # Make sure to behave as required by the SWORD2 server - not sending too large a file, not asking for invalid packaging types and so on. marco@16: marco@16: honour_receipts=True, marco@16: marco@16: # Two means of handling server error responses: marco@16: # If set to True - An exception will be thrown from `sword2.exceptions` (caused by any server error response w/ marco@16: # HTTP code greater than or equal to 400) marco@16: # OR marco@16: # If set to False - A `sword2.error_document:Error_Document` object will be returned. marco@16: marco@16: error_response_raises_exceptions=True marco@16: ) marco@16: marco@16: If a `Connection` is created with the parameter `download_service_document` set to `False`, then no attempt marco@16: to dereference the `service_document_iri` (SD-IRI) will be made at this stage. marco@16: marco@16: To cause it to get or refresh the service document from this IRI, call `self.get_service_document()` marco@16: marco@16: Loading in a locally held Service Document: marco@16: marco@16: >>> conn = Connection(....) marco@16: marco@16: >>> with open("service_doc.xml", "r") as f: marco@16: ... conn.load_service_document(f.read()) marco@16: marco@16: marco@16: """ marco@16: self.sd_iri = service_document_iri marco@16: self.sd = None marco@16: marco@16: # Client behaviour flags: marco@16: # Honour deposit receipts - eg raise exceptions if interactions are attempted that the service document marco@16: # does not allow without bothering the server - invalid packaging types, max upload sizes, etc marco@16: self.honour_receipts = honour_receipts marco@16: marco@16: # When error_response_raises_exceptions == True: marco@16: # Error responses (HTTP codes >399) will raise exceptions (from sword2.exceptions) in response marco@16: # when False: marco@16: # Error Responses, if Content-Type is text/xml or application/xml, a sword2.error_document.Error_Document will be the marco@16: # return - No Exception will be raised! marco@16: # Check Error_Document.code to get the response code, regardless to whether a valid Sword2 error document was received. marco@16: self.raise_except = error_response_raises_exceptions marco@16: marco@16: self.keep_cache = cache_deposit_receipts marco@16: marco@16: marco@16: # DEBUG marco@16: httplib2.debuglevel = 0 marco@16: marco@16: marco@16: self.h = httplib2.Http(".cache", timeout=30.0) marco@16: marco@16: self.user_name = user_name marco@16: self.on_behalf_of = on_behalf_of marco@16: marco@16: # Cached Deposit Receipt 'indexes' *cough, cough* marco@16: self.edit_iris = {} # Key = IRI, Value = ref to latest Deposit Receipt for the resource marco@16: self.cont_iris = {} # Key = IRI, Value = ref to latest Deposit Receipt marco@16: self.se_iris = {} # Key = IRI, Value = ref to latest Deposit Receipt marco@16: self.cached_at = {} # Key = Edit-IRI, Value = Timestamp for when receipt was cached marco@16: marco@16: # Transaction history hooks marco@16: self.history = None marco@16: self._t = Timer() marco@16: self.keep_history = keep_history marco@16: if keep_history: marco@16: conn_l.info("keep_history=True--> This instance will keep a JSON-compatible transaction log of all (SWORD/APP) activities in 'self.history'") marco@16: self.reset_transaction_history() marco@16: self.history.log('init', marco@16: sd_iri = self.sd_iri, marco@16: user_name = self.user_name, marco@16: on_behalf_of = self.on_behalf_of ) marco@16: # Add credentials to http client marco@16: if user_name: marco@16: conn_l.info("Adding username/password credentials for the client to use.") marco@16: self.h.add_credentials(user_name, user_pass) marco@16: marco@16: if self.sd_iri and download_service_document: marco@16: self._t.start("get_service_document") marco@16: self.get_service_document() marco@16: conn_l.debug("Getting service document and dealing with the response: %s s" % self._t.time_since_start("get_service_document")[1]) marco@16: marco@16: def _return_error_or_exception(self, cls, resp, content): marco@16: """Internal method for reporting errors, behaving as the `self.raise_except` flag requires. marco@16: marco@16: `self.raise_except` can be altered at any time to affect this methods behaviour.""" marco@16: if self.raise_except: marco@16: raise cls(resp) marco@16: else: marco@16: if resp['content-type'] in ['text/xml', 'application/xml']: marco@16: conn_l.info("Returning an error document, due to HTTP response code %s" % resp.status) marco@16: e = Error_Document(content, code=resp.status, resp = resp) marco@16: return e marco@16: else: marco@16: conn_l.info("Returning due to HTTP response code %s" % resp.status) marco@16: e = Error_Document(code=resp.status, resp = resp) marco@16: return e marco@16: marco@16: def _handle_error_response(self, resp, content): marco@16: """Catch a number of general HTTP error responses from the server, based on HTTP code marco@16: marco@16: 401 - Unauthorised. marco@16: Will throw a `sword2.exceptions.NotAuthorised` exception, if exceptions are set to be on. marco@16: Otherwise will return a `sword2.Error_Document` (likewise for the rest of these) marco@16: marco@16: 403 - Forbidden. marco@16: Will throw a `sword2.exceptions.Forbidden` exception marco@16: marco@16: 404 - Not Found. marco@16: Will throw a `sword2.exceptions.NotFound` exception marco@16: marco@16: 408 - Request Timeout marco@16: Will throw a `sword2.exceptions.RequestTimeOut` exception marco@16: marco@16: 500-599 errors: marco@16: Will throw a general `sword2.exceptions.ServerError` exception marco@16: marco@16: 4XX not listed: marco@16: Will throw a general `sword2.exceptions.HTTPResponseError` exception marco@16: """ marco@16: if resp['status'] == "401": marco@16: conn_l.error("You are unauthorised (401) to access this document on the server. Check your username/password credentials and your 'On Behalf Of'") marco@16: self._return_error_or_exception(NotAuthorised, resp, content) marco@16: elif resp['status'] == "403": marco@16: conn_l.error("You are Forbidden (401) to POST to '%s'. Check your username/password credentials and your 'On Behalf Of'") marco@16: self._return_error_or_exception(Forbidden, resp, content) marco@16: elif resp['status'] == "408": marco@16: conn_l.error("Request Timeout (408) - error uploading.") marco@16: self._return_error_or_exception(RequestTimeOut, resp, content) marco@16: elif int(resp['status']) > 499: marco@16: conn_l.error("Server error occured. Response headers from the server:\n%s" % resp) marco@16: print content marco@16: self._return_error_or_exception(ServerError, resp, content) marco@16: else: marco@16: conn_l.error("Unknown error occured. Response headers from the server:\n%s" % resp) marco@16: print "-------------------------" marco@16: print content marco@16: self._return_error_or_exception(HTTPResponseError, resp, content) marco@16: marco@16: def _cache_deposit_receipt(self, d): marco@16: """Method for storing the deposit receipts, and also for providing lookup dictionaries that marco@16: reference these objects. marco@16: marco@16: (only provides cache if `self.keep_cache` is `True` [via the `cache_deposit_receipts` init parameter flag]) marco@16: marco@16: Provides and maintains: marco@16: self.edit_iris -- a `dict`, keys: Edit-IRI hrefs, values: `sword2.Deposit_Receipt` objects they appear in marco@16: marco@16: self.cont_iris -- a `dict`, keys: Content-IRI hrefs, values: `sword2.Deposit_Receipt` objects they appear in marco@16: marco@16: self.se_iris -- a `dict`, keys: Sword-Edit-IRI hrefs, values: `sword2.Deposit_Receipt` objects they appear in marco@16: marco@16: self.cached_at -- a `dict`, keys: Edit-IRIs, values: timestamp when receipt was last cached. marco@16: """ marco@16: if self.keep_cache: marco@16: timestamp = self._t.get_timestamp() marco@16: conn_l.debug("Caching document (Edit-IRI:%s) - at %s" % (d.edit, timestamp)) marco@16: self.edit_iris[d.edit] = d marco@16: if d.cont_iri: # SHOULD exist within receipt marco@16: self.cont_iris[d.cont_iri] = d marco@16: if d.se_iri: marco@16: # MUST exist according to the spec, but as it can be the same as the Edit-IRI marco@16: # it seems likely that a server implementation might ignore the 'MUST' part. marco@16: self.se_iris[d.se_iri] = d marco@16: self.cached_at[d.edit] = self._t.get_timestamp() marco@16: else: marco@16: conn_l.debug("Caching request denied - deposit receipt caching is set to 'False'") marco@16: marco@16: def load_service_document(self, xml_document): marco@16: """Load the Service Document XML from bytestring, `xml_document` marco@16: marco@16: Useful if SD-IRI is non-existant or invalid. marco@16: marco@16: Will set the following convenience attributes: marco@16: marco@16: `self.sd` -- the `sword2.ServiceDocument` instance marco@16: marco@16: `self.workspaces` -- a `list` of workspace tuples, of the form: marco@16: ('Workspace atom:title', [<`sword2.Collection` object>, ....]), marco@16: marco@16: `self.maxUploadSize` -- the maximum filesize for a deposit, if given in the service document marco@16: """ marco@16: self._t.start("SD Parse") marco@16: self.sd = ServiceDocument(xml_document) marco@16: _, took_time = self._t.time_since_start("SD Parse") marco@16: # Set up some convenience references marco@16: self.workspaces = self.sd.workspaces marco@16: self.maxUploadSize = self.sd.maxUploadSize marco@16: marco@16: if self.history: marco@16: if self.sd.valid: marco@16: self.history.log('SD Parse', marco@16: sd_iri = self.sd_iri, marco@16: valid = self.sd.valid, marco@16: workspaces_found = [k for k,v in self.sd.workspaces], marco@16: sword_version = self.sd.version, marco@16: maxUploadSize = self.sd.maxUploadSize, marco@16: process_duration = took_time) marco@16: else: marco@16: self.history.log('SD Parse', marco@16: sd_iri = self.sd_iri, marco@16: valid = self.sd.valid, marco@16: process_duration = took_time) marco@16: marco@16: def get_service_document(self): marco@16: """Perform an HTTP GET on the Service Document IRI (SD-IRI) and attempt to parse the result as marco@16: a SWORD2 Service Document (using `self.load_service_document`) marco@16: """ marco@16: headers = {} marco@16: if self.on_behalf_of: marco@16: headers['on-behalf-of'] = self.on_behalf_of marco@16: self._t.start("SD_URI request") marco@16: resp, content = self.h.request(self.sd_iri, "GET", headers=headers) marco@16: _, took_time = self._t.time_since_start("SD_URI request") marco@16: if self.history: marco@16: self.history.log('SD_IRI GET', marco@16: sd_iri = self.sd_iri, marco@16: response = resp, marco@16: process_duration = took_time) marco@16: if resp['status'] == "200": marco@16: conn_l.info("Received a document for %s" % self.sd_iri) marco@16: self.load_service_document(content) marco@16: elif resp['status'] == "401": marco@16: conn_l.error("You are unauthorised (401) to access this document on the server. Check your username/password credentials") marco@16: elif resp['status'] == "403": marco@16: conn_l.error("Access forbidden (403). Check your username/password credentials") marco@16: self._return_error_or_exception(Forbidden, resp, content) marco@16: def reset_transaction_history(self): marco@16: """ Clear the transaction history - `self.history`""" marco@16: del self.history marco@16: self.history = Transaction_History() marco@16: marco@16: def _make_request(self, marco@16: target_iri, marco@16: payload=None, # These need to be set to upload a file marco@16: mimetype=None, marco@16: filename=None, marco@16: packaging=None, marco@16: marco@16: metadata_entry=None, # a sword2.Entry needs to be here, if marco@16: # a metadata entry is to be uploaded marco@16: marco@16: # Set both a file and a metadata entry for the method to perform a multipart marco@16: # related upload. marco@16: suggested_identifier=None, # 'slug' marco@16: in_progress=True, marco@16: on_behalf_of=None, marco@16: metadata_relevant=False, marco@16: marco@16: # flags: marco@16: empty = None, # If this is True, then the POST/PUT is sent with an empty body marco@16: # and the 'Content-Length' header explicitly set to 0 marco@16: method = "POST", marco@16: request_type="" # text label for transaction history reports marco@16: ): marco@16: """Performs an HTTP request, as defined by the parameters. This is an internally used method and it is best that it marco@16: is not called directly. marco@16: marco@16: target_iri -- IRI that will be the target of the HTTP call marco@16: marco@16: # File upload parameters: marco@16: payload - the payload to send. Can be either a bytestring or a File-like object that supports `payload.read()` marco@16: mimetype - MIMEType of the payload marco@16: filename - filename. Most SWORD2 uploads have this as being mandatory. marco@16: packaging - the SWORD2 packaging type of the payload. marco@16: eg packaging = 'http://purl.org/net/sword/package/Binary' marco@16: marco@16: # NB to work around a possible bug in httplib2 0.6.0, the file-like object is read into memory rather than streamed marco@16: # from disc, so is not as efficient as it should be. That said, it is recommended that file handles are passed to marco@16: # the _make_request method, as this is hoped to be a temporary situation. marco@16: marco@16: metadata_entry - a `sword2.Entry` to be uploaded with metadata fields set as desired. marco@16: marco@16: # If there is both a payload and a metadata_entry, then the request will be made as a Multipart-related request marco@16: # Otherwise, it will be a normal request for whicever type of upload. marco@16: marco@16: empty - a flag to specify that an empty request should be made. A blank body and a 'Content-Length:0' header will be explicitly added marco@16: and any payload or metadata_entry passed in will be ignored. marco@16: marco@16: marco@16: # Header flags: marco@16: suggested_identifier -- set the 'Slug' header marco@16: in_progress -- 'In-Progress' marco@16: on_behalf_of -- 'On-Behalf-Of' marco@16: metadata_relevant -- 'Metadata-Relevant' marco@16: marco@16: # HTTP settings: marco@16: method -- "GET", "POST", etc marco@16: request_type -- A label to be used in the transaction history for this particular operation. marco@16: marco@16: Response: marco@16: marco@16: A `sword2.Deposit_Receipt` object containing the deposit receipt data. If the response was blank or marco@16: not a Deposit Response, then only a few attributes will be populated: marco@16: marco@16: `code` -- HTTP code of the response marco@16: `response_headers` -- `dict` of the reponse headers marco@16: `content` -- (Optional) in case the response body is not empty but the response is not a Deposit Receipt marco@16: marco@16: If exception-throwing is turned off (`error_response_raises_exceptions = False` or `self.raise_except = False`) marco@16: then the response will be a `sword2.Error_Document`, but will still have the aforementioned attributes set, (code, marco@16: response_headers, etc) marco@16: """ marco@16: if payload: marco@16: md5sum, f_size = get_md5(payload) marco@16: marco@16: # request-level headers marco@16: headers = {} marco@16: headers['In-Progress'] = str(in_progress).lower() marco@16: if on_behalf_of: marco@16: headers['On-Behalf-Of'] = self.on_behalf_of marco@16: elif self.on_behalf_of: marco@16: headers['On-Behalf-Of'] = self.on_behalf_of marco@16: marco@16: if suggested_identifier: marco@16: headers['Slug'] = str(suggested_identifier) marco@16: marco@16: if suggested_identifier: marco@16: headers['Slug'] = str(suggested_identifier) marco@16: marco@16: if metadata_relevant: marco@16: headers['Metadata-Relevant'] = str(metadata_relevant).lower() marco@16: marco@16: if hasattr(payload, 'read'): marco@16: # Need to work out why a 401 challenge will stop httplib2 from sending the file... marco@16: # likely need to make it re-seek to 0... marco@16: # In the meantime, read the file into memory... *sigh* marco@16: payload = payload.read() marco@16: marco@16: self._t.start(request_type) marco@16: if empty: marco@16: # NULL body with explicit zero length. marco@16: headers['Content-Length'] = "0" marco@16: resp, content = self.h.request(target_iri, method, headers=headers) marco@16: _, took_time = self._t.time_since_start(request_type) marco@16: if self.history: marco@16: self.history.log(request_type + ": Empty request", marco@16: sd_iri = self.sd_iri, marco@16: target_iri = target_iri, marco@16: method = method, marco@16: response = resp, marco@16: headers = headers, marco@16: process_duration = took_time) marco@16: elif method == "DELETE": marco@16: resp, content = self.h.request(target_iri, method, headers=headers) marco@16: _, took_time = self._t.time_since_start(request_type) marco@16: if self.history: marco@16: self.history.log(request_type + ": DELETE request", marco@16: sd_iri = self.sd_iri, marco@16: target_iri = target_iri, marco@16: method = method, marco@16: response = resp, marco@16: headers = headers, marco@16: process_duration = took_time) marco@16: marco@16: elif metadata_entry and not (filename and payload): marco@16: # Metadata-only resource creation marco@16: headers['Content-Type'] = "application/atom+xml;type=entry" marco@16: data = str(metadata_entry) marco@16: headers['Content-Length'] = str(len(data)) marco@16: marco@16: resp, content = self.h.request(target_iri, method, headers=headers, body = data) marco@16: _, took_time = self._t.time_since_start(request_type) marco@16: if self.history: marco@16: self.history.log(request_type + ": Metadata-only resource request", marco@16: sd_iri = self.sd_iri, marco@16: target_iri = target_iri, marco@16: method = method, marco@16: response = resp, marco@16: headers = headers, marco@16: process_duration = took_time) marco@16: marco@16: elif metadata_entry and filename and payload: marco@16: # Multipart resource creation marco@16: multicontent_type, payload_data = create_multipart_related([{'key':'atom', marco@16: 'type':'application/atom+xml; charset="utf-8"', marco@16: 'data':str(metadata_entry), # etree default is utf-8 marco@16: }, marco@16: {'key':'payload', marco@16: 'type':str(mimetype), marco@16: 'filename':filename, marco@16: 'data':payload, marco@16: 'headers':{'Content-MD5':str(md5sum), marco@16: 'Packaging':str(packaging), marco@16: } marco@16: } marco@16: ]) marco@16: marco@16: headers['Content-Type'] = multicontent_type + '; type="application/atom+xml"' marco@16: headers['Content-Length'] = str(len(payload_data)) # must be str, not int type marco@16: resp, content = self.h.request(target_iri, method, headers=headers, body = payload_data) marco@16: _, took_time = self._t.time_since_start(request_type) marco@16: if self.history: marco@16: self.history.log(request_type + ": Multipart resource request", marco@16: sd_iri = self.sd_iri, marco@16: target_iri = target_iri, marco@16: response = resp, marco@16: headers = headers, marco@16: method = method, marco@16: multipart = [{'key':'atom', marco@16: 'type':'application/atom+xml; charset="utf-8"' marco@16: }, marco@16: {'key':'payload', marco@16: 'type':str(mimetype), marco@16: 'filename':filename, marco@16: 'headers':{'Content-MD5':str(md5sum), marco@16: 'Packaging':str(packaging), marco@16: } marco@16: }], # record just the headers used in multipart construction marco@16: process_duration = took_time) marco@16: elif filename and payload: marco@16: headers['Content-Type'] = str(mimetype) marco@16: headers['Content-MD5'] = str(md5sum) marco@16: headers['Content-Length'] = str(f_size) marco@16: headers['Content-Disposition'] = "attachment; filename=%s" % filename # TODO: ensure filename is ASCII marco@16: headers['Packaging'] = str(packaging) marco@16: #print headers marco@16: resp, content = self.h.request(target_iri, method, headers=headers, body = payload) marco@16: _, took_time = self._t.time_since_start(request_type) marco@16: print content marco@16: if self.history: marco@16: self.history.log(request_type + ": simple resource request", marco@16: sd_iri = self.sd_iri, marco@16: target_iri = target_iri, marco@16: method = method, marco@16: response = resp, marco@16: headers = headers, marco@16: process_duration = took_time) marco@16: else: marco@16: conn_l.error("Parameters were not complete: requires a metadata_entry, or a payload/filename/packaging or both") marco@16: raise Exception("Parameters were not complete: requires a metadata_entry, or a payload/filename/packaging or both") marco@16: marco@16: if resp['status'] == "201": marco@16: # Deposit receipt in content marco@16: conn_l.info("Received a Resource Created (201) response.") marco@16: # Check response headers for updated Location IRI marco@16: location = resp.get('location', None) marco@16: if len(content) > 0: marco@16: # Fighting chance that this is a deposit receipt marco@16: d = Deposit_Receipt(xml_deposit_receipt = content) marco@16: if d.parsed: marco@16: conn_l.info("Server response included a Deposit Receipt. Caching a copy in .resources['%s']" % d.edit) marco@16: d.response_headers = dict(resp) marco@16: d.location = location marco@16: d.code = 201 marco@16: self._cache_deposit_receipt(d) marco@16: return d marco@16: else: marco@16: # No body... marco@16: d = Deposit_Receipt() marco@16: conn_l.info("Server response dir not include a Deposit Receipt.") marco@16: d.response_headers = dict(resp) marco@16: d.code = 201 marco@16: d.location = location marco@16: return d marco@16: elif resp['status'] == "204": marco@16: # Deposit receipt in content marco@16: conn_l.info("Received a valid 'No Content' (204) response.") marco@16: location = resp.get('location', None) marco@16: # Check response headers for updated Locatio marco@16: return Deposit_Receipt(response_headers = dict(resp), location=location, code=204) marco@16: elif resp['status'] == "200": marco@16: # Deposit receipt in content marco@16: conn_l.info("Received a valid (200) OK response.") marco@16: content_type = resp.get('content-type') marco@16: location = resp.get('location', None) marco@16: if content_type == "application/atom+xml;type=entry" and len(content) > 0: marco@16: d = Deposit_Receipt(content) marco@16: if d.parsed: marco@16: conn_l.info("Server response included a Deposit Receipt. Caching a copy in .resources['%s']" % d.edit) marco@16: d.response_headers = dict(resp) marco@16: d.location = location marco@16: d.code = 200 marco@16: self._cache_deposit_receipt(d) marco@16: return d marco@16: else: marco@16: # No atom entry... marco@16: d = Deposit_Receipt() marco@16: conn_l.info("Server response dir not include a Deposit Receipt Entry.") marco@16: d.response_headers = dict(resp) marco@16: d.location = location marco@16: d.content = content marco@16: return d marco@16: else: marco@16: return self._handle_error_response(resp, content) marco@16: marco@16: marco@16: marco@16: def create(self, marco@16: workspace=None, # Either provide workspace/collection or marco@16: collection=None, # the exact Col-IRI itself marco@16: col_iri=None, marco@16: marco@16: payload=None, # These need to be set to upload a file marco@16: mimetype=None, marco@16: filename=None, marco@16: packaging=None, marco@16: marco@16: metadata_entry=None, # a sword2.Entry needs to be here, if marco@16: # a metadata entry is to be uploaded marco@16: marco@16: # Set both a file and a metadata entry for the method to perform a multipart marco@16: # related upload. marco@16: marco@16: suggested_identifier=None, marco@16: in_progress=True, marco@16: on_behalf_of=None, marco@16: ): marco@16: """ marco@16: Creating a Resource marco@16: =================== marco@16: marco@16: #BETASWORD2URL marco@16: See 6.3 Creating a Resource http://sword-app.svn.sourceforge.net/viewvc/sword-app/spec/trunk/SWORDProfile.html?revision=HEAD#protocoloperations_creatingresource marco@16: marco@16: Basic parameters: marco@16: marco@16: This method can create a new resource in a Collection on a SWORD2 server, given suitable authentication to do so. marco@16: marco@16: Select a collection to send a request to by either: marco@16: marco@16: setting the param `col_iri` to its Collection-IRI or Col-IRI marco@16: marco@16: or marco@16: marco@16: setting 'workspace' and 'collection' to the labels for the desired workspace and collection. marco@16: marco@16: SWORD2 request parameters: marco@16: marco@16: `suggested_identifier` -- the suggested identifier of this resource (HTTP header of 'Slug'), marco@16: marco@16: `in_progress` (`True` or `False`) -- whether or not the deposit should be considered by the marco@16: server to be in progress ('In-Progress') marco@16: `on_behalf_of` -- if this is a mediated deposit ('On-Behalf-Of') marco@16: (the client-wide setting `self.on_behalf_of will be used otherwise) marco@16: marco@16: marco@16: 1. "Binary File Deposit in a given Collection" marco@16: ---------------------------------------------- marco@16: marco@16: Set the following parameters in addition to the basic parameters: marco@16: marco@16: `payload` - the payload to send. Can be either a bytestring or a File-like object that supports `payload.read()` marco@16: `mimetype` - MIMEType of the payload marco@16: `filename` - filename. Most SWORD2 uploads have this as being mandatory. marco@16: `packaging` - the SWORD2 packaging type of the payload. marco@16: eg packaging = 'http://purl.org/net/sword/package/Binary' marco@16: marco@16: Response: marco@16: marco@16: A `sword2.Deposit_Receipt` object containing the deposit receipt data. If the response was blank or marco@16: not a Deposit Response, then only a few attributes will be populated: marco@16: marco@16: `code` -- HTTP code of the response marco@16: `response_headers` -- `dict` of the reponse headers marco@16: `content` -- (Optional) in case the response body is not empty but the response is not a Deposit Receipt marco@16: marco@16: If exception-throwing is turned off (`error_response_raises_exceptions = False` or `self.raise_except = False`) marco@16: then the response will be a `sword2.Error_Document`, but will still have the aforementioned attributes set, (code, marco@16: response_headers, etc) marco@16: marco@16: 2. "Creating a Resource with an Atom Entry" marco@16: ------------------------------------------- marco@16: marco@16: create a container within a SWORD server and optionally provide it with metadata without adding any binary content to it. marco@16: marco@16: Set the following parameters in addition to the basic parameters: marco@16: marco@16: `metadata_entry` - An instance of `sword2.Entry`, set with the metadata required. marco@16: marco@16: for example: marco@16: # conn = `sword2.Connection`, collection_iri = Collection-IRI marco@16: >>> from sword2 import Entry marco@16: >>> entry = Entry(title = "My new deposit", marco@16: ... id = "foo:id", marco@16: ... dcterms_abstract = "My Thesis", marco@16: ... dcterms_author = "Me", marco@16: ... dcterms_issued = "2009") marco@16: marco@16: >>> conn.create(col_iri = collection_iri, marco@16: ... metadata_entry = entry, marco@16: ... in_progress = True) marco@16: # likely to want to add the thesis files later for example but get the identifier for the deposit now marco@16: marco@16: Response: marco@16: marco@16: A `sword2.Deposit_Receipt` object containing the deposit receipt data. If the response was blank or marco@16: not a Deposit Response, then only a few attributes will be populated: marco@16: marco@16: `code` -- HTTP code of the response marco@16: `response_headers` -- `dict` of the reponse headers marco@16: `content` -- (Optional) in case the response body is not empty but the response is not a Deposit Receipt marco@16: marco@16: If exception-throwing is turned off (`error_response_raises_exceptions = False` or `self.raise_except = False`) marco@16: then the response will be a `sword2.Error_Document`, but will still have the aforementioned attributes set, (code, marco@16: response_headers, etc) marco@16: marco@16: 3. "Creating a Resource with a Multipart Deposit" marco@16: ------------------------------------------------- marco@16: marco@16: Create a resource in a given collection by uploading a file AND the metadata about this resource. marco@16: marco@16: To make this sort of request, just set the parameters as shown for both the binary upload and the metadata upload. marco@16: marco@16: eg: marco@16: marco@16: >>> conn.create(col_iri = collection_iri, marco@16: ... metadata_entry = entry, marco@16: ... payload = open("foo.zip", "r"), marco@16: ... mimetype = marco@16: .... and so on marco@16: marco@16: Response: marco@16: marco@16: A `sword2.Deposit_Receipt` object containing the deposit receipt data. If the response was blank or marco@16: not a Deposit Response, then only a few attributes will be populated: marco@16: marco@16: `code` -- HTTP code of the response marco@16: `response_headers` -- `dict` of the reponse headers marco@16: `content` -- (Optional) in case the response body is not empty but the response is not a Deposit Receipt marco@16: marco@16: If exception-throwing is turned off (`error_response_raises_exceptions = False` or `self.raise_except = False`) marco@16: then the response will be a `sword2.Error_Document`, but will still have the aforementioned attributes set, (code, marco@16: response_headers, etc) marco@16: marco@16: (under the hood, this request uses Atom Multipart-related) marco@16: marco@16: From the spec: marco@16: marco@16: "In order to ensure that all SWORD clients and servers can exchange a full range of file content and metadata, the use of Atom Multipart [AtomMultipart] is permitted to combine a package (possibly a simple ZIP) with a set of Dublin Core metadata terms [DublinCore] embedded in an Atom Entry. marco@16: marco@16: The SWORD server is not required to support packaging formats, but this profile RECOMMENDS that the server be able to accept a ZIP file as the Media Part of an Atom Multipart request (See Section 5: IRIs and Section 7: Packaging for more details)." marco@16: """ marco@16: conn_l.debug("Create Resource") marco@16: if not col_iri: marco@16: for w, collections in self.workspaces: marco@16: if w == workspace: marco@16: for c in collections: marco@16: if c.title == collection: marco@16: conn_l.debug("Matched: Workspace='%s', Collection='%s' ==> Col-IRI='%s'" % (workspace, marco@16: collection, marco@16: c.href)) marco@16: col_iri = c.href marco@16: break marco@16: marco@16: if not col_iri: # no col_iri provided and no valid workspace/collection given marco@16: conn_l.error("No suitable Col-IRI was found, with the given parameters.") marco@16: return marco@16: marco@16: return self._make_request(target_iri = col_iri, marco@16: payload=payload, marco@16: mimetype=mimetype, marco@16: filename=filename, marco@16: packaging=packaging, marco@16: metadata_entry=metadata_entry, marco@16: suggested_identifier=suggested_identifier, marco@16: in_progress=in_progress, marco@16: on_behalf_of=on_behalf_of, marco@16: method="POST", marco@16: request_type='Col_IRI POST') marco@16: marco@16: def update(self, metadata_entry = None, # required for a metadata update marco@16: payload = None, # required for a file update marco@16: filename = None, # required for a file update marco@16: mimetype=None, # required for a file update marco@16: packaging=None, # required for a file update marco@16: marco@16: dr = None, # Important! Without this, you will have to set the edit_iri AND the edit_media_iri parameters. marco@16: marco@16: edit_iri = None, marco@16: edit_media_iri = None, marco@16: marco@16: metadata_relevant=False, marco@16: in_progress=False, marco@16: on_behalf_of=None, marco@16: ): marco@16: """ marco@16: Replacing the Metadata and/or Files of a Resource marco@16: marco@16: #BETASWORD2URL marco@16: See http://sword-app.svn.sourceforge.net/viewvc/sword-app/spec/trunk/SWORDProfile.html?revision=HEAD#protocoloperations_editingcontent_multipart marco@16: marco@16: Replace the metadata and/or files of a resource. marco@16: marco@16: This wraps a number of methods and relies on being passed the Deposit Receipt, as the target IRI changes depending marco@16: on whether the metadata, the files or both are to be updated by the request. marco@16: marco@16: This method has the same functionality as the following methods: marco@16: update_files_for_resource marco@16: update_metadata_for_resource marco@16: update_metadata_and_files_for_resource marco@16: marco@16: Usage: marco@16: ------ marco@16: marco@16: Set the target for this request: marco@16: -------------------------------- marco@16: marco@16: You MUST pass back the `sword2.Deposit_Receipt` object you got from a previous transaction as the `dr` parameter, marco@16: and the correct IRI will automatically be chosen based on what combination of files you want to upload. marco@16: marco@16: Then, add in the metadata and/or file information as desired: marco@16: ------------------------------------------------------------- marco@16: marco@16: File information requires: marco@16: marco@16: `payload` - the payload to send. Can be either a bytestring or a File-like object that supports `payload.read()` marco@16: `mimetype` - MIMEType of the payload marco@16: `filename` - filename. Most SWORD2 uploads have this as being mandatory. marco@16: `packaging` - the SWORD2 packaging type of the payload. marco@16: eg packaging = 'http://purl.org/net/sword/package/Binary' marco@16: marco@16: `metadata_relevant` - This should be set to `True` if the server should consider the file a potential source of metadata extraction, marco@16: or `False` if the server should not attempt to extract any metadata from the deposi marco@16: marco@16: Metadata information requires: marco@16: marco@16: `metadata_entry` - An instance of `sword2.Entry`, set with the metadata required. marco@16: marco@16: for example, to create a metadata entry marco@16: >>> from sword2 import Entry marco@16: >>> entry = Entry(title = "My new deposit", marco@16: ... id = "new:id", # atom:id marco@16: ... dcterms_abstract = "My Thesis", marco@16: ... dcterms_author = "Ben", marco@16: ... dcterms_issued = "2010") marco@16: marco@16: Response: marco@16: marco@16: A `sword2.Deposit_Receipt` object containing the deposit receipt data. If the response was blank or marco@16: not a Deposit Response, then only a few attributes will be populated: marco@16: marco@16: `code` -- HTTP code of the response marco@16: `response_headers` -- `dict` of the reponse headers marco@16: `content` -- (Optional) in case the response body is not empty but the response is not a Deposit Receipt marco@16: marco@16: If exception-throwing is turned off (`error_response_raises_exceptions = False` or `self.raise_except = False`) marco@16: then the response will be a `sword2.Error_Document`, but will still have the aforementioned attributes set, (code, marco@16: response_headers, etc) marco@16: """ marco@16: target_iri = None marco@16: request_type = "Update PUT" marco@16: if metadata_entry != None: marco@16: # Metadata or Metadata + file --> Edit-IRI marco@16: conn_l.info("Using the Edit-IRI - Metadata or Metadata + file multipart-related uses a PUT request to the Edit-IRI") marco@16: if payload != None and filename != None: marco@16: request_type = "Update Multipart PUT" marco@16: else: marco@16: request_type = "Update Metadata PUT" marco@16: if dr != None and dr.edit != None: marco@16: conn_l.info("Using the deposit receipt to get the Edit-IRI: %s" % dr.edit) marco@16: target_iri = dr.edit marco@16: elif edit_iri != None: marco@16: conn_l.info("Using the %s receipt as the Edit-IRI" % edit_iri) marco@16: target_iri = edit_iri marco@16: else: marco@16: conn_l.error("Metadata or Metadata + file multipart-related update: Cannot find the Edit-IRI from the parameters supplied.") marco@16: elif payload != None and filename != None: marco@16: # File-only --> Edit-Media-IRI marco@16: conn_l.info("Using the Edit-Media-IRI - File update uses a PUT request to the Edit-Media-IRI") marco@16: request_type = "Update File PUT" marco@16: if dr != None and dr.edit_media != None: marco@16: conn_l.info("Using the deposit receipt to get the Edit-Media-IRI: %s" % dr.edit_media) marco@16: target_iri = dr.edit_media marco@16: elif edit_media_iri != None: marco@16: conn_l.info("Using the %s receipt as the Edit-Media-IRI" % edit_media_iri) marco@16: target_iri = edit_media_iri marco@16: else: marco@16: conn_l.error("File update: Cannot find the Edit-Media-IRI from the parameters supplied.") marco@16: marco@16: if target_iri == None: marco@16: raise Exception("No suitable IRI was found for the request needed.") marco@16: marco@16: return self._make_request(target_iri = target_iri, marco@16: metadata_entry=metadata_entry, marco@16: payload=payload, marco@16: mimetype=mimetype, marco@16: filename=filename, marco@16: packaging=packaging, marco@16: on_behalf_of=on_behalf_of, marco@16: in_progress=in_progress, marco@16: metadata_relevant=str(metadata_relevant), marco@16: method="PUT", marco@16: request_type=request_type) marco@16: marco@16: marco@16: marco@16: def add_file_to_resource(self, marco@16: edit_media_iri, marco@16: payload, # These need to be set to upload a file marco@16: filename, # According to spec, "The client MUST supply a Content-Disposition header with a filename parameter marco@16: # (note that this requires the filename be expressed in ASCII)." marco@16: mimetype=None, marco@16: marco@16: marco@16: on_behalf_of=None, marco@16: in_progress=False, marco@16: metadata_relevant=False, marco@16: packaging=None marco@16: ): marco@16: """ marco@16: Adding Files to the Media Resource marco@16: marco@16: From the spec, paraphrased: marco@16: marco@16: "This feature is for use when clients wish to send individual files to the server and to receive back the IRI for the created resource. [Adding new items to the deposit container] will not give back the location of the deposited resources, so in cases where the server does not provide the (optional) Deposit Receipt, it is not possible for the client to ascertain the location of the file actually deposited - the Location header in that operation is the Edit-IRI. By POSTing to the EM-IRI, the Location header will return the IRI of the deposited file itself, rather than that of the container. marco@16: marco@16: As the EM-IRI represents the Media Resource itself, rather than the Container, this operation will not formally support metadata handling, and therefore also offers no explicit support for packaging either since packages may be both content and metadata. Nonetheless, for files which may contain extractable metadata, there is a Metadata-Relevant header which can be defined to indicate whether the deposit can be used to augment the metadata of the container." marco@16: marco@16: #BETASWORD2URL marco@16: See http://sword-app.svn.sourceforge.net/viewvc/sword-app/spec/trunk/SWORDProfile.html?revision=HEAD#protocoloperations_addingcontent_mediaresource marco@16: marco@16: marco@16: Set the following parameters in addition to the basic parameters: marco@16: marco@16: `edit_media_iri` - The Edit-Media-IRI marco@16: marco@16: `payload` - the payload to send. Can be either a bytestring or a File-like object that supports `payload.read()` marco@16: `mimetype` - MIMEType of the payload marco@16: `filename` - filename. Most SWORD2 uploads have this as being mandatory. marco@16: `packaging` - the SWORD2 packaging type of the payload. marco@16: eg packaging = 'http://purl.org/net/sword/package/Binary' marco@16: marco@16: Response: marco@16: marco@16: A `sword2.Deposit_Receipt` object containing the deposit receipt data. If the response was blank or marco@16: not a Deposit Response, then only a few attributes will be populated: marco@16: marco@16: `code` -- HTTP code of the response marco@16: `response_headers` -- `dict` of the reponse headers marco@16: `content` -- (Optional) in case the response body is not empty but the response is not a Deposit Receipt marco@16: marco@16: If exception-throwing is turned off (`error_response_raises_exceptions = False` or `self.raise_except = False`) marco@16: then the response will be a `sword2.Error_Document`, but will still have the aforementioned attributes set, (code, marco@16: response_headers, etc) marco@16: marco@16: """ marco@16: conn_l.info("Appending file to a deposit via Edit-Media-IRI %s" % edit_media_iri) marco@16: return self._make_request(target_iri = edit_media_iri, marco@16: payload=payload, marco@16: mimetype=mimetype, marco@16: filename=filename, marco@16: on_behalf_of=on_behalf_of, marco@16: in_progress=in_progress, marco@16: method="POST", marco@16: metadata_relevant=metadata_relevant, marco@16: request_type='EM_IRI POST (APPEND)', marco@16: packaging=packaging) marco@16: marco@16: def append(self, marco@16: se_iri = None, marco@16: marco@16: payload = None, # These need to be set to upload a file marco@16: filename = None, # According to spec, "The client MUST supply a Content-Disposition header with a filename parameter marco@16: # (note that this requires the filename be expressed in ASCII)." marco@16: mimetype = None, marco@16: packaging = None, marco@16: on_behalf_of = None, marco@16: metadata_entry = None, marco@16: metadata_relevant = False, marco@16: in_progress = False, marco@16: dr = None marco@16: ): marco@16: """ marco@16: Adding Content to a Resource marco@16: marco@16: #BETASWORD2URL marco@16: See http://sword-app.svn.sourceforge.net/viewvc/sword-app/spec/trunk/SWORDProfile.html?revision=HEAD#protocoloperations_addingcontent marco@16: marco@16: Usage: marco@16: ------ marco@16: marco@16: Set the target for this request: marco@16: -------------------------------- marco@16: marco@16: Set `se_iri` to be the SWORD2-Edit-IRI for a given deposit. (This can be found in `sword2.Deposit_Receipt.se_iri`) marco@16: marco@16: marco@16: OR marco@16: marco@16: you can pass back the `sword2.Deposit_Receipt` object you got from a previous transaction as the `dr` parameter, marco@16: and the correct IRI will automatically be chosen. marco@16: marco@16: Then: marco@16: ----- marco@16: marco@16: 1. "Adding New Packages or Files to a Container" marco@16: ------------------------------------------------ marco@16: marco@16: Set the following parameters in addition to the basic parameters: marco@16: marco@16: `payload` - the payload to send. Can be either a bytestring or a File-like object that supports `payload.read()` marco@16: `mimetype` - MIMEType of the payload marco@16: `filename` - filename. Most SWORD2 uploads have this as being mandatory. marco@16: `packaging` - the SWORD2 packaging type of the payload. marco@16: eg packaging = 'http://purl.org/net/sword/package/Binary' marco@16: marco@16: Response: marco@16: marco@16: A `sword2.Deposit_Receipt` object containing the deposit receipt data. If the response was blank or marco@16: not a Deposit Response, then only a few attributes will be populated: marco@16: marco@16: `code` -- HTTP code of the response marco@16: `response_headers` -- `dict` of the reponse headers marco@16: `content` -- (Optional) in case the response body is not empty but the response is not a Deposit Receipt marco@16: marco@16: If exception-throwing is turned off (`error_response_raises_exceptions = False` or `self.raise_except = False`) marco@16: then the response will be a `sword2.Error_Document`, but will still have the aforementioned attributes set, (code, marco@16: response_headers, etc) marco@16: marco@16: marco@16: 2. "Adding New Metadata to a Container" marco@16: --------------------------------------- marco@16: marco@16: NB SWORD2 does not instruct the server on the best way to handle metadata, only that metadata SHOULD be marco@16: added and not overwritten; in certain circumstances this may not produce the desired behaviour. marco@16: marco@16: Set the following parameters in addition to the basic parameters: marco@16: marco@16: `metadata_entry` - An instance of `sword2.Entry`, set with the metadata required. marco@16: marco@16: for example: marco@16: # conn = `sword2.Connection`, se_iri = SWORD2-Edit-IRI marco@16: >>> from sword2 import Entry marco@16: >>> entry = Entry(dcterms:identifier = "doi://......") marco@16: >>> conn.add_new_item_to_container(se_iri = se_iri, marco@16: ... metadata_entry = entry) marco@16: marco@16: Response: marco@16: marco@16: A `sword2.Deposit_Receipt` object containing the deposit receipt data. If the response was blank or marco@16: not a Deposit Response, then only a few attributes will be populated: marco@16: marco@16: `code` -- HTTP code of the response marco@16: `response_headers` -- `dict` of the reponse headers marco@16: `content` -- (Optional) in case the response body is not empty but the response is not a Deposit Receipt marco@16: marco@16: If exception-throwing is turned off (`error_response_raises_exceptions = False` or `self.raise_except = False`) marco@16: then the response will be a `sword2.Error_Document`, but will still have the aforementioned attributes set, (code, marco@16: response_headers, etc) marco@16: marco@16: 3. "Adding New Metadata and Packages or Files to a Container with Multipart" marco@16: ---------------------------------------------------------------------------- marco@16: marco@16: Create a resource in a given collection by uploading a file AND the metadata about this resource. marco@16: marco@16: To make this sort of request, just set the parameters as shown for both the binary upload and the metadata upload. marco@16: marco@16: eg: marco@16: marco@16: >>> conn.add_new_item_to_container(se_iri = se_iri, marco@16: ... metadata_entry = entry, marco@16: ... payload = open("foo.zip", "r"), marco@16: ... mimetype = marco@16: .... and so on marco@16: marco@16: Response: marco@16: marco@16: A `sword2.Deposit_Receipt` object containing the deposit receipt data. If the response was blank or marco@16: not a Deposit Response, then only a few attributes will be populated: marco@16: marco@16: `code` -- HTTP code of the response marco@16: `response_headers` -- `dict` of the reponse headers marco@16: `content` -- (Optional) in case the response body is not empty but the response is not a Deposit Receipt marco@16: marco@16: If exception-throwing is turned off (`error_response_raises_exceptions = False` or `self.raise_except = False`) marco@16: then the response will be a `sword2.Error_Document`, but will still have the aforementioned attributes set, (code, marco@16: response_headers, etc) marco@16: marco@16: """ marco@16: marco@16: if not se_iri: marco@16: if dr != None: marco@16: conn_l.info("Using the deposit receipt to get the SWORD2-Edit-IRI") marco@16: se_iri = dr.se_iri marco@16: if se_iri: marco@16: conn_l.info("Update Resource via SWORD2-Edit-IRI %s" % se_iri) marco@16: else: marco@16: raise Exception("No SWORD2-Edit-IRI was given and no suitable IRI was found in the deposit receipt.") marco@16: else: marco@16: raise Exception("No SWORD2-Edit-IRI was given") marco@16: else: marco@16: conn_l.info("Update Resource via SWORD2-Edit-IRI %s" % se_iri) marco@16: marco@16: conn_l.info("Adding new file, metadata or both to a SWORD deposit via SWORD-Edit-IRI %s" % se_iri) marco@16: return self._make_request(target_iri = se_iri, marco@16: payload=payload, marco@16: mimetype=mimetype, marco@16: packaging=packaging, marco@16: filename=filename, marco@16: metadata_entry=metadata_entry, marco@16: on_behalf_of=on_behalf_of, marco@16: in_progress=in_progress, marco@16: method="POST", marco@16: metadata_relevant=metadata_relevant, marco@16: request_type='SE_IRI POST (APPEND PKG)') marco@16: marco@16: marco@16: def delete(self, marco@16: resource_iri, marco@16: on_behalf_of=None): marco@16: """ marco@16: Delete resource marco@16: marco@16: Generic method to send an HTTP DELETE request to a given IRI. marco@16: marco@16: Can be given the optional parameter of `on_behalf_of`. marco@16: """ marco@16: conn_l.info("Deleting resource %s" % resource_iri) marco@16: return self._make_request(target_iri = resource_iri, marco@16: on_behalf_of=on_behalf_of, marco@16: method="DELETE", marco@16: request_type='IRI DELETE') marco@16: marco@16: def delete_content_of_resource(self, edit_media_iri = None, marco@16: on_behalf_of = None, marco@16: dr = None): marco@16: marco@16: """ marco@16: Deleting the Content of a Resource marco@16: marco@16: Remove all the content of a resource without removing the resource itself marco@16: marco@16: #BETASWORD2URL marco@16: See http://sword-app.svn.sourceforge.net/viewvc/sword-app/spec/trunk/SWORDProfile.html?revision=HEAD#protocoloperations_deletingcontent marco@16: marco@16: Usage: marco@16: ------ marco@16: marco@16: Set the target for this request: marco@16: -------------------------------- marco@16: marco@16: Set `edit_media_iri` to be the Edit-Media-IRI for a given resource. marco@16: marco@16: marco@16: OR marco@16: marco@16: you can pass back the `sword2.Deposit_Receipt` object you got from a previous transaction as the `dr` parameter, marco@16: and the correct IRI will automatically be chosen. marco@16: """ marco@16: if not edit_media_iri: marco@16: if dr != None: marco@16: conn_l.info("Using the deposit receipt to get the Edit-Media-IRI") marco@16: edit_media_iri = dr.edit_media marco@16: if edit_media_iri: marco@16: conn_l.info("Deleting Resource via Edit-Media-IRI %s" % edit_media_iri) marco@16: else: marco@16: raise Exception("No Edit-Media-IRI was given and no suitable IRI was found in the deposit receipt.") marco@16: else: marco@16: raise Exception("No Edit-Media-IRI was given") marco@16: else: marco@16: conn_l.info("Deleting Resource via Edit-Media-IRI %s" % edit_media_iri) marco@16: marco@16: return self.delete_resource(edit_media_iri, marco@16: on_behalf_of = on_behalf_of) marco@16: marco@16: marco@16: marco@16: marco@16: marco@16: def delete_container(self, edit_iri = None, marco@16: on_behalf_of = None, marco@16: dr = None): marco@16: marco@16: """ marco@16: Deleting the Container marco@16: marco@16: Delete the entire object on the server, effectively removing the deposit entirely. marco@16: marco@16: #BETASWORD2URL marco@16: See http://sword-app.svn.sourceforge.net/viewvc/sword-app/spec/trunk/SWORDProfile.html?revision=HEAD#protocoloperations_deleteconteiner marco@16: marco@16: Usage: marco@16: ------ marco@16: marco@16: Set the target for this request: marco@16: -------------------------------- marco@16: marco@16: Set `edit_iri` to be the Edit-IRI for a given resource. marco@16: marco@16: marco@16: OR marco@16: marco@16: you can pass back the `sword2.Deposit_Receipt` object you got from a previous transaction as the `dr` parameter, marco@16: and the correct IRI will automatically be chosen. marco@16: marco@16: """ marco@16: if not edit_iri: marco@16: if dr != None: marco@16: conn_l.info("Using the deposit receipt to get the Edit-IRI") marco@16: edit_iri = dr.edit marco@16: if edit_iri: marco@16: conn_l.info("Deleting Container via Edit-IRI %s" % edit_iri) marco@16: else: marco@16: raise Exception("No Edit-IRI was given and no suitable IRI was found in the deposit receipt.") marco@16: else: marco@16: raise Exception("No Edit-IRI was given") marco@16: else: marco@16: conn_l.info("Deleting Container via Edit-IRI %s" % edit_iri) marco@16: marco@16: return self.delete_resource(edit_iri, marco@16: on_behalf_of = on_behalf_of) marco@16: marco@16: def complete_deposit(self, marco@16: se_iri = None, marco@16: on_behalf_of=None, marco@16: dr = None): marco@16: """ marco@16: Completing a Previously Incomplete Deposit marco@16: marco@16: Use this method to indicate to a server that a deposit which was 'in progress' is now complete. In other words, complete a deposit marco@16: which had the 'In-Progress' flag set to True. marco@16: marco@16: #BETASWORD2URL marco@16: http://sword-app.svn.sourceforge.net/viewvc/sword-app/spec/trunk/SWORDProfile.html?revision=HEAD#continueddeposit_complete marco@16: marco@16: Usage: marco@16: ------ marco@16: marco@16: Set the target for this request: marco@16: -------------------------------- marco@16: marco@16: Set `se_iri` to be the SWORD2-Edit-IRI for a given resource. marco@16: marco@16: marco@16: OR marco@16: marco@16: you can pass back the `sword2.Deposit_Receipt` object you got from a previous transaction as the `dr` parameter, marco@16: and the correct IRI will automatically be chosen. marco@16: """ marco@16: marco@16: if not se_iri: marco@16: if dr != None: marco@16: conn_l.info("Using the deposit receipt to get the SWORD2-Edit-IRI") marco@16: se_iri = dr.se_iri marco@16: if se_iri: marco@16: conn_l.info("Complete deposit using the SWORD2-Edit-IRI %s" % se_iri) marco@16: else: marco@16: raise Exception("No SWORD2-Edit-IRI was given and no suitable IRI was found in the deposit receipt.") marco@16: else: marco@16: raise Exception("No SWORD2-Edit-IRI was given") marco@16: else: marco@16: conn_l.info("Complete deposit using the SWORD2-Edit-IRI %s" % se_iri) marco@16: marco@16: return self._make_request(target_iri = se_iri, marco@16: on_behalf_of=on_behalf_of, marco@16: in_progress='false', marco@16: method="POST", marco@16: empty=True, marco@16: request_type='SE_IRI Complete Deposit') marco@16: marco@16: def update_files_for_resource(self, marco@16: payload, # These need to be set to upload a file marco@16: filename, # According to spec, "The client MUST supply a Content-Disposition header with a filename parameter marco@16: # (note that this requires the filename be expressed in ASCII)." marco@16: mimetype=None, marco@16: packaging=None, marco@16: marco@16: edit_media_iri = None, marco@16: marco@16: on_behalf_of=None, marco@16: in_progress=False, marco@16: metadata_relevant=False, marco@16: # Pass back the deposit receipt to automatically get the right IRI to use marco@16: dr = None marco@16: ): marco@16: """ marco@16: Replacing the File Content of a Resource marco@16: marco@16: #BETASWORD2URL marco@16: See http://sword-app.svn.sourceforge.net/viewvc/sword-app/spec/trunk/SWORDProfile.html?revision=HEAD#protocoloperations_editingcontent_binary marco@16: marco@16: The `Connection` can replace the file content of a resource, given the Edit-Media-IRI for this resource. This can be found marco@16: from the `sword2.Deposit_Receipt.edit_media` attribute of a previous deposit, or directly from the deposit receipt XML response. marco@16: marco@16: Usage: marco@16: ------ marco@16: marco@16: Set the target for this request: marco@16: -------------------------------- marco@16: marco@16: Set the `edit_media_iri` parameter to the Edit-Media-IRI. marco@16: marco@16: OR marco@16: marco@16: you can pass back the `sword2.Deposit_Receipt` object you got from a previous transaction as the `dr` parameter, marco@16: and the correct IRI will automatically be chosen. marco@16: marco@16: Then, add in the payload: marco@16: ------------------------- marco@16: marco@16: Set the following parameters in addition to the basic parameters (see `self.create_resource`): marco@16: marco@16: `payload` - the payload to send. Can be either a bytestring or a File-like object that supports `payload.read()` marco@16: `mimetype` - MIMEType of the payload marco@16: `filename` - filename. Most SWORD2 uploads have this as being mandatory. marco@16: `packaging` - the SWORD2 packaging type of the payload. marco@16: eg packaging = 'http://purl.org/net/sword/package/Binary' marco@16: marco@16: `metadata_relevant` - This should be set to `True` if the server should consider the file a potential source of metadata extraction, marco@16: or `False` if the server should not attempt to extract any metadata from the deposi marco@16: marco@16: Response: marco@16: marco@16: A `sword2.Deposit_Receipt` object containing the deposit receipt data. If the response was blank or marco@16: not a Deposit Response, then only a few attributes will be populated: marco@16: marco@16: `code` -- HTTP code of the response marco@16: `response_headers` -- `dict` of the reponse headers marco@16: `content` -- (Optional) in case the response body is not empty but the response is not a Deposit Receipt marco@16: marco@16: If exception-throwing is turned off (`error_response_raises_exceptions = False` or `self.raise_except = False`) marco@16: then the response will be a `sword2.Error_Document`, but will still have the aforementioned attributes set, (code, marco@16: response_headers, etc) marco@16: """ marco@16: if not edit_media_iri: marco@16: if dr != None: marco@16: conn_l.info("Using the deposit receipt to get the Edit-Media-IRI") marco@16: edit_media_iri = dr.edit_media marco@16: if edit_media_iri: marco@16: conn_l.info("Update Resource via Edit-Media-IRI %s" % edit_media_iri) marco@16: else: marco@16: raise Exception("No Edit-Media-IRI was given and no suitable IRI was found in the deposit receipt.") marco@16: else: marco@16: raise Exception("No Edit-Media-IRI was given") marco@16: else: marco@16: conn_l.info("Update Resource via Edit-Media-IRI %s" % edit_media_iri) marco@16: marco@16: return self._make_request(target_iri = edit_media_iri, marco@16: payload=payload, marco@16: mimetype=mimetype, marco@16: filename=filename, marco@16: in_progress=in_progress, marco@16: packaging=packaging, marco@16: on_behalf_of=on_behalf_of, marco@16: method="PUT", marco@16: metadata_relevant=str(metadata_relevant), marco@16: request_type='EM_IRI PUT') marco@16: marco@16: def update_metadata_for_resource(self, metadata_entry, # required marco@16: edit_iri = None, marco@16: in_progress=False, marco@16: on_behalf_of=None, marco@16: dr = None marco@16: ): marco@16: """ marco@16: Replacing the Metadata of a Resource marco@16: marco@16: #BETASWORD2URL marco@16: See http://sword-app.svn.sourceforge.net/viewvc/sword-app/spec/trunk/SWORDProfile.html?revision=HEAD#protocoloperations_editingcontent_metadata marco@16: marco@16: Replace the metadata of a resource as identified by its Edit-IRI. marco@16: marco@16: Note, from the specification: "The client can only be sure that the server will support this process when using the default format supported by SWORD: Qualified Dublin Core XML embedded directly in the atom:entry. Other metadata formats MAY be supported by a particular server, but this is not covered by the SWORD profile" marco@16: marco@16: Usage: marco@16: ------ marco@16: marco@16: Set the target for this request: marco@16: -------------------------------- marco@16: marco@16: Set the `edit_iri` parameter to the Edit-IRI. marco@16: marco@16: OR marco@16: marco@16: you can pass back the `sword2.Deposit_Receipt` object you got from a previous transaction as the `dr` parameter, marco@16: and the correct IRI will automatically be chosen. marco@16: marco@16: Then, add in the metadata: marco@16: -------------------------- marco@16: marco@16: Set the following in addition to the basic parameters: marco@16: marco@16: `metadata_entry` - An instance of `sword2.Entry`, set with the metadata required. marco@16: marco@16: for example, to replace the metadata for a given: marco@16: # conn = `sword2.Connection`, edit_iri = Edit-IRI marco@16: marco@16: >>> from sword2 import Entry marco@16: >>> entry = Entry(title = "My new deposit", marco@16: ... id = "new:id", # atom:id marco@16: ... dcterms_abstract = "My Thesis", marco@16: ... dcterms_author = "Ben", marco@16: ... dcterms_issued = "2010") marco@16: marco@16: >>> conn.update_metadata_for_resource(edit_iri = edit_iri, marco@16: ... metadata_entry = entry) marco@16: marco@16: marco@16: Response: marco@16: marco@16: A `sword2.Deposit_Receipt` object containing the deposit receipt data. If the response was blank or marco@16: not a Deposit Response, then only a few attributes will be populated: marco@16: marco@16: `code` -- HTTP code of the response marco@16: `response_headers` -- `dict` of the reponse headers marco@16: `content` -- (Optional) in case the response body is not empty but the response is not a Deposit Receipt marco@16: marco@16: If exception-throwing is turned off (`error_response_raises_exceptions = False` or `self.raise_except = False`) marco@16: then the response will be a `sword2.Error_Document`, but will still have the aforementioned attributes set, (code, marco@16: response_headers, etc) marco@16: """ marco@16: if not edit_iri: marco@16: if dr != None: marco@16: conn_l.info("Using the deposit receipt to get the Edit-IRI") marco@16: edit_iri = dr.edit marco@16: if edit_iri: marco@16: conn_l.info("Update Resource via Edit-IRI %s" % edit_iri) marco@16: else: marco@16: raise Exception("No Edit-IRI was given and no suitable IRI was found in the deposit receipt.") marco@16: else: marco@16: raise Exception("No Edit-IRI was given") marco@16: else: marco@16: conn_l.info("Update Resource via Edit-IRI %s" % edit_iri) marco@16: marco@16: return self._make_request(target_iri = edit_iri, marco@16: metadata_entry=metadata_entry, marco@16: on_behalf_of=on_behalf_of, marco@16: in_progress=in_progress, marco@16: method="PUT", marco@16: request_type='Edit_IRI PUT') marco@16: marco@16: def update_metadata_and_files_for_resource(self, metadata_entry, # required marco@16: payload, # These need to be set to upload a file marco@16: filename, # According to spec, "The client MUST supply a Content-Disposition header with a filename parameter marco@16: # (note that this requires the filename be expressed in ASCII)." marco@16: mimetype=None, marco@16: packaging=None, marco@16: marco@16: edit_iri = None, marco@16: marco@16: metadata_relevant=False, marco@16: in_progress=False, marco@16: on_behalf_of=None, marco@16: dr = None marco@16: ): marco@16: """ marco@16: Replacing the Metadata and Files of a Resource marco@16: marco@16: #BETASWORD2URL marco@16: See http://sword-app.svn.sourceforge.net/viewvc/sword-app/spec/trunk/SWORDProfile.html?revision=HEAD#protocoloperations_editingcontent_multipart marco@16: marco@16: Replace the metadata and files of a resource as identified by its Edit-IRI. marco@16: marco@16: Usage: marco@16: ------ marco@16: marco@16: Set the target for this request: marco@16: -------------------------------- marco@16: marco@16: Set the `edit_iri` parameter to the Edit-IRI. marco@16: marco@16: OR marco@16: marco@16: you can pass back the `sword2.Deposit_Receipt` object you got from a previous transaction as the `dr` parameter, marco@16: and the correct IRI will automatically be chosen. marco@16: marco@16: Then, add in the file and metadata information: marco@16: ----------------------------------------------- marco@16: Set the following in addition to the basic parameters: marco@16: marco@16: File information: marco@16: marco@16: `payload` - the payload to send. Can be either a bytestring or a File-like object that supports `payload.read()` marco@16: `mimetype` - MIMEType of the payload marco@16: `filename` - filename. Most SWORD2 uploads have this as being mandatory. marco@16: `packaging` - the SWORD2 packaging type of the payload. marco@16: eg packaging = 'http://purl.org/net/sword/package/Binary' marco@16: marco@16: `metadata_relevant` - This should be set to `True` if the server should consider the file a potential source of metadata extraction, marco@16: or `False` if the server should not attempt to extract any metadata from the deposi marco@16: marco@16: Metadata information: marco@16: marco@16: `metadata_entry` - An instance of `sword2.Entry`, set with the metadata required. marco@16: marco@16: for example, to create a metadata entry marco@16: >>> from sword2 import Entry marco@16: >>> entry = Entry(title = "My new deposit", marco@16: ... id = "new:id", # atom:id marco@16: ... dcterms_abstract = "My Thesis", marco@16: ... dcterms_author = "Ben", marco@16: ... dcterms_issued = "2010") marco@16: marco@16: Response: marco@16: marco@16: A `sword2.Deposit_Receipt` object containing the deposit receipt data. If the response was blank or marco@16: not a Deposit Response, then only a few attributes will be populated: marco@16: marco@16: `code` -- HTTP code of the response marco@16: `response_headers` -- `dict` of the reponse headers marco@16: `content` -- (Optional) in case the response body is not empty but the response is not a Deposit Receipt marco@16: marco@16: If exception-throwing is turned off (`error_response_raises_exceptions = False` or `self.raise_except = False`) marco@16: then the response will be a `sword2.Error_Document`, but will still have the aforementioned attributes set, (code, marco@16: response_headers, etc) marco@16: """ marco@16: if not edit_iri: marco@16: if dr != None: marco@16: conn_l.info("Using the deposit receipt to get the Edit-IRI") marco@16: edit_iri = dr.edit marco@16: if edit_iri: marco@16: conn_l.info("Update Resource via Edit-IRI %s" % edit_iri) marco@16: else: marco@16: raise Exception("No Edit-IRI was given and no suitable IRI was found in the deposit receipt.") marco@16: else: marco@16: raise Exception("No Edit-IRI was given") marco@16: else: marco@16: conn_l.info("Update Resource via Edit-IRI %s" % edit_iri) marco@16: marco@16: return self._make_request(target_iri = edit_iri, marco@16: metadata_entry=metadata_entry, marco@16: payload=payload, marco@16: mimetype=mimetype, marco@16: filename=filename, marco@16: packaging=packaging, marco@16: on_behalf_of=on_behalf_of, marco@16: in_progress=in_progress, marco@16: metadata_relevant=str(metadata_relevant), marco@16: method="PUT", marco@16: request_type='Edit_IRI PUT') marco@16: marco@16: marco@16: def get_atom_sword_statement(self, sword_statement_iri): marco@16: """ marco@16: Getting the Sword Statement. marco@16: marco@16: IN PROGRESS - USE AT OWN RISK.... see `sword2.Sword_Statement`. marco@16: """ marco@16: # get the statement first marco@16: conn_l.debug("Trying to GET the ATOM Sword Statement at %s." % sword_statement_iri) marco@16: response = self.get_resource(sword_statement_iri, headers = {'Accept':'application/atom+xml;type=feed'}) marco@16: if response.code == 200: marco@16: #try: marco@16: if True: marco@16: conn_l.debug("Attempting to parse the response as a ATOM Sword Statement") marco@16: s = Sword_Statement(response.content) marco@16: conn_l.debug("Parsed SWORD2 Statement, returning") marco@16: return s marco@16: #except Exception, e: marco@16: # # Any error here is to do with the parsing marco@16: # return response.content marco@16: marco@16: def get_resource(self, content_iri = None, marco@16: packaging=None, marco@16: on_behalf_of=None, marco@16: headers = {}, marco@16: dr = None): marco@16: """ marco@16: Retrieving the content marco@16: marco@16: Get the file or package from the SWORD2 server. marco@16: marco@16: From the specification: marco@16: "The Deposit Receipt contains two IRIs which can be used to retrieve content from the server: Cont-IRI and EM-IRI. These are provided in the atom:content@src element and the atom:link@rel="edit-media" elements respectively. Their only functional difference is that the client MUST NOT carry out any HTTP operations other than GET on the Cont-IRI, while all operations are permitted on the EM-IRI. It is acceptable, but not required, that both IRIs to be the same, and in this section we refer only to the EM-IRI but in all cases it can be substituted for the Cont-IRI." marco@16: marco@16: #BETASWORD2URL marco@16: See http://sword-app.svn.sourceforge.net/viewvc/sword-app/spec/trunk/SWORDProfile.html?revision=HEAD#protocoloperations_retrievingcontent marco@16: marco@16: Usage: marco@16: ------ marco@16: marco@16: Set the target for this request: marco@16: -------------------------------- marco@16: marco@16: Set `content_iri` to be the Content-IRI for a given resource (or to the IRI of any resource you wish to HTTP GET) marco@16: marco@16: marco@16: OR marco@16: marco@16: you can pass back the `sword2.Deposit_Receipt` object you got from a previous transaction as the `dr` parameter, marco@16: and the correct IRI will automatically be chosen. marco@16: marco@16: Response: marco@16: marco@16: A `ContentWrapper` - marco@16: `ContentWrapper.response_headers` -- response headers marco@16: `ContentWrapper.content` -- body of response from server (the file or package) marco@16: `ContentWrapper.code` -- status code ('200' on success.) marco@16: marco@16: """ marco@16: marco@16: if not content_iri: marco@16: if dr != None: marco@16: conn_l.info("Using the deposit receipt to get the SWORD2-Edit-IRI") marco@16: content_iri = dr.cont_iri marco@16: if content_iri: marco@16: conn_l.info("Getting the resource at Content-IRI %s" % content_iri) marco@16: else: marco@16: raise Exception("No Content-IRI was given and no suitable IRI was found in the deposit receipt.") marco@16: else: marco@16: raise Exception("No Content-IRI was given") marco@16: else: marco@16: conn_l.info("Getting the resource at Content-IRI %s" % content_iri) marco@16: marco@16: # 406 - PackagingFormatNotAvailable marco@16: if self.honour_receipts and packaging: marco@16: # Make sure that the packaging format is available from the deposit receipt, if loaded marco@16: conn_l.debug("Checking that the packaging format '%s' is available." % content_iri) marco@16: conn_l.debug("Cached Cont-IRI Receipts: %s" % self.cont_iris.keys()) marco@16: if content_iri in self.cont_iris.keys(): marco@16: if not (packaging in self.cont_iris[content_iri].packaging): marco@16: conn_l.error("Desired packaging format '%' not available from the server, according to the deposit receipt. Change the client parameter 'honour_receipts' to False to avoid this check.") marco@16: return self._return_error_or_exception(PackagingFormatNotAvailable, {}, "") marco@16: if on_behalf_of: marco@16: headers['On-Behalf-Of'] = self.on_behalf_of marco@16: elif self.on_behalf_of: marco@16: headers['On-Behalf-Of'] = self.on_behalf_of marco@16: if packaging: marco@16: headers['Accept-Packaging'] = packaging marco@16: marco@16: self._t.start("IRI GET resource") marco@16: if packaging: marco@16: conn_l.info("IRI GET resource '%s' with Accept-Packaging:%s" % (content_iri, packaging)) marco@16: else: marco@16: conn_l.info("IRI GET resource '%s'" % content_iri) marco@16: resp, content = self.h.request(content_iri, "GET", headers=headers) marco@16: _, took_time = self._t.time_since_start("IRI GET resource") marco@16: if self.history: marco@16: self.history.log('Cont_IRI GET resource', marco@16: sd_iri = self.sd_iri, marco@16: content_iri = content_iri, marco@16: packaging = packaging, marco@16: on_behalf_of = self.on_behalf_of, marco@16: response = resp, marco@16: headers = headers, marco@16: process_duration = took_time) marco@16: conn_l.info("Server response: %s" % resp['status']) marco@16: conn_l.debug(resp) marco@16: if resp['status'] == '200': marco@16: conn_l.debug("Cont_IRI GET resource successful - got %s bytes from %s" % (len(content), content_iri)) marco@16: class ContentWrapper(object): marco@16: def __init__(self, resp, content): marco@16: self.response_headers = dict(resp) marco@16: self.content = content marco@16: self.code = resp.status marco@16: return ContentWrapper(resp, content) marco@16: elif resp['status'] == '408': # Unavailable packaging format marco@16: conn_l.error("Desired packaging format '%' not available from the server.") marco@16: return self._return_error_or_exception(PackagingFormatNotAvailable, resp, content) marco@16: else: marco@16: return self._handle_error_response(resp, content) marco@16: