Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client update example #52

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions .github/workflows/issues.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: Issue FAQ
name: Answer issues

on:
issues:
types:
Expand All @@ -9,15 +10,34 @@ jobs:
permissions:
issues: write
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Check issue body
id: check_body
run: |
if echo "${{ github.event.issue.body }}" | grep -q ".client.update"; then
echo "close_issue=true" >> $GITHUB_ENV
else
echo "close_issue=false" >> $GITHUB_ENV
fi

- name: Close issue if condition met
if: env.close_issue == 'true'
run: gh issue close ${{ github.event.issue.number }} --comment "This issue is being closed automatically because it contains a question that exists in FAQ. Check the [demo.py](https://github.com/iwatkot/py3xui/blob/main/demo.py) file for an example. If you believe this is a mistake, please reopen the issue."
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Add comment
run: gh issue comment "$NUMBER" --body "$BODY"
if: env.close_issue == 'false'
run: gh issue comment "${{ github.event.issue.number }}" --body "$BODY"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
NUMBER: ${{ github.event.issue.number }}
BODY: >
Thank you for opening an issue!<br>
Please, first check our [FAQ](https://github.com/iwatkot/py3xui/discussions/categories/faq) section in discussions to see if your question has already been answered.<br>
Please, first check our [FAQ](https://github.com/iwatkot/py3xui/discussions/categories/faq?discussions_q=) section in discussions to see if your question has already been answered.<br>
You can also use Discussion if you have any questions or suggestions, while the Issues section is for bug reports and feature requests.<br>

If you want to proceed with the issue, please provide the following information:<br>
Expand Down
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,56 @@ You'll find detailed docs with usage examples for both APIs and for used models

In this section, you'll find some examples of how to use the SDK. In the examples, we'll use the synchronous API, but you can use the asynchronous API in the same way, just remember to use `await` before calling the methods.<br>

### Set the traffic limit for the client
ℹ️ You'll also find this example in the [demo.py](demo.py) file.

```python
from py3xui import Api

# 1️⃣ Create an instance of the API class.
host = "**************************"
username = "**********"
password = "**********"
api = Api(host, username, password)

# 2️⃣ Login to the API.
api.login()

user_email = "iwatkot" # ⬅️ Your user email here.
inbound_id = 4 # ⬅️ Your inbound ID here.

# 3️⃣ Get the inbound.
inbound = api.inbound.get_by_id(inbound_id)
print(f"Inbound has {len(inbound.settings.clients)} clients")

# 4️⃣ Find the needed client in the inbound.
client = None
for c in inbound.settings.clients:
if c.email == user_email:
client = c
break

if client:
print(f"Found client with ID: {client.id}") # ⬅️ The actual Client UUID.
else:
raise ValueError(f"Client with email {user_email} not found")

cliend_uuid = client.id

# 5️⃣ Get the client by email.
client_by_email = api.client.get_by_email(user_email)
print(f"Client by email has ID: {client_by_email.id}") # ⬅️ The numeric ID here.

# 6️⃣ Update the client with needed parameters.
client_by_email.total_gb = 1000 * 1024 * 1024 # ⬅️ Your value here.

# 7️⃣ Update the client ID so it will be UUID, not numeric.
client_by_email.id = cliend_uuid

# 8️⃣ Update the client.
api.client.update(client_by_email.id, client_by_email)
```

### Get inbounds list
```python
from py3xui import Api, Inbound
Expand Down
55 changes: 55 additions & 0 deletions demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from py3xui import Api

# In this short example we will update the client's traffic limit.
# Due to the way API returns the data, we need to get the needed inbound to
# obtain the actual UUID of the client.
# Becaue other endpoints return the numeric ID of the client in the given inbound,
# not the UUID.
# In the same way it can be implemented for AsyncApi, the methods are called
# exactly the same way, and has the same signatures.

# 1️⃣ Create an instance of the API class.
host = "**************************"
username = "**********"
password = "**********"
api = Api(host, username, password)

# 2️⃣ Login to the API.
api.login()

user_email = "iwatkot" # ⬅️ Your user email here.
inbound_id = 4 # ⬅️ Your inbound ID here.

# 3️⃣ Get the inbound.
inbound = api.inbound.get_by_id(inbound_id)
print(f"Inbound has {len(inbound.settings.clients)} clients")

# 4️⃣ Find the needed client in the inbound.
client = None
for c in inbound.settings.clients:
if c.email == user_email:
client = c
break

if client:
print(f"Found client with ID: {client.id}") # ⬅️ The actual Client UUID.
else:
raise ValueError(f"Client with email {user_email} not found")

cliend_uuid = client.id

# 5️⃣ Get the client by email.
client_by_email = api.client.get_by_email(user_email)
print(f"Client by email has ID: {client_by_email.id}") # ⬅️ The numeric ID here.

# 6️⃣ Update the client with needed parameters.
client_by_email.total_gb = 1000 * 1024 * 1024 # ⬅️ Your value here.

# 7️⃣ Update the client ID so it will be UUID, not numeric.
client_by_email.id = cliend_uuid

# 8️⃣ Update the client.
api.client.update(client_by_email.id, client_by_email)

# We are done! 🎉
# In this example we set the maximum traffic for the user with email "iwatkot" to 1GB.
Loading