File: deleting.rst

package info (click to toggle)
python-pyodata 1.11.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,280 kB
  • sloc: python: 7,170; xml: 947; makefile: 67; sh: 15
file content (56 lines) | stat: -rw-r--r-- 1,621 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Deleting
========

.. _CSRF: https://en.wikipedia.org/wiki/Cross-site_request_forgery
.. _Requests: https://2.python-requests.org/en/master/

The delete action executes the HTTP method DELETE which is usually protected by
CSRF_ and therefore you must make some effort to initialize your HTTP Session
to send DELETE requests acceptable by the remote server.

Let's assume you use the python library Requests_

.. code-block:: python

    import pyodata
    import requests

    SERVICE_URL = 'http://example.io/TheServiceRoot/'

    session = requests.Session()
    response = session.head(SERVICE_URL, headers={'x-csrf-token': 'fetch'})
    token = response.headers.get('x-csrf-token', '')
    session.headers.update({'x-csrf-token': token})

    theservice = pyodata.Client(SERVICE_URL, session)


Deleting an entity
------------------

You can either delete entity by passing its PropertyRef value to the delete function

.. code-block:: python

    request = service.entity_sets.Employees.delete_entity(23)
    request.execute()

or by passing the EntityKey object

.. code-block:: python

    key = EntityKey(service.schema.entity_type('Employee'), ID=23)
    request = service.entity_sets.Employees.delete_entity(key=key)
    request.execute()


Encode OData URL Path
-------------------------------------------

By default the resource paths of requests are percent encoded. However if this is not what your API expects, 
you can disable the encoding with the variable encode_path by setting it to False.


.. code-block:: python

   request = service.entity_sets.Employees.delete_entity(key=key, encode_path=False)