Skip to content
Snippets Groups Projects
Commit d64f8711 authored by lucien senaneuch's avatar lucien senaneuch Committed by GATEAU Thibault
Browse files

[Create, Delete functions] implements create and delete function for all elements

parent 471e1d43
No related branches found
No related tags found
No related merge requests found
......@@ -19,6 +19,34 @@ nanospace.update_value(66, 'sma', '9000000')
print(nanospace.get_value(66)) # 9000000
```
### Creation of element
It is also possibile to create component, value, and modes from python. However, we advice to use the UI instead.
```python
# from nanospace import Nanospace
# nanospace = Nanospace('http://yourSrvUrl/','yourUsername', 'yourPassword')
nanospace._create_component(12, 'new_component)
nanospace._create_mode(12, my_new_mode)
nanospace._create_string_value(45, '')
```
If the id is missing check the nanospace_ui url can help you to get one.
Then each one of the _create_ function are returning a python dictionary where you can find id.
### Delete a element
Even if it is advise to delete element from the UI we give also the possibilities to delete element from python.
```python
# from nanospace import Nanospace
# nanospace = Nanospace('http://yourSrvUrl/','yourUsername', 'yourPassword')
nanospace._delete_component(12)
nanospace._delete_mode(45)
nanospace._delete_value(63)
```
Note that __delete_value__ is working either if the value is a formula, or a string
## Bash client
......
......@@ -208,13 +208,13 @@ class Nanospace:
# return r_data
def create_component(self, idParent, componentName):
def _create_component(self, idParent, componentName):
"""
Create a component children of the parent passed as id
Return the a JSON component
Return a JSON component
:Exemple:
nanospace.create_component(89, 'mySuperComponent')
nanospace._create_component(89, 'mySuperComponent')
"""
r = rq.post(self.ApiBaseAddress + 'component?parentId=' + str(idParent), json={'name': componentName}, headers=self.headers)
r_data = r.json()
......@@ -223,6 +223,97 @@ class Nanospace:
else:
return r_data
def _create_mode(self, idParent, modeName):
"""
Create a mode of a component parent passed as id
Return the a JSON component
:Exemple:
nanospace._create_mode(28, 'test-mode')
"""
r = rq.post(self.ApiBaseAddress + 'mode?parentId=' + str(idParent), json={'name': modeName}, headers=self.headers)
r_data = r.json()
if r.status_code < 200 or r.status_code >= 300:
raise ValueError(r_data)
else:
return r_data
def _create_string_value(self, idParent, valueName, value):
"""
Create a string of a mode parent passed as id
Return the a JSON component
:Exemple:
nanospace._create_string_value(28, 'string_value', 'Hello-World')
"""
r = rq.post(self.ApiBaseAddress + 'string?parentId=' + str(idParent), json={'name': valueName, 'value': value, 'type': 'string_value'}, headers=self.headers)
r_data = r.json()
if r.status_code < 200 or r.status_code >= 300:
raise ValueError(r_data)
else:
return r_data
def _create_formula_value(self, idParent, valueName, value):
"""
Create a formula of a mode parent passed as id
Return the a JSON component
:Exemple:
nanospace._create_formula_value(28, 'value', '5*8')
"""
r = rq.post(self.ApiBaseAddress + 'formula?parentId=' + str(idParent), json={'name': valueName, 'formula': value, 'type': 'string_value'}, headers=self.headers)
r_data = r.json()
if r.status_code < 200 or r.status_code >= 300:
raise ValueError(r_data)
else:
return r_data
def _delete_value(self, idValue):
"""
Delete a value which the id has been passed as parameter
Return the a JSON component
:Exemple:
nanospace._delete_value(28)
"""
r = rq.delete(self.ApiBaseAddress + 'value/' + str(idValue), headers=self.headers)
# r_data = r.json()
# if r.status_code < 200 or r.status_code >= 300:
# raise ValueError(r_data)
# else:
# return r_data
def _delete_mode(self, idMode):
"""
Delete a mode which the id has been passed as parameter
Return the a JSON component
:Exemple:
nanospace._delete_mode(28)
"""
r = rq.delete(self.ApiBaseAddress + 'mode/' + str(idMode), headers=self.headers)
# r_data = r.json()
# if r.status_code < 200 or r.status_code >= 300:
# raise ValueError(r_data)
# else:
# return r_data
def _delete_component(self, idComponent):
"""
Delete a component which the id has been passed as parameter
Return the a JSON component
:Exemple:
nanospace._delete_component(28)
"""
r = rq.delete(self.ApiBaseAddress + 'component/' + str(idComponent), headers=self.headers)
# r_data = r.json()
# if r.status_code < 200 or r.status_code >= 300:
# raise ValueError(r_data)
# else:
# return r_data
def setApiBaseAddress(self, url):
'''
......
from nanospace import Nanospace
usr = 'usr'
pw = 'usrPw'
usr = 'lucien'
pw = 'coucou'
srvAddr = 'http://localhost:8888/'
nanospace = Nanospace(srvAddr, usr, pw)
print(nanospace.get_string_value(83))
nanospace.update__string_value(83, 'test', '1Kg5')
print(nanospace.get_formula_value(84))
nanospace.update__formula_value(84, 'multiplication','5*20')
# create component
nanospace.create_component(40, 'newComponent')
\ No newline at end of file
nanospace.update_formula_value(84, 'multiplication','5*20')
create component
nanospace._create_component(40, 'newComponent')
nanospace._create_formula_value(231, 'test2', '5*5')
nanospace._create_string_value(231, 'test3', 'Hello-world')
nanospace._create_mode(60, 'new_mode')
nanospace._delete_component(60)
nanospace._delete_value(102)
nanospace._delete_mode(231)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment