120 lines
3.4 KiB
Markdown
120 lines
3.4 KiB
Markdown
# Subtask: URL Shortening API Endpoint
|
|
|
|
**Parent Task**: Decomp/001
|
|
**Subtask ID**: Decomp/001-05
|
|
**Dependencies**: Decomp/001-03 (Short Code Generation)
|
|
|
|
## Description
|
|
|
|
Implement REST API endpoint for creating short URLs. Supports both auto-generated and custom short codes, with optional TTL configuration.
|
|
|
|
## Requirements
|
|
|
|
- Accept POST requests with original URL
|
|
- Return short code in JSON response
|
|
- Support custom alias parameter
|
|
- Support TTL (time-to-live) parameter
|
|
- Validate URL format
|
|
- Rate limit to prevent abuse
|
|
|
|
## Technical Specifications
|
|
|
|
**API endpoint**: `POST /api/shorten`
|
|
|
|
**Request format**:
|
|
```json
|
|
{
|
|
"url": "https://example.com/very/long/path",
|
|
"custom_alias": "mylink", // optional
|
|
"ttl": 86400 // optional, seconds
|
|
}
|
|
```
|
|
|
|
**Response format**:
|
|
```json
|
|
{
|
|
"short_code": "abc123",
|
|
"short_url": "https://shrt.io/abc123",
|
|
"original_url": "https://example.com/very/long/path",
|
|
"created_at": "2025-02-04T12:00:00Z",
|
|
"expires_at": "2025-02-05T12:00:00Z"
|
|
}
|
|
```
|
|
|
|
**Validation rules**:
|
|
- `url`: required, valid HTTP/HTTPS URL, max 2048 chars
|
|
- `custom_alias`: optional, 3-8 chars, alphanumeric + hyphen, no reserved words
|
|
- `ttl`: optional, 0-31536000 seconds (max 1 year), 0 = no expiration
|
|
|
|
**Error responses**:
|
|
```json
|
|
// 400 Bad Request
|
|
{ "error": "invalid_url", "message": "URL must start with http:// or https://" }
|
|
|
|
// 409 Conflict
|
|
{ "error": "alias_exists", "message": "Custom alias 'mylink' is already taken" }
|
|
|
|
// 429 Too Many Requests
|
|
{ "error": "rate_limit", "message": "Rate limit exceeded: 100 req/min" }
|
|
```
|
|
|
|
**Rate limiting**:
|
|
- Token bucket algorithm: 100 requests/minute per IP
|
|
- Redis-backed for distributed rate limiting
|
|
- Sliding window for accurate throttling
|
|
|
|
**API signatures**:
|
|
```python
|
|
def create_shortened_url(request: Request) -> Response:
|
|
url = request.json.get("url")
|
|
custom_alias = request.json.get("custom_alias")
|
|
ttl = request.json.get("ttl")
|
|
|
|
# Validate
|
|
validation_errors = validate_request(url, custom_alias, ttl)
|
|
if validation_errors:
|
|
return Response(validation_errors, status=400)
|
|
|
|
# Check rate limit
|
|
if not rate_limit_check(request.remote_addr):
|
|
return Response({"error": "rate_limit"}, status=429)
|
|
|
|
# Create short URL
|
|
short_code = create_short_url(url, custom_alias, ttl)
|
|
|
|
return Response({
|
|
"short_code": short_code,
|
|
"short_url": f"{BASE_URL}/{short_code}",
|
|
"original_url": url,
|
|
...
|
|
})
|
|
```
|
|
|
|
## Acceptance Criteria
|
|
|
|
- [ ] Valid URL returns 201 with short code
|
|
- [ ] Invalid URL format returns 400
|
|
- [ ] Duplicate URL returns existing short code (idempotent)
|
|
- [ ] Custom alias conflict returns 409
|
|
- [ ] TTL sets correct expiration timestamp
|
|
- [ ] Rate limit enforced at 100 req/min per IP
|
|
- [ ] Response includes all required fields
|
|
|
|
## Notes
|
|
|
|
**Trade-offs**:
|
|
- Rate limiting per IP: simple but can be bypassed with proxies
|
|
- Duplicate URL returns same code: saves space but reduces analytics granularity
|
|
- TTL in seconds: more flexible than presets
|
|
|
|
**Edge cases**:
|
|
- URL with fragments (#): preserve in stored URL
|
|
- URL with query params: preserve exactly as provided
|
|
- Internationalized domains (IDN): store as Punycode
|
|
- TTL = 0: means no expiration (NULL in DB)
|
|
|
|
**Alternatives considered**:
|
|
- Rate limiting per API key: rejected, adds auth requirement
|
|
- Hash-based codes: rejected, less predictable, collision risk
|
|
- TTL presets (1h/1d/1w): rejected, less flexible
|