コード例 #1
0
 def __init__(self, base_url, auth_context):
     if base_url.endswith("/"):
         base_url = base_url[:len(base_url) - 1]
     super(ClientContext, self).__init__(base_url + "/_api/", auth_context)
     self.__web = None
     self.__site = None
     self.contextWebInformation = None
     self.json_format = JsonLightFormat(ODataMetadataLevel.Verbose)
コード例 #2
0
 def request_form_digest(self):
     """Request Form Digest"""
     request = RequestOptions(self.service_root_url + "contextinfo")
     request.method = HttpMethod.Post
     response = self.execute_request_direct(request)
     json = response.json()
     json_format = JsonLightFormat()
     json_format.function_tag_name = "GetContextWebInformation"
     self.get_pending_request().map_json(json, self._contextWebInformation, json_format)
コード例 #3
0
 def __init__(self, base_url, auth_context):
     if base_url.endswith("/"):
         base_url = base_url[:len(base_url) - 1]
     super(ListDataService,
           self).__init__(base_url + "/_vti_bin/listdata.svc/",
                          auth_context)
     self.json_format = JsonLightFormat(ODataMetadataLevel.Verbose)
コード例 #4
0
class ClientContext(ClientRuntimeContext):
    """SharePoint client context"""
    def __init__(self, base_url, auth_context):
        if base_url.endswith("/"):
            base_url = base_url[:len(base_url) - 1]
        super(ClientContext, self).__init__(base_url + "/_api/", auth_context)
        self.__web = None
        self.__site = None
        self.contextWebInformation = None
        self.json_format = JsonLightFormat(ODataMetadataLevel.Verbose)

    def ensure_form_digest(self, request_options):
        if not self.contextWebInformation:
            self.request_form_digest()
        request_options.set_header('X-RequestDigest',
                                   self.contextWebInformation.formDigestValue)

    def request_form_digest(self):
        """Request Form Digest"""
        request = RequestOptions(self.serviceRootUrl + "contextinfo")
        request.method = HttpMethod.Post
        request.set_headers(self.json_format.build_http_headers())
        response = self.execute_request_direct(request)
        payload = response.json()
        if self.json_format.metadata == ODataMetadataLevel.Verbose:
            payload = payload['d']['GetContextWebInformation']
        self.contextWebInformation = ContextWebInformation()
        self.contextWebInformation.from_json(payload)

    def execute_query(self):
        self.pending_request.before_execute_request(self._build_specific_query)
        super(ClientContext, self).execute_query()

    def _build_specific_query(self, request, query):
        if request.method == HttpMethod.Post:
            self.ensure_form_digest(request)
        # set custom SharePoint control headers
        if isinstance(self.json_format, JsonLightFormat):
            if isinstance(query, DeleteEntityQuery):
                request.set_header("X-HTTP-Method", "DELETE")
                request.set_header("IF-MATCH", '*')
            elif isinstance(query, UpdateEntityQuery):
                request.set_header("X-HTTP-Method", "MERGE")
                request.set_header("IF-MATCH", '*')

    @property
    def web(self):
        """Get Web client object"""
        if not self.__web:
            self.__web = Web(self)
        return self.__web

    @property
    def site(self):
        """Get Site client object"""
        if not self.__site:
            self.__site = Site(self)
        return self.__site
コード例 #5
0
    def execute_batch(self):
        """Construct and submit a batch request"""
        batch_request = ODataBatchRequest(self, JsonLightFormat(ODataMetadataLevel.Verbose))

        def _prepare_batch_request(request):
            self.ensure_form_digest(request)

        batch_request.beforeExecute += _prepare_batch_request
        batch_request.execute_query()
コード例 #6
0
 def __init__(self, base_url, auth_context):
     if base_url.endswith("/"):
         base_url = base_url[:len(base_url) - 1]
     super(ClientContext, self).__init__(base_url + "/_api/", auth_context)
     self.__web = None
     self.__site = None
     self.__base_url = base_url
     self._contextWebInformation = None
     self._pendingRequest = ODataRequest(self, JsonLightFormat(ODataMetadataLevel.Verbose))
     self._pendingRequest.beforeExecute += self._build_specific_query
     self._accessToken = None
コード例 #7
0
 def __init__(self, base_url, auth_context=None):
     """
     :type base_url: str
     :type auth_context: AuthenticationContext or None
     """
     if base_url.endswith("/"):
         base_url = base_url[:len(base_url) - 1]
     super(ClientContext, self).__init__(base_url + "/_api/", auth_context)
     self.__web = None
     self.__site = None
     self._base_url = base_url
     self._contextWebInformation = None
     self._pendingRequest = ODataRequest(self, JsonLightFormat(ODataMetadataLevel.Verbose))
     self._pendingRequest.beforeExecute += self._build_modification_query
コード例 #8
0
    def test5_construct_batch_request(self):
        client = ClientContext(
            settings['url']).with_credentials(user_credentials)
        current_user = client.web.currentUser
        client.load(current_user)
        current_web = client.web
        client.load(current_web)

        batch_request = ODataBatchRequest(
            client, JsonLightFormat(ODataMetadataLevel.Verbose))

        def _prepare_request(request):
            client.ensure_form_digest(request)

        batch_request.beforeExecute += _prepare_request
        batch_request.execute_query()
コード例 #9
0
class ClientContext(ClientRuntimeContext):
    """SharePoint client context"""
    def __init__(self, url, auth_context):
        if url.endswith("/"):
            base_url = url[:len(url) - 1]
        else:
            base_url = url
        super(ClientContext, self).__init__(base_url + "/_api/", auth_context)
        self.__web = None
        self.__site = None
        self.contextWebInformation = None
        self.json_format = JsonLightFormat(ODataMetadataLevel.Verbose)

    def ensure_form_digest(self, request_options):
        if not self.contextWebInformation:
            self.request_form_digest()
        request_options.set_header(
            'X-RequestDigest', self.contextWebInformation.form_digest_value)

    def request_form_digest(self):
        """Request Form Digest"""
        request = RequestOptions(self.service_root_url + "contextinfo")
        self.authenticate_request(request)
        request.set_headers(self.json_format.build_http_headers())
        response = requests.post(url=request.url,
                                 headers=request.headers,
                                 auth=request.auth)
        self.pending_request.validate_response(response)
        payload = response.json()
        if self.json_format.metadata == ODataMetadataLevel.Verbose:
            payload = payload['d']['GetContextWebInformation']
        self.contextWebInformation = ContextWebInformation()
        self.contextWebInformation.from_json(payload)

    @property
    def web(self):
        """Get Web client object"""
        if not self.__web:
            self.__web = Web(self)
        return self.__web

    @property
    def site(self):
        """Get Site client object"""
        if not self.__site:
            self.__site = Site(self)
        return self.__site
コード例 #10
0
 def __init__(self, url, auth_context):
     super(ClientContext, self).__init__(url + "/_api/", auth_context)
     self.__web = None
     self.__site = None
     self.contextWebInformation = None
     self.json_format = JsonLightFormat(ODataMetadataLevel.Verbose)
コード例 #11
0
 def __init__(self, base_url, auth_context):
     if base_url.endswith("/"):
         base_url = base_url[:len(base_url) - 1]
     self._base_url = base_url
     super(ListDataService, self).__init__(auth_context)
     self._pendingRequest = ODataRequest(self, JsonLightFormat(ODataMetadataLevel.Verbose))