Skip to content
Snippets Groups Projects
Commit 19556750 authored by DHOMBRES Stephanie's avatar DHOMBRES Stephanie
Browse files

Update example and URL for demo

parent 8ef1ef76
Branches
No related tags found
1 merge request!1Dev update
JSatOrb: Simplified Mission Analysis REST-based Tool Dedicated to Education and Training
NanoSpace Python Client: API functions to exchange data with back-end
Copyright (C) 2019 ISAE-SUPAERO
Copyright (C) 2019-2025 ISAE-SUPAERO
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
......
# Nanospace-client
## Python client
### Requirement
Request package is needed
```bash
sudo apt install python3 python3-pip python-is-python3
pip install requests
```
### Simple examle
Check simple_example.py. Before try to launch it, think to change the login, password and srvUrl in the file.
Think to end the address with a '/'.
Check the `simple_example.py` python file.
Before try to launch it, change the `username`, `password` and `server` address (End the server address with a '/').
```python
from nanospace import Nanospace
nanospace = Nanospace('http://yourSrvUrl/','yourUsername', 'yourPassword')
nanospace = Nanospace('http://server_address/','username', 'password')
print(nanospace.get_string_value(66))
nanospace.update_value(66, 'sma', '9000000')
print(nanospace.get_string_value(66)) # 9000000
print(nanospace.get_string_value(66)) # Should print 9000000
print(nanospace.get_formula_value(66))
nanospace.update_value(77, 'sma', '9000000')
print(nanospace.get_formula_value(66)) # 9000000
print(nanospace.get_formula_value(66)) # Should print 9000000
```
### Creation of element
It is also possibile to create component, value, and modes from python. However, we advice to use the UI instead.
It is also possible 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 = Nanospace('http://server_address/','username', 'password')
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.
If the id is missing, check the Nanospace front-end url or the #id, that 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.
Even if it is advise to delete element from the UI, the following functions give also the possibilities to delete element from Python.
```python
# from nanospace import Nanospace
# nanospace = Nanospace('http://yourSrvUrl/','yourUsername', 'yourPassword')
# nanospace = Nanospace('http://server_address/','username', 'password')
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
Note that __delete_value__ is working either if the value is a formula, or a string.
## Bash client
......
import requests
def login_to_api(username, password):
"""
Logs in to the API using the provided username and password.
Parameters:
username (str): The username for login.
password (str): The password for login.
Returns:
dict: A dictionary containing the API response, including the status code and response text.
"""
# Login API URL
url = 'http://localhost:8888/login'
# Login data
data = {
'username': username,
'password': password
}
response = requests.post(url, json=data)
# Check response status
if response.status_code == 200:
print("Login successful!")
return response
else:
print(f"Login error: {response.status_code}")
return {'error': response.text, 'status_code': response.status_code}
# Example usage
if __name__ == "__main__":
result = login_to_api('test', 'test')
print(result)
This diff is collapsed.
from nanospace import Nanospace
usr = 'usrLogin'
pw = 'usrPw'
srvAddr = 'https://dcas-nanostar.isae.fr/api/'
nanospace = Nanospace(srvAddr, usr, pw)
print(nanospace.get_string_value(5))
# nanospace.update__string_value(83, 'test', '1Kg5')
# print(nanospace.get_formula_value(84))
# nanospace.update_formula_value(84, 'multiplication','5*20')
# 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
# ================================================================================
# ================================================================================
username = 'test'
password = 'test'
server = 'http://localhost:8888/'
id = 278 # For test : be sure this value exists
# ================================================================================
# ================================================================================
# Connection
nanospace = Nanospace(server, username, password)
# ================================================================================
# ================================================================================
# Get a value
print("--------------------------------------------")
print(nanospace.get_string_value(id))
# ================================================================================
# ================================================================================
# Update the value and associated verification
nanospace.update_string_value(id, 'Earth_Radius', '6378000')
print("--------------------------------------------")
print(nanospace.get_string_value(id))
# ================================================================================
# ================================================================================
print("--------------------------------------------")
print(nanospace._delete_value(393))
# ================================================================================
# ================================================================================
print("--------------------------------------------")
print(nanospace._create_string_value(44, 'test3', 'Hello-world'))
# ================================================================================
# ================================================================================
print("--------------------------------------------")
print(nanospace.get_formula_value(444))
nanospace.update_formula_value(444, 'multiplication','5*20')
print(nanospace.get_formula_value(444))
# ================================================================================
# ================================================================================
print("--------------------------------------------")
nanospace._create_component(85, 'newComponent')
# ================================================================================
# ================================================================================
print("--------------------------------------------")
nanospace._create_formula_value(73, 'test1', '5*5')
# ================================================================================
# ================================================================================
print("--------------------------------------------")
nanospace._create_string_value(73, 'test2', 'Hello-world')
# ================================================================================
# ================================================================================
print("--------------------------------------------")
nanospace._create_mode(23, 'new_mode_tutu')
# ================================================================================
# ================================================================================
print("--------------------------------------------")
nanospace._delete_component(448)
# ================================================================================
# ================================================================================
print("--------------------------------------------")
nanospace._delete_mode(449)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment