1
1
# Web API Overview
2
2
3
3
The BusyLight HTTP API provides RESTful endpoints for controlling USB LED
4
- lights. The API returns JSON responses and supports the same functionality
5
- as the command-line interface .
4
+ lights. Built with FastAPI, it features proper HTTP methods, domain-based
5
+ organization, API versioning, and comprehensive OpenAPI documentation .
6
6
7
7
## Quick Start
8
8
@@ -25,82 +25,107 @@ busyserve -D
25
25
### Basic Usage
26
26
27
27
``` bash
28
- # Get server status
28
+ # Get API information
29
29
curl http://localhost:8000/
30
30
31
- # Turn light on (green)
32
- curl http://localhost:8000/light/0/on
31
+ # Check system health
32
+ curl http://localhost:8000/system/health
33
33
34
- # Turn light red
35
- curl " http://localhost:8000/light/0/on?color=red"
34
+ # Turn light on (green) - REST endpoint
35
+ curl -X POST http://localhost:8000/api/v1/lights/0/on \
36
+ -H " Content-Type: application/json" \
37
+ -d ' {"color": "green", "dim": 1.0, "led": 0}'
36
38
37
- # Blink light blue 3 times
38
- curl " http://localhost:8000/light/0/blink ?color=blue&count=3 "
39
+ # Turn light red - backwards compatible
40
+ curl " http://localhost:8000/light/0/on ?color=red "
39
41
40
- # Turn light off
41
- curl http://localhost:8000/light/0/off
42
+ # Start rainbow effect
43
+ curl -X POST http://localhost:8000/api/v1/effects/rainbow \
44
+ -H " Content-Type: application/json" \
45
+ -d ' {"dim": 0.8, "speed": "fast"}'
42
46
```
43
47
44
48
## API Features
45
49
46
- - ** RESTful design** with consistent JSON responses
50
+ - ** Domain-based organization** - lights, effects, system endpoints
51
+ - ** API versioning** - v1 endpoints with backwards compatibility
52
+ - ** Proper HTTP methods** - POST for actions, GET for queries
53
+ - ** JSON request/response** with Pydantic validation
47
54
- ** Device targeting** by index or all devices
48
55
- ** LED targeting** for multi-LED devices
49
- - ** Color specification** via query parameters
50
- - ** Effect control** with customizable parameters
51
56
- ** Authentication** via HTTP Basic Auth (optional)
52
57
- ** CORS support** for browser-based applications
53
58
- ** Interactive documentation** at ` /docs ` and ` /redoc `
59
+ - ** OpenAPI 3.0** specification for code generation
60
+
61
+ ## API Endpoints
62
+
63
+ The API provides multiple access patterns:
54
64
55
- ## Base URL
65
+ ### Versioned API (Recommended)
66
+ ```
67
+ http://localhost:8000/api/v1/
68
+ ```
56
69
70
+ ### Backwards Compatible
57
71
```
58
- http://localhost:8000
72
+ http://localhost:8000/
59
73
```
60
74
61
75
Default host is ` localhost ` and port is ` 8000 ` . Configure with command-line
62
76
options:
63
77
64
78
``` bash
65
- busyserve --host 0.0.0.0 --port 8080
79
+ busyserve --host 0.0.0.0 --port 8080 --debug # Enable debug logging
66
80
```
67
81
68
82
## Response Format
69
83
70
84
All endpoints return JSON responses with consistent structure:
71
85
72
- ### Success Response
86
+ ### Light Operation Response
73
87
74
88
``` json
75
89
{
76
- "action" : " on" ,
77
- "light_id" : 0 ,
78
- "color" : " red" ,
79
- "rgb" : [255 , 0 , 0 ],
80
- "led" : 0
90
+ "success" : true ,
91
+ "action" : " turned on" ,
92
+ "lights_affected" : 1 ,
93
+ "details" : [
94
+ {
95
+ "light_id" : 0 ,
96
+ "action" : " turned on" ,
97
+ "color" : " red" ,
98
+ "rgb" : [255 , 0 , 0 ],
99
+ "dim" : 1.0 ,
100
+ "led" : 0
101
+ }
102
+ ]
81
103
}
82
104
```
83
105
84
106
### Error Response
85
107
86
108
``` json
87
109
{
88
- "message " : " Light index 5 not found"
110
+ "detail " : " Light index 5 not found"
89
111
}
90
112
```
91
113
92
114
## Content Types
93
115
94
- - ** Request** : Query parameters or JSON body
95
- - ** Response** : ` application/json `
116
+ - ** V1 Endpoints** : JSON request/response (` application/json ` )
117
+ - ** Compatibility Endpoints** : Query parameters with JSON response
118
+ - ** All Responses** : ` application/json `
96
119
97
120
## Status Codes
98
121
99
122
| Code | Description |
100
123
| ------| -------------|
101
124
| 200 | Success |
125
+ | 401 | Authentication required |
102
126
| 404 | Device not found |
103
- | 422 | Invalid parameter |
127
+ | 422 | Validation error |
128
+ | 503 | No lights available |
104
129
| 500 | Server error |
105
130
106
131
## Authentication
@@ -121,8 +146,13 @@ busyserve
121
146
### Usage
122
147
123
148
``` bash
124
- # Authenticated request
125
- curl -u admin:secret http://localhost:8000/light/0/on
149
+ # V1 authenticated request
150
+ curl -u admin:secret -X POST http://localhost:8000/api/v1/lights/0/on \
151
+ -H " Content-Type: application/json" \
152
+ -d ' {"color": "red"}'
153
+
154
+ # Compatibility authenticated request
155
+ curl -u admin:secret http://localhost:8000/light/0/on? color=red
126
156
```
127
157
128
158
!!! warning "Security Warning"
@@ -165,20 +195,70 @@ These interfaces allow testing endpoints directly from the browser.
165
195
The API does not impose rate limiting, but devices may have physical
166
196
limitations on how quickly they can change colors or effects.
167
197
168
- ## Health Check
198
+ ## System Endpoints
169
199
170
200
``` bash
171
- # Check server status
201
+ # API information
172
202
curl http://localhost:8000/
173
203
174
- # Response
175
- [
176
- {" path" : " /" },
177
- {" path" : " /lights" },
178
- {" path" : " /light/{light_id}" }
179
- ]
204
+ # System health check
205
+ curl http://localhost:8000/system/health
206
+
207
+ # System information
208
+ curl http://localhost:8000/system/info
180
209
```
181
210
211
+ ### Health Response
212
+ ``` json
213
+ {
214
+ "status" : " healthy" ,
215
+ "lights_available" : 2 ,
216
+ "timestamp" : " 2023-01-01T12:00:00Z"
217
+ }
218
+ ```
219
+
220
+ ## Backwards Compatibility
221
+
222
+ The API maintains full backwards compatibility with existing integrations:
223
+
224
+ - ** Original endpoints** work unchanged (GET requests with query parameters)
225
+ - ** Original response format** preserved for compatibility endpoints
226
+ - ** Gradual migration** - use v1 endpoints for new features, keep existing code working
227
+ - ** Deprecation warnings** in OpenAPI docs for legacy endpoints
228
+
229
+ ### Migration Path
230
+
231
+ 1 . ** Keep existing code working** - no immediate changes needed
232
+ 2 . ** New features** - use ` /api/v1/ ` endpoints with JSON payloads
233
+ 3 . ** Gradual adoption** - migrate endpoints one at a time
234
+ 4 . ** Full migration** - eventually adopt v1 endpoints for all operations
235
+
236
+ ## Logging
237
+
238
+ The API uses integrated logging that properly displays both FastAPI and uvicorn events:
239
+
240
+ ``` bash
241
+ # Normal logging
242
+ busyserve
243
+
244
+ # Debug logging with detailed output
245
+ busyserve --debug
246
+ ```
247
+
248
+ ** Features:**
249
+ - ** Integrated logging** - All API, uvicorn, and application events appear in a unified log
250
+ - ** Color-coded output** - Different log levels are visually distinct
251
+ - ** Request tracing** - HTTP requests and responses are logged when debug is enabled
252
+ - ** Structured format** - Timestamps, levels, module names, and line numbers included
253
+
254
+ ** Log Levels:**
255
+ - ** INFO** : Server startup, configuration, API operations
256
+ - ** DEBUG** : Detailed request/response data, internal operations
257
+ - ** WARNING** : Configuration issues, recoverable errors
258
+ - ** ERROR** : Request failures, server errors
259
+
260
+ ** Previous Issue Resolved:** Earlier versions had logging compatibility issues between loguru and uvicorn - this has been fixed with integrated logging handlers.
261
+
182
262
## Error Handling
183
263
184
264
The API provides detailed error messages for troubleshooting:
0 commit comments