|
| 1 | +| title | authors | creation-date | last-updated | |
| 2 | +|-----------------------|------------------------------------|---------------|--------------| |
| 3 | +| add litmus python sdk | [@inpyu](https://github.com/inpyu) | 2024-09-07 | 2024-09-07 | |
| 4 | + |
| 5 | +# Adding Litmus python SDK |
| 6 | + |
| 7 | +- [Adding Litmus python SDK](#adding-litmus-python-sdk) |
| 8 | + - [Summary](#summary) |
| 9 | + - [Motivation](#motivation) |
| 10 | + - [Goals](#goals) |
| 11 | + - [Non-Goals](#non-goals) |
| 12 | + - [Proposal](#proposal) |
| 13 | + - [Use Cases](#use-cases) |
| 14 | + - [Implementation Details](#implementation-details) |
| 15 | + - [Initialize client](#initialize-client) |
| 16 | + - [Use Client](#use-client) |
| 17 | + - [Risks and Mitigations](#risks-and-mitigations) |
| 18 | + - [Upgrade / Downgrade Strategy](#upgrade--downgrade-strategy) |
| 19 | + - [Drawbacks](#drawbacks) |
| 20 | + - [Alternatives](#alternatives) |
| 21 | + - [References](#references) |
| 22 | + |
| 23 | +## Summary |
| 24 | + |
| 25 | +The purpose of litmus-python-sdk is to provide a client that can easily access to auth and backend server of litmus. |
| 26 | + |
| 27 | +## Motivation |
| 28 | + |
| 29 | +Litmuschaos’ backend server uses graphQL as an API communication method. |
| 30 | +Graphql is not as familiar to developers as REST API, so it is difficult to call backend server directly. |
| 31 | +Therefore we planned to add a Python-based SDK that makes developers to communicate easily with litmus backend server. |
| 32 | +The SDK also provides an interface to communicate with the auth server to wrap the entire litmus control plane. |
| 33 | + |
| 34 | +### Goals |
| 35 | + |
| 36 | +- Add client calling the API for Auth Server |
| 37 | +- Add client calling graphQL for Backend Server |
| 38 | + |
| 39 | + |
| 40 | +### Non-Goals |
| 41 | + |
| 42 | +- Changing APIs that auth server and backend server already provide is non-goal |
| 43 | + |
| 44 | +## Proposal |
| 45 | + |
| 46 | +### Use Cases |
| 47 | + |
| 48 | +In organization, litmusChaos administrators can call python SDK to manage multiple users and projects. |
| 49 | + |
| 50 | +### Implementation Details |
| 51 | + |
| 52 | +#### Initialize client |
| 53 | + |
| 54 | +```python |
| 55 | +class LitmusClient: |
| 56 | + def __init__(self, host_url, username, password): |
| 57 | + self.host_url = host_url |
| 58 | + self.username = username |
| 59 | + self.password = password |
| 60 | + self.session = requests.Session() |
| 61 | + self.session.auth = (username, password) |
| 62 | + |
| 63 | + def create_project(self, project_name, description, tags): |
| 64 | + url = f"{self.host_url}/projects" |
| 65 | + payload = { |
| 66 | + "projectName": project_name, |
| 67 | + "description": description, |
| 68 | + "tags": tags |
| 69 | + } |
| 70 | + response = self.session.post(url, json=payload) |
| 71 | + |
| 72 | + if response.status_code == 201: |
| 73 | + return response.json() |
| 74 | + else: |
| 75 | + response.raise_for_status() |
| 76 | + |
| 77 | +def init(): |
| 78 | + host_url = "http://localhost:3000" |
| 79 | + username = "admin" |
| 80 | + password = "litmus" |
| 81 | + return LitmusClient(host_url, username, password) |
| 82 | + |
| 83 | +``` |
| 84 | +#### Use Client |
| 85 | +```python |
| 86 | +def execute(): |
| 87 | + client = init() |
| 88 | + |
| 89 | + project_name = "demo project" |
| 90 | + description = "demo project description" |
| 91 | + tags = ["litmus", "chaos"] |
| 92 | + |
| 93 | + response = client.create_project(project_name, description, tags) |
| 94 | + print(response) |
| 95 | + |
| 96 | +``` |
| 97 | +## Risks and Mitigations |
| 98 | + |
| 99 | +## Upgrade / Downgrade Strategy |
| 100 | + |
| 101 | +## Drawbacks |
| 102 | + |
| 103 | +It will be a great opportunity for administrators of litmusChaos to manage users and conduct experiments more conveniently. |
| 104 | + |
| 105 | +## Alternatives |
| 106 | + |
| 107 | +This is the first python sdk we created in Litmus. No other alternatives exist. |
| 108 | + |
| 109 | +## References |
0 commit comments