Taking a look at Action
Hint
Let’s create a simple program that retrieve a Dossier, change one of its annotation and submit it to the Demarche
See also
The method is nearly the same for all actions, you can check them in the Actions section.
Retrieving a Dossier
First you need to retrieve a Dossier object as we did in the previous example.
1from demarches_simpy import Profile, Dossier, AnnotationModifier
2
3# Create a profile
4profile = Profile('API_KEY',verbose=True)
5
6# Create a dossier
7dossier = Dossier(000000, profile)
Note
You can notice here that we didn’t use a Demarche object to retrieve a Dossier, if you know a Dossier’s number you can manually create a Dossier object from a Profile and a Dossier number.
Get an instructeur id
To submit a Dossier to the Demarche you need to provide an instructeur id, this id can be retrieved either with the Demarche or with the Dossier object.
1
2# Assign an instructeur_id
3
4# First method
5profile.set_instructeur_id('instructeur_id')
6
7# Second method
8## Only works if the dossier is not in CONSTRUCTION state
9instructeurs = dossier.get_attached_instructeurs_info()
10instructeur = instructeurs[0]
11
12# print(instructeur)
13# > {'id': 'instructeur_id', 'email': 'mail@mail.com'}
14
15profile.set_instructeur_id(instructeur['id'])
Caution
You can access to instructeurs_id on a Dossier only if the dossier has been passed to INSTRUCTION state (because the instructeur_id is assigned at this state or later)
Note
You can assign an instructeur id directly in the Profile constructor.
Creating the Action
Now that we have a Dossier and an instructeur id we can create an Action, for this tutorial we will create an AnnotationModifier action. But all objects that derives from the IAction interface works the same way.
1# Create the annotation action
2
3action = AnnotationModifier(profile, dossier)
4
5# Get an annotation
6
7annotation = dossier.get_annotations()['annotation-label']
8
9# print(annotation)
10# > {'id': 'annotation_id', 'stringValue' : 'value'}
11
12# Modify the annotation
13
14annotation['stringValue'] = 'new_value'
Here we’ve created an AnnotationModifier action by passing the Dossier object we want to modify. For now all action objects works independently from the Demarche object, and alterate a Dossier. We can retrieve an annotation (which is a dict) and modify its stringValue.
Note
You can passing the instructeur_id directly in the action constructor if you profile doesn’t have one.
Submitting the Action
Until now, the changement we made on the annotation is local, we need to perform the action to take effect. We can perform the action by passing the modified annotation to the perform method.
1# Perform the action
2
3result = action.perform(annotation)
4
5if result == AnnotationModifier.NETWORK_ERROR:
6 print("Network error")
7elif result == AnnotationModifier.SUCCESS:
8 print("Success")
The result if the perform method is always an integer that represent the state of the action. The IAction inteface defines three constants that can be used to check the result of the action.
0 : SUCCESS
1 : NETWORK_ERROR
2 : REQUEST_ERROR
Note
If the action has also a result, you can retrieve it with specific methods of the action. Take also note that an action can also implement more error codes.
Full example
1from demarches_simpy import Profile, Dossier, AnnotationModifier
2
3# Create a profile
4profile = Profile('API_KEY',verbose=True)
5
6# Create a dossier
7dossier = Dossier(000000, profile)
8
9
10# Assign an instructeur_id
11
12# First method
13profile.set_instructeur_id('instructeur_id')
14
15# Second method
16## Only works if the dossier is not in CONSTRUCTION state
17instructeurs = dossier.get_attached_instructeurs_info()
18instructeur = instructeurs[0]
19
20# print(instructeur)
21# > {'id': 'instructeur_id', 'email': 'mail@mail.com'}
22
23profile.set_instructeur_id(instructeur['id'])
24
25# Create the annotation action
26
27action = AnnotationModifier(profile, dossier)
28
29# Get an annotation
30
31annotation = dossier.get_annotations()['annotation-label']
32
33# print(annotation)
34# > {'id': 'annotation_id', 'stringValue' : 'value'}
35
36# Modify the annotation
37
38annotation['stringValue'] = 'new_value'
39
40# Perform the action
41
42result = action.perform(annotation)
43
44if result == AnnotationModifier.NETWORK_ERROR:
45 print("Network error")
46elif result == AnnotationModifier.SUCCESS:
47 print("Success")
48
49