|
1 | 1 | [](https://www.python.org/downloads/release/python/)
|
2 | 2 | [](https://badge.fury.io/py/cloudshell-sandboxapi-wrapper)
|
3 | 3 |
|
4 |
| -# Cloudshell Sandbox API Wrapper |
5 |
| -A python client implementation of the [cloudshell sandbox api](https://help.quali.com/Online%20Help/0.0/Portal/Content/API/CS-Snbx-API-Topic.htm?Highlight=sandbox%20api). |
6 |
| - |
| 4 | +# Cloudshell Sandbox Rest Api Client |
| 5 | +A python client wrapper around the [cloudshell sandbox api](https://help.quali.com/Online%20Help/0.0/Portal/Content/API/CS-Snbx-API-Topic.htm?Highlight=sandbox%20api). |
7 | 6 | This client provides an object-oriented interface to instantiating an api session and interacting with the endpoints.
|
8 |
| -All methods return a loaded python dictionary/list of the [documented](https://help.quali.com/Online%20Help/0.0/Portal/Content/API/RefGuides/Sndbx-REST-API/REST-API-V2-Ref-Guide.htm?tocpath=CloudShell%20API%20Guide%7CCloudShell%20Sandbox%20API%7C_____3) json responses. |
9 |
| -No additional library object wrapping implemented. |
10 | 7 |
|
| 8 | +No additional library object wrapping implemented. All methods return the json.loads python object in the shape of the documented json response. See the [documentation](https://help.quali.com/Online%20Help/0.0/Portal/Content/API/RefGuides/Sndbx-REST-API/REST-API-V2-Ref-Guide.htm?tocpath=CloudShell%20API%20Guide%7CCloudShell%20Sandbox%20API%7C_____3) |
| 9 | +for details. |
11 | 10 |
|
12 | 11 | ### Installation
|
13 | 12 |
|
14 | 13 | ```
|
15 |
| -pip install cloudshell_sandboxapi_wrapper |
| 14 | +pip install cloudshell-sandbox-rest |
16 | 15 | ```
|
17 |
| -## Usage |
| 16 | + |
| 17 | +### Basic Usage |
18 | 18 | ```python
|
19 |
| -pass |
| 19 | +from cloudshell.sandbox_rest.sandbox_api import SandboxRestApiSession |
| 20 | +api = SandboxRestApiSession(host="localhost", username="admin", password="admin", domain="Global") |
| 21 | +start_response = api.start_sandbox(blueprint_id="<MY_BLUEPRINT_NAME>", sandbox_name="My Rest Api Blueprint") |
| 22 | +sandbox_id = start_response["id"] |
| 23 | +components_response = api.get_sandbox_components(sandbox_id) |
| 24 | +print(f"total components in sandbox: {len(components_response)}") |
20 | 25 | ```
|
| 26 | + |
| 27 | +### Context Manager Usage |
| 28 | +Using the api session with a context manager "with" statement will log out and invalidate the token for you. |
| 29 | + |
| 30 | +```python |
| 31 | +from cloudshell.sandbox_rest.sandbox_api import SandboxRestApiSession |
| 32 | + |
| 33 | +api = SandboxRestApiSession(host="localhost", username="admin", password="admin", domain="Global") |
| 34 | + |
| 35 | +with api: |
| 36 | + start_response = api.start_sandbox(blueprint_id="<MY_BLUEPRINT_NAME>", sandbox_name="My Rest Api Blueprint") |
| 37 | + sandbox_id = start_response["id"] |
| 38 | + components_response = api.get_sandbox_components(sandbox_id) |
| 39 | + print(f"total components in sandbox: {len(components_response)}") |
| 40 | + print(f"session token is: {api.token}") |
| 41 | + |
| 42 | +print(f"session token outside context manager: {api.token}") |
| 43 | +``` |
| 44 | +- NOTE: api login happens during init, not on entering context |
| 45 | +- context exit invalidates token |
| 46 | + |
| 47 | +### Instantiate Session with Token |
| 48 | +Common use case is for admin to pull user token and start a session on their behalf. This can be done as seen in example below. |
| 49 | +```python |
| 50 | +from cloudshell.sandbox_rest.sandbox_api import SandboxRestApiSession |
| 51 | + |
| 52 | +admin_api = SandboxRestApiSession(host="localhost", username="admin", password="admin", domain="Global") |
| 53 | + |
| 54 | +with admin_api: |
| 55 | + user_token = admin_api.get_token_for_target_user("end_user") |
| 56 | + user_api = SandboxRestApiSession(host="localhost", token=user_token, domain="<END_USERS_DOMAIN>") |
| 57 | + with user_api: |
| 58 | + start_response = user_api.start_sandbox(blueprint_id="<MY_BLUEPRINT_NAME>", sandbox_name="My Rest Api Blueprint") |
| 59 | + sandbox_id = start_response["id"] |
| 60 | + components_response = user_api.get_sandbox_components(sandbox_id) |
| 61 | + print(f"total components in sandbox: {len(components_response)}") |
| 62 | +``` |
| 63 | +- Note the use of nested context managers to manage the different session tokens |
| 64 | + |
| 65 | +### License |
| 66 | +Free Software: MIT License |
0 commit comments