def execute(self, chunk_uploaded=None): ctx = self._target_folder.context file_name = os.path.basename(self._source_path) # 1. create an empty file target_item = self._target_folder.upload(file_name, None) ctx.execute_query() # 2. create upload session item = DriveItemUploadableProperties() item.name = file_name session_result = target_item.create_upload_session(item) ctx.execute_query() # 3. start upload fh = open(self._source_path, 'rb') st = os.stat(self._source_path) f_pos = 0 for piece in read_in_chunks(fh, chunk_size=self._chunk_size): req = RequestOptions(session_result.value.uploadUrl) req.method = HttpMethod.Put req.set_header('Content-Length', str(len(piece))) req.set_header( 'Content-Range', 'bytes {0}-{1}/{2}'.format(f_pos, (f_pos + len(piece) - 1), st.st_size)) req.set_header('Accept', '*/*') req.data = piece resp = ctx.execute_request_direct(req) f_pos += len(piece) if chunk_uploaded is not None: chunk_uploaded(f_pos) return target_item
def build_request(self): request_url = "{0}$batch".format(self.context.service_root_url) request = RequestOptions(request_url) request.method = HttpMethod.Post request.ensure_header('Content-Type', self._content_type) request.data = self._prepare_payload().as_bytes() return request
def build_request(self, query): """ :type query: office365.runtime.queries.client_query.ClientQuery """ self._current_query = query if query.resource_path is not None: request_url = query.service_root_url + str(query.resource_path) else: request_url = query.service_root_url + "/" if isinstance(query, ReadEntityQuery) and not query.query_options.is_empty: request_url += "?" + query.query_options.to_url() request = RequestOptions(request_url) self.ensure_media_type(request) # set method request.method = HttpMethod.Get if isinstance(query, DeleteEntityQuery): request.method = HttpMethod.Post elif isinstance( query, (CreateEntityQuery, UpdateEntityQuery, ServiceOperationQuery)): request.method = HttpMethod.Post if query.parameter_type is not None: request.data = self._normalize_payload(query) return request
def build_request(self): qry = self.context.current_query self.json_format.function_tag_name = None if isinstance(qry, ServiceOperationQuery): self.json_format.function_tag_name = qry.method_name if qry.static: request_url = self.context.service_root_url + '.'.join( [qry.binding_type.entity_type_name, qry.method_url]) else: request_url = '/'.join( [qry.binding_type.resource_url, qry.method_url]) else: request_url = qry.binding_type.resource_url request = RequestOptions(request_url) # set method request.method = HttpMethod.Get if isinstance(qry, DeleteEntityQuery): request.method = HttpMethod.Post elif isinstance( qry, (CreateEntityQuery, UpdateEntityQuery, ServiceOperationQuery)): request.method = HttpMethod.Post if qry.parameter_type is not None: request.data = self._normalize_payload(qry.parameter_type) return request
def build_request(self): url = "{0}/$batch".format(self.context.service_root_url()) request = RequestOptions(url) request.method = HttpMethod.Post request.ensure_header('Content-Type', "application/json") request.ensure_header('Accept', "application/json") request.data = self._prepare_payload() return request
def save_binary(ctx, server_relative_url, content): """Uploads a file""" url = r"{0}web/getfilebyserverrelativeurl('{1}')/\$value".format( ctx.serviceRootUrl, server_relative_url) request = RequestOptions(url) request.method = HttpMethod.Post request.set_header('X-HTTP-Method', 'PUT') request.data = content response = ctx.execute_request_direct(request) return response
def build_request(self, query): """ :type query: office365.runtime.queries.client_query.ClientQuery """ url = "{0}/$batch".format(self.context.service_root_url()) request = RequestOptions(url) request.method = HttpMethod.Post request.ensure_header('Content-Type', "application/json") request.ensure_header('Accept', "application/json") request.data = self._prepare_payload() return request
def build_request(self): url = "{0}$batch".format(self.context.service_root_url()) request = RequestOptions(url) request.method = HttpMethod.Post media_type = "multipart/mixed" content_type = "; ".join([ media_type, "boundary={0}".format(self._current_query.current_boundary) ]) request.ensure_header('Content-Type', content_type) request.data = self._prepare_payload().as_bytes() return request
def build_request(self): qry = self.current_query action_url = qry.build_url() request = RequestOptions(action_url) self.ensure_media_type(request) # set method request.method = HttpMethod.Get if isinstance(qry, DeleteEntityQuery): request.method = HttpMethod.Post elif isinstance(qry, (CreateEntityQuery, UpdateEntityQuery, ServiceOperationQuery)): request.method = HttpMethod.Post if qry.parameter_type is not None: request.data = self._normalize_payload(qry.parameter_type) return request
def save_binary(context, server_relative_url, content): """Uploads a file :type context: office365.sharepoint.client_context.ClientContext :type server_relative_url: str :type content: str """ url = r"web/getFileByServerRelativePath(DecodedUrl='{0}')/\$value".format( server_relative_url) request = RequestOptions(url) request.method = HttpMethod.Post request.set_header('X-HTTP-Method', 'PUT') request.data = content response = context.execute_request_direct(request) return response
def save_binary(ctx, server_relative_url, content): """Uploads a file :type ctx: ClientContext :type server_relative_url: str :type content: str """ url = r"{0}web/getFileByServerRelativeUrl('{1}')/\$value".format( ctx.service_root_url(), server_relative_url) request = RequestOptions(url) request.method = HttpMethod.Post request.set_header('X-HTTP-Method', 'PUT') request.data = content response = ctx.execute_request_direct(request) return response
def build_request(self, query): """ :type query: office365.runtime.queries.client_query.ClientQuery """ self._current_query = query request = RequestOptions(query.url) self.ensure_media_type(request) # set method request.method = HttpMethod.Get if isinstance(query, DeleteEntityQuery): request.method = HttpMethod.Post elif isinstance(query, (CreateEntityQuery, UpdateEntityQuery, ServiceOperationQuery)): request.method = HttpMethod.Post if query.parameter_type is not None: request.data = self._normalize_payload(query.parameter_type) return request
def build_request(self, query): """ Construct a OData v3 Batch request :type query: office365.runtime.queries.client_query.ClientQuery """ url = "{0}/$batch".format(self.context.service_root_url()) request = RequestOptions(url) request.method = HttpMethod.Post media_type = "multipart/mixed" content_type = "; ".join([ media_type, "boundary={0}".format(self.current_query.current_boundary) ]) request.ensure_header('Content-Type', content_type) request.data = self._prepare_payload() return request
def _execute_upload_session(self, resp): item = DriveItemUploadableProperties() item.name = self._file_name self._session_result = self._target_item.create_upload_session(item) self.context.execute_query() fh = open(self._source_path, 'rb') st = os.stat(self._source_path) f_pos = 0 for piece in read_in_chunks(fh, chunk_size=self._chunk_size): req = RequestOptions(self._session_result.value.uploadUrl) req.method = HttpMethod.Put req.set_header('Content-Length', str(len(piece))) req.set_header('Content-Range', 'bytes {0}-{1}/{2}'.format(f_pos, (f_pos + len(piece) - 1), st.st_size)) req.set_header('Accept', '*/*') req.data = piece self.context.execute_request_direct(req) f_pos += len(piece) if callable(self._chunk_uploaded): self._chunk_uploaded(f_pos)
def build_request(self): qry = self._get_next_query() request = RequestOptions(qry.bindingType.resourceUrl) self.json_format.function_tag_name = None if isinstance(qry, ServiceOperationQuery): request.url = '/'.join( [qry.bindingType.resourceUrl, qry.methodUrl]) self.json_format.function_tag_name = qry.methodName # set method request.method = HttpMethod.Get if isinstance(qry, DeleteEntityQuery): request.method = HttpMethod.Post elif isinstance(qry, CreateEntityQuery) \ or isinstance(qry, UpdateEntityQuery) \ or isinstance(qry, ServiceOperationQuery): request.method = HttpMethod.Post if qry.parameterType is not None: request.data = self._normalize_payload(qry.parameterType) return request
def build_request(self): qry = self._get_current_query() request = RequestOptions(qry.bindingType.resourceUrl) if isinstance(qry, ServiceOperationQuery): request.url = '/'.join([qry.bindingType.resourceUrl, qry.methodUrl]) # set json format headers media_type = self.json_format.get_media_type() request.set_headers({'Content-Type': media_type, 'Accept': media_type}) # set method request.method = HttpMethod.Get if isinstance(qry, DeleteEntityQuery): request.method = HttpMethod.Post elif isinstance(qry, CreateEntityQuery) \ or isinstance(qry, UpdateEntityQuery) \ or isinstance(qry, ServiceOperationQuery): request.method = HttpMethod.Post if qry.parameterType is not None: request.data = self._normalize_payload(qry.parameterType) return request
s = json.loads(datadigest.content) FormDigestValue = s['FormDigestValue'] print("FormDigestValue" + FormDigestValue) # Send email options = RequestOptions( "{0}/_api/SP.Utilities.Utility.SendEmail".format(site_url)) options.set_header('Accept', 'application/json') options.set_header('Content-Type', 'application/json;odata=verbose') options.set_header('X-RequestDigest', FormDigestValue) options.method = HttpMethod.Post options.data = { "properties": { "__metadata": { "type": "SP.Utilities.EmailProperties" }, "To": { "results": ["*****@*****.**"] }, "Body": "Body01", "Subject": "SubjectOfEmail01" } } data = request.execute_request_direct(options) se = json.loads(data.content) print(se) else: print(ctx_auth.get_last_error())