Beispiel #1
0
def ProfilesFeedFromString(xml_string):
  """Converts an XML string into a ProfilesFeed object.

  Args:
    xml_string: string The XML describing a Profiles feed.

  Returns:
    A ProfilesFeed object corresponding to the given XML.
  """
  return atom_core.parse(ProfilesFeed, xml_string)
Beispiel #2
0
  def set_xml_blob(self, blob):
    """Sets the contents of the extendedProperty to XML as a child node.

    Since the extendedProperty is only allowed one child element as an XML
    blob, setting the XML blob will erase any preexisting member elements
    in this object.

    Args:
      blob: str  or atom_core.XmlElement representing the XML blob stored in
            the extendedProperty.
    """
    # Erase any existing extension_elements, clears the child nodes from the
    # extendedProperty.
    if isinstance(blob, atom_core.XmlElement):
      self._other_elements = [blob]
    else:
      self._other_elements = [atom_core.parse(str(blob))]
Beispiel #3
0
    def request(self,
                method=None,
                uri=None,
                auth_token=None,
                http_request=None,
                converter=None,
                desired_class=None,
                redirects_remaining=4,
                **kwargs):
        """Make an HTTP request to the server.

    See also documentation for atom_client.AtomPubClient.request.

    If a 302 redirect is sent from the server to the client, this client
    assumes that the redirect is in the form used by the Google Calendar API.
    The same request URI and method will be used as in the original request,
    but a gsessionid URL parameter will be added to the request URI with
    the value provided in the server's 302 redirect response. If the 302
    redirect is not in the format specified by the Google Calendar API, a
    RedirectError will be raised containing the body of the server's
    response.

    The method calls the client's modify_request method to make any changes
    required by the client before the request is made. For example, a
    version 2 client could add a GData-Version: 2 header to the request in
    its modify_request method.

    Args:
      method: str The HTTP verb for this request, usually 'GET', 'POST',
              'PUT', or 'DELETE'
      uri: atom_http_core.Uri, str, or unicode The URL being requested.
      auth_token: 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: (optional) atom_http_core.HttpRequest
      converter: function which takes the body of the response as it's only
                 argument and returns the desired object.
      desired_class: class descended from atom_core.XmlElement to which a
                     successful response should be converted. If there is no
                     converter function specified (converter=None) then the
                     desired_class will be used in calling the
                     atom_core.parse function. If neither
                     the desired_class nor the converter is specified, an
                     HTTP reponse object will be returned.
      redirects_remaining: (optional) int, if this number is 0 and the
                           server sends a 302 redirect, the request method
                           will raise an exception. This parameter is used in
                           recursive request calls to avoid an infinite loop.

    Any additional arguments are passed through to
    atom_client.AtomPubClient.request.

    Returns:
      An HTTP response object (see atom_http_core.HttpResponse for a
      description of the object's interface) if no converter was
      specified and no desired_class was specified. If a converter function
      was provided, the results of calling the converter are returned. If no
      converter was specified but a desired_class was provided, the response
      body will be converted to the class using
      atom_core.parse.
    """
        if isinstance(uri, (str, unicode)):
            uri = atom_http_core.Uri.parse_uri(uri)

        # Add the gsession ID to the URL to prevent further redirects.
        # TODO: If different sessions are using the same client, there will be a
        # multitude of redirects and session ID shuffling.
        # If the gsession ID is in the URL, adopt it as the standard location.
        if uri is not None and uri.query is not None and 'gsessionid' in uri.query:
            self.__gsessionid = uri.query['gsessionid']
        # The gsession ID could also be in the HTTP request.
        elif (http_request is not None and http_request.uri is not None
              and http_request.uri.query is not None
              and 'gsessionid' in http_request.uri.query):
            self.__gsessionid = http_request.uri.query['gsessionid']
        # If the gsession ID is stored in the client, and was not present in the
        # URI then add it to the URI.
        elif self.__gsessionid is not None:
            uri.query['gsessionid'] = self.__gsessionid

        # The AtomPubClient should call this class' modify_request before
        # performing the HTTP request.
        #http_request = self.modify_request(http_request)

        response = atom_client.AtomPubClient.request(self,
                                                     method=method,
                                                     uri=uri,
                                                     auth_token=auth_token,
                                                     http_request=http_request,
                                                     **kwargs)
        # On success, convert the response body using the desired converter
        # function if present.
        if response is None:
            return None
        if response.status == 200 or response.status == 201:
            if converter is not None:
                return converter(response)
            elif desired_class is not None:
                if self.api_version is not None:
                    return atom_core.parse(response.read(),
                                           desired_class,
                                           version=get_xml_version(
                                               self.api_version))
                else:
                    # No API version was specified, so allow parse to
                    # use the default version.
                    return atom_core.parse(response.read(), desired_class)
            else:
                return response
        # TODO: move the redirect logic into the Google Calendar client once it
        # exists since the redirects are only used in the calendar API.
        elif response.status == 302:
            if redirects_remaining > 0:
                location = (response.getheader('Location')
                            or response.getheader('location'))
                if location is not None:
                    m = re.compile('[\?\&]gsessionid=(\w*)').search(location)
                    if m is not None:
                        self.__gsessionid = m.group(1)
                    # Make a recursive call with the gsession ID in the URI to follow
                    # the redirect.
                    return self.request(
                        method=method,
                        uri=uri,
                        auth_token=auth_token,
                        http_request=http_request,
                        converter=converter,
                        desired_class=desired_class,
                        redirects_remaining=redirects_remaining - 1,
                        **kwargs)
                else:
                    raise error_from_response(
                        '302 received without Location header', response,
                        RedirectError)
            else:
                raise error_from_response('Too many redirects from server',
                                          response, RedirectError)
        elif response.status == 401:
            raise error_from_response('Unauthorized - Server responded with',
                                      response, Unauthorized)
        elif response.status == 304:
            raise error_from_response(
                'Entry Not Modified - Server responded with', response,
                NotModified)
        elif response.status == 501:
            raise error_from_response(
                'This API operation is not implemented. - Server responded with',
                response, NotImplemented)
        # If the server's response was not a 200, 201, 302, 304, 401, or 501, raise
        # an exception.
        else:
            raise error_from_response('Server responded with', response,
                                      RequestError)
Beispiel #4
0
    def request(
        self,
        method=None,
        uri=None,
        auth_token=None,
        http_request=None,
        converter=None,
        desired_class=None,
        redirects_remaining=4,
        **kwargs
    ):
        """Make an HTTP request to the server.

    See also documentation for atom_client.AtomPubClient.request.

    If a 302 redirect is sent from the server to the client, this client
    assumes that the redirect is in the form used by the Google Calendar API.
    The same request URI and method will be used as in the original request,
    but a gsessionid URL parameter will be added to the request URI with
    the value provided in the server's 302 redirect response. If the 302
    redirect is not in the format specified by the Google Calendar API, a
    RedirectError will be raised containing the body of the server's
    response.

    The method calls the client's modify_request method to make any changes
    required by the client before the request is made. For example, a
    version 2 client could add a GData-Version: 2 header to the request in
    its modify_request method.

    Args:
      method: str The HTTP verb for this request, usually 'GET', 'POST',
              'PUT', or 'DELETE'
      uri: atom_http_core.Uri, str, or unicode The URL being requested.
      auth_token: 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: (optional) atom_http_core.HttpRequest
      converter: function which takes the body of the response as it's only
                 argument and returns the desired object.
      desired_class: class descended from atom_core.XmlElement to which a
                     successful response should be converted. If there is no
                     converter function specified (converter=None) then the
                     desired_class will be used in calling the
                     atom_core.parse function. If neither
                     the desired_class nor the converter is specified, an
                     HTTP reponse object will be returned.
      redirects_remaining: (optional) int, if this number is 0 and the
                           server sends a 302 redirect, the request method
                           will raise an exception. This parameter is used in
                           recursive request calls to avoid an infinite loop.

    Any additional arguments are passed through to
    atom_client.AtomPubClient.request.

    Returns:
      An HTTP response object (see atom_http_core.HttpResponse for a
      description of the object's interface) if no converter was
      specified and no desired_class was specified. If a converter function
      was provided, the results of calling the converter are returned. If no
      converter was specified but a desired_class was provided, the response
      body will be converted to the class using
      atom_core.parse.
    """
        if isinstance(uri, (str, unicode)):
            uri = atom_http_core.Uri.parse_uri(uri)

        # Add the gsession ID to the URL to prevent further redirects.
        # TODO: If different sessions are using the same client, there will be a
        # multitude of redirects and session ID shuffling.
        # If the gsession ID is in the URL, adopt it as the standard location.
        if uri is not None and uri.query is not None and "gsessionid" in uri.query:
            self.__gsessionid = uri.query["gsessionid"]
        # The gsession ID could also be in the HTTP request.
        elif (
            http_request is not None
            and http_request.uri is not None
            and http_request.uri.query is not None
            and "gsessionid" in http_request.uri.query
        ):
            self.__gsessionid = http_request.uri.query["gsessionid"]
        # If the gsession ID is stored in the client, and was not present in the
        # URI then add it to the URI.
        elif self.__gsessionid is not None:
            uri.query["gsessionid"] = self.__gsessionid

        # The AtomPubClient should call this class' modify_request before
        # performing the HTTP request.
        # http_request = self.modify_request(http_request)

        response = atom_client.AtomPubClient.request(
            self, method=method, uri=uri, auth_token=auth_token, http_request=http_request, **kwargs
        )
        # On success, convert the response body using the desired converter
        # function if present.
        if response is None:
            return None
        if response.status == 200 or response.status == 201:
            if converter is not None:
                return converter(response)
            elif desired_class is not None:
                if self.api_version is not None:
                    return atom_core.parse(response.read(), desired_class, version=get_xml_version(self.api_version))
                else:
                    # No API version was specified, so allow parse to
                    # use the default version.
                    return atom_core.parse(response.read(), desired_class)
            else:
                return response
        # TODO: move the redirect logic into the Google Calendar client once it
        # exists since the redirects are only used in the calendar API.
        elif response.status == 302:
            if redirects_remaining > 0:
                location = response.getheader("Location") or response.getheader("location")
                if location is not None:
                    m = re.compile("[\?\&]gsessionid=(\w*)").search(location)
                    if m is not None:
                        self.__gsessionid = m.group(1)
                    # Make a recursive call with the gsession ID in the URI to follow
                    # the redirect.
                    return self.request(
                        method=method,
                        uri=uri,
                        auth_token=auth_token,
                        http_request=http_request,
                        converter=converter,
                        desired_class=desired_class,
                        redirects_remaining=redirects_remaining - 1,
                        **kwargs
                    )
                else:
                    raise error_from_response("302 received without Location header", response, RedirectError)
            else:
                raise error_from_response("Too many redirects from server", response, RedirectError)
        elif response.status == 401:
            raise error_from_response("Unauthorized - Server responded with", response, Unauthorized)
        elif response.status == 304:
            raise error_from_response("Entry Not Modified - Server responded with", response, NotModified)
        elif response.status == 501:
            raise error_from_response(
                "This API operation is not implemented. - Server responded with", response, NotImplemented
            )
        # If the server's response was not a 200, 201, 302, 304, 401, or 501, raise
        # an exception.
        else:
            raise error_from_response("Server responded with", response, RequestError)
Beispiel #5
0
def ExternalIdFromString(xml_string):
  return atom_core.parse(ExternalId, xml_string)
Beispiel #6
0
def WebsiteFromString(xml_string):
  return atom_core.parse(Website, xml_string)
Beispiel #7
0
def UserDefinedFieldFromString(xml_string):
  return atom_core.parse(UserDefinedField, xml_string)