jnpr/space/collection.py
def post(self, new_obj=None, accept=None, content_type=None,
             request_body=None, xml_name=None, task_monitor=None):
        """
        Sends a POST request to the Space server to create a new Resource in
        this collection.

        :param new_obj: The new Resource that needs to be created as a member
            of this collection. This can be omitted in which case the caller
            must supply a request body for the POST request as the
            ``request_body`` argument.
        :type new_obj: A single ``jnpr.space.resource.Resource`` instance
            or a list of them.

        :param str accept: This can be used to supply a media-type that must
            be used as the Accept header in the request. This defaults to
            ``None`` and in this case SpaceEZ will use the media-type modeled
            in the description file.

        :param str content_type: This can be used to supply a media-type that must
            be used as the Content-Type header in the request. This defaults to
            ``None`` and in this case SpaceEZ will use the media-type modeled
            in the description file.

        :param str request_body: This can be used to supply a string that must
            be used as the request body in the request. This defaults to
            ``None`` and in this case the caller must supply the ``new_obj``
            argument from which the request body will be formed.

        :param str xml_name: Can be used to override the name of the top-level
            XML element in the generated request body. This is useful in some
            cases such as creating a quick config template.
            This parameter defaults to ``None``.

        :param task_monitor: A TaskMonitor object that can be used to monitor
            the progress of the POST request, in case of asynchronous
            invocations. You need to check Junos Space API documentation to
            see if the POST invocation on this resource has asynchronous
            semantics and supply the task_monitor parameter only if it is
            asynchronous. Otherwise, this will default to ``None`` and the
            method will behave with synchronous semantics.
        :type task_monitor: jnpr.space.async.TaskMonitor

        :returns: If the new_obj parameter is a list, then the same list is
            returned. Otherwise, this method creates a new Resource object
            based on the state of the newly created resource, as extracted from
            the POST response body. In the case of asynchronous
            invocation, this will represent a Task object and will contain the
            unique id of the Task executing the POST request in Space.

        :raises: ``jnpr.space.rest.RestException`` if the POST method results in
            an error response. The exception's ``response`` attribute will have
            the full response from Space.

        """
        if content_type is not None:
            media_type = content_type
        elif isinstance(new_obj, list):
            if self._meta_object.content_type is not None:
                media_type = self._meta_object.content_type
            else:
                media_type = self._meta_object.get_media_type(None)
        else:
            if new_obj is None:
                raise ValueError('Must provide content_type when providing request_body')
            media_type = new_obj.get_meta_object().get_media_type(None)

        headers = {'content-type': media_type}
        if accept is not None:
            headers['accept'] = accept

        if request_body is not None:
            body = request_body
            saved_root_tag = None
            if new_obj is not None:
                raise ValueError('Cannot use both request_body and new_obj!')
        else:
            if new_obj is None:
                raise ValueError('Cannot omit both request_body and new_obj!')

            xml_obj = None
            if isinstance(new_obj, list):
                xml_obj = etree.Element(self._meta_object.xml_name)
                for obj in new_obj:
                    xml_obj.append(obj.form_xml())
            else:
                xml_obj = new_obj.form_xml()

            saved_root_tag = xml_obj.tag

            if xml_name is not None:
                xml_obj.tag = xml_name

            body = xmlutil.cleanup(etree.tostring(xml_obj, encoding='unicode'))

        url = self.get_href()
        if task_monitor is not None:
            url = '?queue='.join([url, task_monitor.get_queue_url()])

        response = self._rest_end_point.post(url,
                                             headers,
                                             body)

        if response.status_code == 204: # Special case of post with null response
            return new_obj

        if response.status_code not in [200, 202]:
            raise rest.RestException("POST failed on %s" % self.get_href(),
                                     response)

        if task_monitor is not None:
            resp_str = xmlutil.get_text_from_response(response)
            return xmlutil.xml2obj(resp_str)

        if not isinstance(new_obj, list):
            # Fixing issue #17
            #r = response.text
            # Skip the <?xml> line to avoid encoding errors in lxml
            #start = r.index('?><') + 2
            #root = etree.fromstring(r[start:])
            root = xmlutil.get_xml_obj_from_response(response)
            #new_obj._xml_data = root
            #new_obj._rest_end_point = self._rest_end_point
            if saved_root_tag is not None:
                root.tag = saved_root_tag
            new_obj = self._create_resource(root)

        return new_obj

        Sends a POST request to the Space server to create a new Resource in
        this collection.

        :param new_obj: The new Resource that needs to be created as a member
            of this collection. This can be omitted in which case the caller
            must supply a request body for the POST request as the
            ``request_body`` argument.
        :type new_obj: A single ``jnpr.space.resource.Resource`` instance
            or a list of them.

        :param str accept: This can be used to supply a media-type that must
            be used as the Accept header in the request. This defaults to
            ``None`` and in this case SpaceEZ will use the media-type modeled
            in the description file.

        :param str content_type: This can be used to supply a media-type that must
            be used as the Content-Type header in the request. This defaults to
            ``None`` and in this case SpaceEZ will use the media-type modeled
            in the description file.

        :param str request_body: This can be used to supply a string that must
            be used as the request body in the request. This defaults to
            ``None`` and in this case the caller must supply the ``new_obj``
            argument from which the request body will be formed.

        :param str xml_name: Can be used to override the name of the top-level
            XML element in the generated request body. This is useful in some
            cases such as creating a quick config template.
            This parameter defaults to ``None``.

        :param task_monitor: A TaskMonitor object that can be used to monitor
            the progress of the POST request, in case of asynchronous
            invocations. You need to check Junos Space API documentation to
            see if the POST invocation on this resource has asynchronous
            semantics and supply the task_monitor parameter only if it is
            asynchronous. Otherwise, this will default to ``None`` and the
            method will behave with synchronous semantics.
        :type task_monitor: jnpr.space.async.TaskMonitor

        :returns: If the new_obj parameter is a list, then the same list is
            returned. Otherwise, this method creates a new Resource object
            based on the state of the newly created resource, as extracted from
            the POST response body. In the case of asynchronous
            invocation, this will represent a Task object and will contain the
            unique id of the Task executing the POST request in Space.

        :raises: ``jnpr.space.rest.RestException`` if the POST method results in
            an error response. The exception's ``response`` attribute will have
            the full response from Space.

        
Sends a POST request to the Space server to create a new Resource in
this collection.
Sends a POST request to the Space server to create a new Resource in this collection .
def post(self, new_obj=None, accept=None, content_type=None,
             request_body=None, xml_name=None, task_monitor=None):
        if content_type is not None:
            media_type = content_type
        elif isinstance(new_obj, list):
            if self._meta_object.content_type is not None:
                media_type = self._meta_object.content_type
            else:
                media_type = self._meta_object.get_media_type(None)
        else:
            if new_obj is None:
                raise ValueError('Must provide content_type when providing request_body')
            media_type = new_obj.get_meta_object().get_media_type(None)
        headers = {'content-type': media_type}
        if accept is not None:
            headers['accept'] = accept
        if request_body is not None:
            body = request_body
            saved_root_tag = None
            if new_obj is not None:
                raise ValueError('Cannot use both request_body and new_obj!')
        else:
            if new_obj is None:
                raise ValueError('Cannot omit both request_body and new_obj!')
            xml_obj = None
            if isinstance(new_obj, list):
                xml_obj = etree.Element(self._meta_object.xml_name)
                for obj in new_obj:
                    xml_obj.append(obj.form_xml())
            else:
                xml_obj = new_obj.form_xml()
            saved_root_tag = xml_obj.tag
            if xml_name is not None:
                xml_obj.tag = xml_name
            body = xmlutil.cleanup(etree.tostring(xml_obj, encoding='unicode'))
        url = self.get_href()
        if task_monitor is not None:
            url = '?queue='.join([url, task_monitor.get_queue_url()])
        response = self._rest_end_point.post(url,
                                             headers,
                                             body)
        if response.status_code == 204: 
            return new_obj
        if response.status_code not in [200, 202]:
            raise rest.RestException("POST failed on %s" % self.get_href(),
                                     response)
        if task_monitor is not None:
            resp_str = xmlutil.get_text_from_response(response)
            return xmlutil.xml2obj(resp_str)
        if not isinstance(new_obj, list):
            root = xmlutil.get_xml_obj_from_response(response)
            if saved_root_tag is not None:
                root.tag = saved_root_tag
            new_obj = self._create_resource(root)
        return new_obj
def post ( self , new_obj = None , accept = None , content_type = None , request_body = None , xml_name = None , task_monitor = None ) : if content_type is not None : media_type = content_type elif isinstance ( new_obj , list ) : if self . _meta_object . content_type is not None : media_type = self . _meta_object . content_type else : media_type = self . _meta_object . get_media_type ( None ) else : if new_obj is None : raise ValueError ( 'Must provide content_type when providing request_body' ) media_type = new_obj . get_meta_object ( ) . get_media_type ( None ) headers = { 'content-type' : media_type } if accept is not None : headers [ 'accept' ] = accept if request_body is not None : body = request_body saved_root_tag = None if new_obj is not None : raise ValueError ( 'Cannot use both request_body and new_obj!' ) else : if new_obj is None : raise ValueError ( 'Cannot omit both request_body and new_obj!' ) xml_obj = None if isinstance ( new_obj , list ) : xml_obj = etree . Element ( self . _meta_object . xml_name ) for obj in new_obj : xml_obj . append ( obj . form_xml ( ) ) else : xml_obj = new_obj . form_xml ( ) saved_root_tag = xml_obj . tag if xml_name is not None : xml_obj . tag = xml_name body = xmlutil . cleanup ( etree . tostring ( xml_obj , encoding = 'unicode' ) ) url = self . get_href ( ) if task_monitor is not None : url = '?queue=' . join ( [ url , task_monitor . get_queue_url ( ) ] ) response = self . _rest_end_point . post ( url , headers , body ) if response . status_code == 204 : return new_obj if response . status_code not in [ 200 , 202 ] : raise rest . RestException ( "POST failed on %s" % self . get_href ( ) , response ) if task_monitor is not None : resp_str = xmlutil . get_text_from_response ( response ) return xmlutil . xml2obj ( resp_str ) if not isinstance ( new_obj , list ) : root = xmlutil . get_xml_obj_from_response ( response ) if saved_root_tag is not None : root . tag = saved_root_tag new_obj = self . _create_resource ( root ) return new_obj
Sends a POST request to the Space server to create a new Resource in
this collection.
Sends a POST request to the Space server to create a new Resource in this collection .
"""
        Sends a POST request to the Space server to create a new Resource in
        this collection.

        :param new_obj: The new Resource that needs to be created as a member
            of this collection. This can be omitted in which case the caller
            must supply a request body for the POST request as the
            ``request_body`` argument.
        :type new_obj: A single ``jnpr.space.resource.Resource`` instance
            or a list of them.

        :param str accept: This can be used to supply a media-type that must
            be used as the Accept header in the request. This defaults to
            ``None`` and in this case SpaceEZ will use the media-type modeled
            in the description file.

        :param str content_type: This can be used to supply a media-type that must
            be used as the Content-Type header in the request. This defaults to
            ``None`` and in this case SpaceEZ will use the media-type modeled
            in the description file.

        :param str request_body: This can be used to supply a string that must
            be used as the request body in the request. This defaults to
            ``None`` and in this case the caller must supply the ``new_obj``
            argument from which the request body will be formed.

        :param str xml_name: Can be used to override the name of the top-level
            XML element in the generated request body. This is useful in some
            cases such as creating a quick config template.
            This parameter defaults to ``None``.

        :param task_monitor: A TaskMonitor object that can be used to monitor
            the progress of the POST request, in case of asynchronous
            invocations. You need to check Junos Space API documentation to
            see if the POST invocation on this resource has asynchronous
            semantics and supply the task_monitor parameter only if it is
            asynchronous. Otherwise, this will default to ``None`` and the
            method will behave with synchronous semantics.
        :type task_monitor: jnpr.space.async.TaskMonitor

        :returns: If the new_obj parameter is a list, then the same list is
            returned. Otherwise, this method creates a new Resource object
            based on the state of the newly created resource, as extracted from
            the POST response body. In the case of asynchronous
            invocation, this will represent a Task object and will contain the
            unique id of the Task executing the POST request in Space.

        :raises: ``jnpr.space.rest.RestException`` if the POST method results in
            an error response. The exception's ``response`` attribute will have
            the full response from Space.

        """ # Special case of post with null response # Fixing issue #17 #r = response.text # Skip the <?xml> line to avoid encoding errors in lxml #start = r.index('?><') + 2 #root = etree.fromstring(r[start:]) #new_obj._xml_data = root #new_obj._rest_end_point = self._rest_end_point
If the new_obj parameter is a list, then the same list is
returned. Otherwise, this method creates a new Resource object
based on the state of the newly created resource, as extracted from
the POST response body. In the case of asynchronous
invocation, this will represent a Task object and will contain the
unique id of the Task executing the POST request in Space.
If the new_obj parameter is a list then the same list is returned . Otherwise this method creates a new Resource object based on the state of the newly created resource as extracted from the POST response body . In the case of asynchronous invocation this will represent a Task object and will contain the unique id of the Task executing the POST request in Space .
``jnpr.space.rest.RestException`` if the POST method results in
an error response. The exception's ``response`` attribute will have
the full response from Space.
` ` jnpr . space . rest . RestException ` ` if the POST method results in an error response . The exception ' s ` ` response ` ` attribute will have the full response from Space .
self
new_obj
The new Resource that needs to be created as a member
of this collection. This can be omitted in which case the caller
must supply a request body for the POST request as the
``request_body`` argument.
The new Resource that needs to be created as a member of this collection . This can be omitted in which case the caller must supply a request body for the POST request as the ` ` request_body ` ` argument .
accept
This can be used to supply a media-type that must
be used as the Accept header in the request. This defaults to
``None`` and in this case SpaceEZ will use the media-type modeled
in the description file.
This can be used to supply a media - type that must be used as the Accept header in the request . This defaults to ` ` None ` ` and in this case SpaceEZ will use the media - type modeled in the description file .
content_type
This can be used to supply a media-type that must
be used as the Content-Type header in the request. This defaults to
``None`` and in this case SpaceEZ will use the media-type modeled
in the description file.
This can be used to supply a media - type that must be used as the Content - Type header in the request . This defaults to ` ` None ` ` and in this case SpaceEZ will use the media - type modeled in the description file .
request_body
This can be used to supply a string that must
be used as the request body in the request. This defaults to
``None`` and in this case the caller must supply the ``new_obj``
argument from which the request body will be formed.
This can be used to supply a string that must be used as the request body in the request . This defaults to ` ` None ` ` and in this case the caller must supply the ` ` new_obj ` ` argument from which the request body will be formed .
xml_name
Can be used to override the name of the top-level
XML element in the generated request body. This is useful in some
cases such as creating a quick config template.
This parameter defaults to ``None``.
Can be used to override the name of the top - level XML element in the generated request body . This is useful in some cases such as creating a quick config template . This parameter defaults to ` ` None ` ` .
``None``
task_monitor
A TaskMonitor object that can be used to monitor
the progress of the POST request, in case of asynchronous
invocations. You need to check Junos Space API documentation to
see if the POST invocation on this resource has asynchronous
semantics and supply the task_monitor parameter only if it is
asynchronous. Otherwise, this will default to ``None`` and the
method will behave with synchronous semantics.
A TaskMonitor object that can be used to monitor the progress of the POST request in case of asynchronous invocations . You need to check Junos Space API documentation to see if the POST invocation on this resource has asynchronous semantics and supply the task_monitor parameter only if it is asynchronous . Otherwise this will default to ` ` None ` ` and the method will behave with synchronous semantics .