예제 #1
0
    def batch(self, feed, uri=None, force=False, auth_token=None, **kwargs):
        """Sends a batch request to the server to execute operation entries.

        Args:
          feed: A batch feed containing batch entries, each is an operation.
          uri: (optional) The uri to which the batch request feed should be POSTed.
              If none is provided, then the feed's edit link will be used.
          force: (optional) boolean set to True if you want the batch update to
              clobber all data. If False, the version in the information in the
              feed object will cause the server to check to see that no changes
              intervened between when you fetched the data and when you sent the
              changes.
          auth_token: (optional) An object which sets the Authorization HTTP header
              in its modify_request method. Recommended classes include
              gdata.gauth.ClientLoginToken and gdata.gauth.AuthSubToken
              among others.
        """
        http_request = http_core.HttpRequest()
        http_request.add_body_part(
            feed.to_string(get_xml_version(self.api_version)),
            'application/atom+xml')
        if force:
            http_request.headers['If-Match'] = '*'
        elif hasattr(feed, 'etag') and feed.etag:
            http_request.headers['If-Match'] = feed.etag

        if uri is None:
            uri = feed.find_edit_link()

        return self.request(method='POST',
                            uri=uri,
                            auth_token=auth_token,
                            http_request=http_request,
                            desired_class=feed.__class__,
                            **kwargs)
예제 #2
0
    def modify_request(self, http_request):
        """Changes the HTTP request before sending it to the server.

        Sets the User-Agent HTTP header and fills in the HTTP host portion
        of the URL if one was not included in the request (for this it uses
        the self.host member if one is set). This method is called in
        self.request.

        Args:
          http_request: An http_core.HttpRequest() (optional) If one is
                        not provided, a new HttpRequest is instantiated.

        Returns:
          An http_core.HttpRequest() with the User-Agent header set and
          if this client has a value in its host member, the host in the request
          URL is set.
        """
        if http_request is None:
            http_request = http_core.HttpRequest()

        if self.host is not None and http_request.uri.host is None:
            http_request.uri.host = self.host

        if self.xoauth_requestor_id is not None:
            http_request.uri.query[
                'xoauth_requestor_id'] = self.xoauth_requestor_id

        # Set the user agent header for logging purposes.
        if self.source:
            http_request.headers[
                'User-Agent'] = '%s gdata-py/2.0.18' % self.source
        else:
            http_request.headers['User-Agent'] = 'gdata-py/2.0.17'

        return http_request
예제 #3
0
    def _refresh(self, request):
        """Refresh the access_token using the refresh_token.

        Args:
          request: The atom.http_core.HttpRequest which contains all of the
              information needed to send a request to the remote server.
        """
        body = urllib.parse.urlencode({
            "grant_type": "refresh_token",
            "client_id": self.client_id,
            "client_secret": self.client_secret,
            "refresh_token": self.refresh_token,
        })
        headers = {
            "user-agent": self.user_agent,
        }

        http_request = http_core.HttpRequest(uri=self.token_uri,
                                             method="POST",
                                             headers=headers)
        http_request.add_body_part(
            body, mime_type="application/x-www-form-urlencoded")
        response = request(http_request)
        body = response.read()
        if response.status == 200:
            self._extract_tokens(body)
        else:
            self._invalid = True
        return response
예제 #4
0
    def delete(self, entry_or_uri, auth_token=None, force=False, **kwargs):
        http_request = http_core.HttpRequest()

        # Include the ETag in the request if present.
        if force:
            http_request.headers["If-Match"] = "*"
        elif hasattr(entry_or_uri, "etag") and entry_or_uri.etag:
            http_request.headers["If-Match"] = entry_or_uri.etag

        # If the user passes in a URL, just delete directly, may not work as
        # the service might require an ETag.
        if isinstance(entry_or_uri, (str, http_core.Uri)):
            return self.request(
                method="DELETE",
                uri=entry_or_uri,
                http_request=http_request,
                auth_token=auth_token,
                **kwargs
            )

        return self.request(
            method="DELETE",
            uri=entry_or_uri.find_edit_link(),
            http_request=http_request,
            auth_token=auth_token,
            **kwargs
        )
예제 #5
0
 def get_entry(self,
               uri,
               auth_token=None,
               converter=None,
               desired_class=gdata_data.GDEntry,
               etag=None,
               **kwargs):
     http_request = http_core.HttpRequest()
     # Conditional retrieval
     if etag is not None:
         http_request.headers['If-None-Match'] = etag
     return self.request(method='GET',
                         uri=uri,
                         auth_token=auth_token,
                         http_request=http_request,
                         converter=converter,
                         desired_class=desired_class,
                         **kwargs)
예제 #6
0
 def post(
     self, entry, uri, auth_token=None, converter=None, desired_class=None, **kwargs
 ):
     if converter is None and desired_class is None:
         desired_class = entry.__class__
     http_request = http_core.HttpRequest()
     http_request.add_body_part(
         entry.to_string(get_xml_version(self.api_version)), "application/atom+xml"
     )
     return self.request(
         method="POST",
         uri=uri,
         auth_token=auth_token,
         http_request=http_request,
         converter=converter,
         desired_class=desired_class,
         **kwargs
     )
예제 #7
0
    def update(self, entry, auth_token=None, force=False, uri=None, **kwargs):
        """Edits the entry on the server by sending the XML for this entry.

        Performs a PUT and converts the response to a new entry object with a
        matching class to the entry passed in.

        Args:
          entry:
          auth_token:
          force: boolean stating whether an update should be forced. Defaults to
                 False. Normally, if a change has been made since the passed in
                 entry was obtained, the server will not overwrite the entry since
                 the changes were based on an obsolete version of the entry.
                 Setting force to True will cause the update to silently
                 overwrite whatever version is present.
          uri: The uri to put to. If provided, this uri is PUT to rather than the
               inferred uri from the entry's edit link.

        Returns:
          A new Entry object of a matching type to the entry which was passed in.
        """
        http_request = http_core.HttpRequest()
        http_request.add_body_part(
            entry.to_string(get_xml_version(self.api_version)), "application/atom+xml"
        )
        # Include the ETag in the request if present.
        if force:
            http_request.headers["If-Match"] = "*"
        elif hasattr(entry, "etag") and entry.etag:
            http_request.headers["If-Match"] = entry.etag

        if uri is None:
            uri = entry.find_edit_link()

        return self.request(
            method="PUT",
            uri=uri,
            auth_token=auth_token,
            http_request=http_request,
            desired_class=entry.__class__,
            **kwargs
        )