-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.py
More file actions
215 lines (187 loc) · 6.56 KB
/
client.py
File metadata and controls
215 lines (187 loc) · 6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"""
URL Shortener Python Client
A lightweight client for the Cloudflare Workers URL Shortener API.
Setup:
pip install requests
export SHORTENER_API_KEY=your-api-key
export SHORTENER_BASE_URL=https://yourdomain.com
Usage:
from client import URLShortenerClient
client = URLShortenerClient()
short = client.create("https://example.com/very/long/url", campaign="blog_post")
print(short) # https://yourdomain.com/s/42
Full tutorial: https://www.iamdevbox.com/posts/building-self-hosted-url-shortener-cloudflare-workers/
"""
import os
import requests
from typing import Optional
class URLShortenerClient:
"""Client for the Cloudflare Workers URL Shortener API."""
def __init__(
self,
api_key: Optional[str] = None,
base_url: Optional[str] = None,
):
self.api_key = api_key or os.getenv("SHORTENER_API_KEY")
self.base_url = (base_url or os.getenv("SHORTENER_BASE_URL", "")).rstrip("/")
if not self.api_key:
raise ValueError("API key required: set SHORTENER_API_KEY env var or pass api_key=")
if not self.base_url:
raise ValueError("Base URL required: set SHORTENER_BASE_URL env var or pass base_url=")
@property
def _headers(self) -> dict:
return {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json",
}
def create(
self,
long_url: str,
code: Optional[str] = None,
campaign: str = "general",
notes: Optional[str] = None,
) -> Optional[str]:
"""
Create a new short URL.
Args:
long_url: The full URL to shorten
code: Optional custom short code (auto-generated if omitted)
campaign: UTM campaign tag (default: "general")
notes: Optional notes for admin reference
Returns:
The short URL string, or None on failure
"""
payload = {"url": long_url, "campaign": campaign}
if code:
payload["code"] = code
if notes:
payload["notes"] = notes
try:
resp = requests.post(
f"{self.base_url}/api/shorten",
json=payload,
headers=self._headers,
timeout=10,
)
resp.raise_for_status()
return resp.json()["shortUrl"]
except requests.HTTPError as e:
print(f"HTTP error creating short URL: {e.response.status_code} {e.response.text}")
except Exception as e:
print(f"Error creating short URL: {e}")
return None
def list_urls(self) -> list[dict]:
"""Return all active (non-deleted) short URLs with stats."""
try:
resp = requests.get(
f"{self.base_url}/api/urls",
headers=self._headers,
timeout=10,
)
resp.raise_for_status()
return resp.json().get("urls", [])
except Exception as e:
print(f"Error listing URLs: {e}")
return []
def get_stats(self, code: str) -> Optional[dict]:
"""Return click statistics for a short code."""
try:
resp = requests.get(
f"{self.base_url}/api/stats/{code}",
timeout=10,
)
resp.raise_for_status()
return resp.json()
except Exception as e:
print(f"Error getting stats for {code}: {e}")
return None
def update(
self,
code: str,
url: Optional[str] = None,
campaign: Optional[str] = None,
new_code: Optional[str] = None,
notes: Optional[str] = None,
) -> Optional[dict]:
"""Update an existing short URL's properties."""
payload = {}
if url:
payload["url"] = url
if campaign is not None:
payload["campaign"] = campaign
if new_code:
payload["code"] = new_code
if notes is not None:
payload["notes"] = notes
try:
resp = requests.put(
f"{self.base_url}/api/urls/{code}",
json=payload,
headers=self._headers,
timeout=10,
)
resp.raise_for_status()
return resp.json()
except Exception as e:
print(f"Error updating {code}: {e}")
return None
def delete(self, code: str) -> bool:
"""Soft-delete a short URL (can be restored later)."""
try:
resp = requests.delete(
f"{self.base_url}/api/urls/{code}",
headers=self._headers,
timeout=10,
)
resp.raise_for_status()
return resp.json().get("deleted", False)
except Exception as e:
print(f"Error deleting {code}: {e}")
return False
def restore(self, code: str) -> bool:
"""Restore a soft-deleted short URL."""
try:
resp = requests.post(
f"{self.base_url}/api/urls/{code}/restore",
headers=self._headers,
timeout=10,
)
resp.raise_for_status()
return resp.json().get("restored", False)
except Exception as e:
print(f"Error restoring {code}: {e}")
return False
def shorten_for_social(long_url: str, campaign: str = "social") -> str:
"""
Convenience wrapper: shorten a URL for social media posting.
Falls back to the original URL with a simple UTM tag on failure.
Example:
url = shorten_for_social("https://example.com/very/long/post/", "twitter")
# Returns: https://yourdomain.com/s/42 (or fallback)
"""
try:
client = URLShortenerClient()
short = client.create(long_url, campaign=campaign)
if short:
return short
except Exception:
pass
return f"{long_url}?utm_source={campaign}"
if __name__ == "__main__":
# Quick demo
import json
client = URLShortenerClient()
print("Creating a test short URL...")
short_url = client.create(
"https://www.iamdevbox.com/posts/building-self-hosted-url-shortener-cloudflare-workers/",
campaign="demo",
notes="README demo",
)
print(f"Short URL: {short_url}")
if short_url:
code = short_url.split("/s/")[-1]
print(f"\nStats for '{code}':")
print(json.dumps(client.get_stats(code), indent=2))
print("\nAll active URLs:")
for u in client.list_urls():
print(f" /s/{u['code']} → {u['url']} ({u['stats'].get('total', 0)} clicks)")