Skip to content

Commit 97c9fd6

Browse files
committed
Whole refactor of structure and readme
1 parent f496527 commit 97c9fd6

File tree

6 files changed

+172
-69
lines changed

6 files changed

+172
-69
lines changed

README.md

Lines changed: 136 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,184 @@
1-
# UnipyAccess API Connector
21

3-
## Overview
2+
# unipyaccess
43

5-
unipyAccess is a Python class designed to interface with the Unifi Access, allowing for the management of users in the system. This connector handles authentication, retrieval of users, creation, activation, deactivation, deletion, and updating of user groups.
4+
`unipyaccess` is a Python package designed to interface with the **Unifi Access** system. This package provides a simple and efficient way to manage users in Unifi Access, including authentication, retrieval, creation, activation, deactivation, deletion, and updating of user groups.
65

7-
Disclaimer: This implementation does not use the new Unifi API. Instead, it utilizes authentication through a admin user account.
6+
> **Note**: This implementation uses Unifi API endpoints with admin user authentication. It does **not** utilize the latest, in my opinion half-baked Unifi API.
7+
8+
## Features
9+
10+
- Authenticate with Unifi Access using admin credentials.
11+
- Retrieve, create, activate, deactivate, and delete user accounts.
12+
- Update user group assignments.
13+
14+
## Installation
15+
16+
Install the package via `pip`:
17+
18+
```bash
19+
pip install unipyaccess
20+
```
821

922
## Requirements
1023

1124
- Python 3.x
1225
- `requests` library
1326

14-
To install the `requests` library, run:
27+
Install the requirements with:
28+
1529
```bash
1630
pip install requests
1731
```
1832

19-
# Class: unipyAccess
20-
## Initialization
21-
The **unipyAccess** class requires the following parameters upon initialization:
33+
## Environment Setup
2234

23-
```python
24-
unipy = unipyAccess(baseUrl, username, password, verify)
35+
Store your configuration details in a `.env` file:
36+
37+
```bash
38+
UNIFI_CONTROLLER_ADDRESS=https://unifi-controller.local
39+
UNIFI_LOGIN=admin
40+
UNIFI_PASSWORD=password123
41+
VERIFY_SSL=False
2542
```
26-
`baseUrl`: Internal URL of the Unifi controller (e.g., https://unifi-controller.local).
27-
`username`: Username of the configuration user.
28-
`password`: Password of the configuration user.
29-
`verify`: Boolean (True/False) to indicate whether SSL verification should be performed on the requests. If None, verification defaults to True.
3043

31-
**Note**: Disabling SSL verification is not recommended in production environments as it can expose your application to security risks.
44+
## Usage
3245

33-
## Example
34-
```python
35-
from unipyAccess import unipyAccess
36-
unipy = unipyAccess("https://unifi-controller.local", "unipy", "password123", verify=False)
37-
```
38-
## Methods
39-
### 1. getUnifiUsers()
40-
Retrieves the list of users from the Unifi Access system.
46+
Import `unipyaccess` and use it in your Python script:
4147

4248
```python
43-
unipy.getUnifiUsers()
44-
```
49+
from unipyaccess import UnipyAccess
50+
from dotenv import load_dotenv
51+
import os
4552

46-
Example Response:
47-
```json
48-
[
53+
load_dotenv()
54+
55+
# Initialize the UnipyAccess API client
56+
unifiApi = UnipyAccess(
57+
base_url=os.getenv('UNIFI_CONTROLLER_ADDRESS'),
58+
username=os.getenv('UNIFI_LOGIN'),
59+
password=os.getenv('UNIFI_PASSWORD'),
60+
verify=os.getenv('VERIFY_SSL')
61+
)
62+
63+
# Example: Create a new user
64+
users = [
4965
{
50-
"id": "12345",
5166
"first_name": "John",
5267
"last_name": "Doe",
53-
"employee_number": "54321"
68+
"PersonId": 124,
69+
"group_ids": ["bc1bf76d-5d2a-4a90-ae50-6aca02bccc63"]
5470
}
5571
]
72+
unifiApi.create_unifi_users(users)
5673
```
57-
### 2. createUnifiUsers(users)
58-
Creates new users in the Unifi Access system.
59-
60-
`users`: List of dictionaries, where each dictionary represents a user with the following structure:
61-
`first_name`: User's first name
62-
`last_name`: User's last name
63-
`PersonId`: Optional employee number (string)
64-
`group_ids`: Optional list of group IDs to assign to the user.
74+
75+
## Methods
76+
77+
### 1. `get_unifi_users()`
78+
Fetches the list of users from Unifi Access.
79+
80+
**Usage:**
81+
82+
```python
83+
users = unifiApi.get_unifi_users()
84+
print(users)
85+
```
86+
87+
### 2. `create_unifi_users(users)`
88+
Creates new users.
89+
90+
**Parameters:**
91+
- `users` (list): List of user dictionaries containing:
92+
- `first_name` (str): User's first name.
93+
- `last_name` (str): User's last name.
94+
- `PersonId` (str): Optional employee number.
95+
- `group_ids` (list): Optional list of group IDs.
96+
97+
**Usage:**
98+
6599
```python
66100
users = [
67-
{"first_name": "Alice", "last_name": "Smith", "PersonId": 123, "group_ids": [1, 2]},
68-
{"first_name": "Bob", "last_name": "Jones", "PersonId": 456}
101+
{"first_name": "Jane", "last_name": "Doe", "PersonId": "789", "group_ids": ["group-123"]}
69102
]
70-
unipy.createUnifiUsers(users)
103+
unifiApi.create_unifi_users(users)
104+
```
105+
106+
### 3. `deactivate_unifi_users(users)`
107+
Deactivates users.
108+
109+
**Usage:**
110+
111+
```python
112+
users = [{"id": "user-123"}]
113+
unifiApi.deactivate_unifi_users(users)
71114
```
72-
### 3. deactivateUnifiUsers(users)
73-
Deactivates the given users.
74115

75-
`users`: List of dictionaries with the following structure:
76-
`id`: User's unique ID
116+
### 4. `activate_unifi_users(users)`
117+
Activates users.
118+
119+
**Usage:**
120+
77121
```python
78-
users = [{"id": "1bae4670-c853-4740-8bbc-62ff90aaad07"}]
79-
unipy.deactivateUnifiUsers(users)
122+
users = [{"id": "user-123"}]
123+
unifiApi.activate_unifi_users(users)
80124
```
81-
### 4. activateUnifiUsers(users)
82-
Activates the given users.
83125

84-
`users`: List of dictionaries with the following structure:
85-
`id`: User's unique ID
126+
### 5. `delete_unifi_users(users)`
127+
Deletes users.
128+
129+
**Usage:**
86130

87131
```python
88-
users = [{"id": "1bae4670-c853-4740-8bbc-62ff90aaad07"}]
89-
unipy.activateUnifiUsers(users)
132+
users = [{"id": "user-123"}]
133+
unifiApi.delete_unifi_users(users)
90134
```
91-
### 5. deleteUnifiUsers(users)
92-
Deletes users from the Unifi Access system.
93135

94-
`users`: List of dictionaries with the following structure:
95-
`id`: User's unique ID
136+
### 6. `set_users_group(users)`
137+
Updates user group assignments.
138+
139+
**Usage:**
96140

97141
```python
98-
users = [{"id": "1bae4670-c853-4740-8bbc-62ff90aaad07"}]
99-
unipy.deleteUnifiUsers(users)
142+
users = [{"id": "user-123", "group": "group-456"}]
143+
unifiApi.set_users_group(users)
100144
```
101-
### 6. setUsersGroup(users)
102-
Assigns or updates the group of the specified users.
103145

104-
`users`: List of dictionaries with the following structure:
105-
`id`: User's unique ID
106-
`group`: Group ID to assign to the user.
146+
## Example Code
107147

108148
```python
149+
from unipyaccess import UnipyAccess
150+
from dotenv import load_dotenv
151+
import os
152+
153+
load_dotenv()
154+
155+
unifiApi = UnipyAccess(
156+
base_url=os.getenv('UNIFI_CONTROLLER_ADDRESS'),
157+
username=os.getenv('UNIFI_LOGIN'),
158+
password=os.getenv('UNIFI_PASSWORD'),
159+
verify=os.getenv('VERIFY_SSL')
160+
)
161+
162+
# Retrieve users
163+
print(unifiApi.get_unifi_users())
164+
165+
# Create a user
166+
new_user = [{"first_name": "Alice", "last_name": "Smith", "PersonId": "125"}]
167+
unifiApi.create_unifi_users(new_user)
168+
169+
# Activate a user
170+
unifiApi.activate_unifi_users([{"id": "user-123"}])
171+
172+
# Deactivate a user
173+
unifiApi.deactivate_unifi_users([{"id": "user-123"}])
174+
175+
# Delete a user
176+
unifiApi.delete_unifi_users([{"id": "user-123"}])
109177

110-
users = [{"id": "12345", "group": 1}]
111-
unipy.setUsersGroup(users)
178+
# Update user group
179+
unifiApi.set_users_group([{"id": "user-123", "group": "group-789"}])
112180
```
113181

114-
# License
115-
This project is licensed under the MIT License.
182+
## License
116183

117-
This README file provides an overview, usage instructions, and example code to help users understand and use the `unipyAccess` class effectively.
184+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.

requirements.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
certifi==2024.8.30
22
charset-normalizer==3.4.0
3+
docutils==0.21.2
34
idna==3.10
5+
importlib_metadata==8.5.0
6+
jaraco.classes==3.4.0
7+
jaraco.context==6.0.1
8+
jaraco.functools==4.1.0
9+
keyring==25.5.0
10+
markdown-it-py==3.0.0
11+
mdurl==0.1.2
12+
more-itertools==10.5.0
13+
nh3==0.2.18
14+
pkginfo==1.10.0
15+
Pygments==2.18.0
416
python-dotenv==1.0.1
17+
readme_renderer==44.0
518
requests==2.32.3
19+
requests-toolbelt==1.0.0
20+
rfc3986==2.0.0
21+
rich==13.9.4
22+
setuptools==75.5.0
23+
twine==5.1.1
624
urllib3==2.2.3
25+
wheel==0.45.0
26+
zipp==3.21.0

setup.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from setuptools import setup, find_packages
2+
3+
with open('README.md', 'r') as f:
4+
long_description = f.read()
5+
6+
setup(
7+
name='unipyAccess',
8+
version='0.1',
9+
packages=find_packages(),
10+
install_requires=[
11+
'requests==2.32.3'
12+
],
13+
long_description=long_description,
14+
long_description_content_type='text/markdown',
15+
)

unipyAccess/.DS_Store

6 KB
Binary file not shown.

unipyAccess/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .main import UnipyAccess

0 commit comments

Comments
 (0)