tinyurl-sdk 0.1.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
tinyurl_sdk/__init__.py
ADDED
tinyurl_sdk/client.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import Optional
|
|
5
|
+
|
|
6
|
+
import httpx
|
|
7
|
+
|
|
8
|
+
from .exceptions import TinyURLClientError, TinyURLHTTPError, TinyURLResponseError
|
|
9
|
+
|
|
10
|
+
@dataclass
|
|
11
|
+
class TinyURLClient:
|
|
12
|
+
api_key: str
|
|
13
|
+
timeout: float = 10.0
|
|
14
|
+
api_base_url: str = "https://api.tinyurl.com"
|
|
15
|
+
user_agent: str = "tinyurl-sdk/0.0.0"
|
|
16
|
+
|
|
17
|
+
def shorten(
|
|
18
|
+
self,
|
|
19
|
+
url: str,
|
|
20
|
+
domain: Optional[str] = None,
|
|
21
|
+
alias: Optional[str] = None,
|
|
22
|
+
tags: Optional[str] = None,
|
|
23
|
+
expires_at: Optional[str] = None,
|
|
24
|
+
description: Optional[str] = None,
|
|
25
|
+
) -> str:
|
|
26
|
+
"""Create a TinyURL short link for the provided long URL."""
|
|
27
|
+
if not url:
|
|
28
|
+
raise ValueError("url is required")
|
|
29
|
+
if alias is not None and len(alias) < 5:
|
|
30
|
+
raise ValueError("alias must be at least 5 characters long")
|
|
31
|
+
|
|
32
|
+
return self._shorten_token(
|
|
33
|
+
url,
|
|
34
|
+
domain=domain,
|
|
35
|
+
alias=alias,
|
|
36
|
+
tags=tags,
|
|
37
|
+
expires_at=expires_at,
|
|
38
|
+
description=description,
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
def _shorten_token(
|
|
42
|
+
self,
|
|
43
|
+
url: str,
|
|
44
|
+
domain: Optional[str] = None,
|
|
45
|
+
alias: Optional[str] = None,
|
|
46
|
+
tags: Optional[str] = None,
|
|
47
|
+
expires_at: Optional[str] = None,
|
|
48
|
+
description: Optional[str] = None,
|
|
49
|
+
) -> str:
|
|
50
|
+
if not self.api_key:
|
|
51
|
+
raise TinyURLClientError("api_key is required for token API usage")
|
|
52
|
+
if alias is not None and len(alias) < 5:
|
|
53
|
+
raise ValueError("alias must be at least 5 characters long")
|
|
54
|
+
|
|
55
|
+
payload = {"url": url}
|
|
56
|
+
if domain:
|
|
57
|
+
payload["domain"] = domain
|
|
58
|
+
if alias:
|
|
59
|
+
payload["alias"] = alias
|
|
60
|
+
if tags:
|
|
61
|
+
payload["tags"] = tags
|
|
62
|
+
if expires_at:
|
|
63
|
+
payload["expires_at"] = expires_at
|
|
64
|
+
if description:
|
|
65
|
+
payload["description"] = description
|
|
66
|
+
|
|
67
|
+
url = f"{self.api_base_url.rstrip('/')}/create"
|
|
68
|
+
headers = {"Authorization": f"Bearer {self.api_key}"}
|
|
69
|
+
|
|
70
|
+
try:
|
|
71
|
+
with httpx.Client(
|
|
72
|
+
timeout=self.timeout,
|
|
73
|
+
headers={"User-Agent": self.user_agent},
|
|
74
|
+
) as client:
|
|
75
|
+
response = client.post(url, json=payload, headers=headers)
|
|
76
|
+
response.raise_for_status()
|
|
77
|
+
data = response.json()
|
|
78
|
+
except httpx.HTTPStatusError as exc:
|
|
79
|
+
raise TinyURLHTTPError(
|
|
80
|
+
f"HTTP error {exc.response.status_code}: {exc.response.reason_phrase}"
|
|
81
|
+
) from exc
|
|
82
|
+
except httpx.RequestError as exc:
|
|
83
|
+
raise TinyURLHTTPError(f"Network error: {exc}") from exc
|
|
84
|
+
except ValueError as exc:
|
|
85
|
+
raise TinyURLResponseError("Token API did not return valid JSON") from exc
|
|
86
|
+
|
|
87
|
+
tiny_url = data.get("data", {}).get("tiny_url")
|
|
88
|
+
if not tiny_url:
|
|
89
|
+
raise TinyURLResponseError("Token API response missing tiny_url")
|
|
90
|
+
|
|
91
|
+
return tiny_url
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
class TinyURLClientError(Exception):
|
|
2
|
+
"""Base error for TinyURL client operations."""
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class TinyURLHTTPError(TinyURLClientError):
|
|
6
|
+
"""Raised when the HTTP layer fails or returns a non-200 response."""
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class TinyURLResponseError(TinyURLClientError):
|
|
10
|
+
"""Raised when the API response is missing expected fields."""
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tinyurl-sdk
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Simplifying TinyURL link creation and management with a lightweight Python SDK.
|
|
5
|
+
Project-URL: Homepage, https://github.com/bilalbaraz/tinyurl-sdk
|
|
6
|
+
Project-URL: Repository, https://github.com/bilalbaraz/tinyurl-sdk
|
|
7
|
+
Author-email: Bilal Baraz <bilalbaraz@windowslive.com>
|
|
8
|
+
License: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: sdk,shortener,tinyurl,url
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Requires-Dist: httpx>=0.23.0
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# tinyurl-sdk
|
|
16
|
+
|
|
17
|
+
A lightweight Python SDK for simplifying TinyURL link creation and management.
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install tinyurl-sdk
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from tinyurl_sdk.client import TinyURLClient
|
|
29
|
+
|
|
30
|
+
client = TinyURLClient(api_key="YOUR_API_KEY")
|
|
31
|
+
short_url = client.shorten(
|
|
32
|
+
url="https://www.example.com/my-really-long-link-that-I-need-to-shorten/84378949",
|
|
33
|
+
domain="tinyurl.com",
|
|
34
|
+
alias="myexamplelink",
|
|
35
|
+
tags="example,link",
|
|
36
|
+
expires_at="2024-10-25 10:11:12",
|
|
37
|
+
description="string",
|
|
38
|
+
)
|
|
39
|
+
print(short_url)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Parameters
|
|
43
|
+
|
|
44
|
+
Constructor:
|
|
45
|
+
|
|
46
|
+
| Name | Required | Type | Notes |
|
|
47
|
+
| --- | --- | --- | --- |
|
|
48
|
+
| `api_key` | Yes | `str` | TinyURL API key. |
|
|
49
|
+
| `timeout` | No | `float` | Request timeout in seconds. Default: `10.0`. |
|
|
50
|
+
| `api_base_url` | No | `str` | Base API URL. Default: `https://api.tinyurl.com`. |
|
|
51
|
+
| `user_agent` | No | `str` | User agent header. Default: `tinyurl-sdk/0.0.0`. |
|
|
52
|
+
|
|
53
|
+
`shorten`:
|
|
54
|
+
|
|
55
|
+
| Name | Required | Type | Notes |
|
|
56
|
+
| --- | --- | --- | --- |
|
|
57
|
+
| `url` | Yes | `str` | Long URL to shorten. |
|
|
58
|
+
| `domain` | No | `str` | TinyURL domain. |
|
|
59
|
+
| `alias` | No | `str` | Must be at least 5 characters if provided. |
|
|
60
|
+
| `tags` | No | `str` | Comma-separated tags. |
|
|
61
|
+
| `expires_at` | No | `str` | Expiration timestamp (API format). |
|
|
62
|
+
| `description` | No | `str` | Link description. |
|
|
63
|
+
|
|
64
|
+
## Development
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
pip install -e .
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
MIT
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
tinyurl_sdk/__init__.py,sha256=X4v54Iet7xfzcxvie-HnoYYvQTvYq13SthKDFvJhjk0,62
|
|
2
|
+
tinyurl_sdk/client.py,sha256=aKoyYECxMNyU1fV5fxorl4zJ_tzT0dkHcAFAqpwPEtE,2982
|
|
3
|
+
tinyurl_sdk/exceptions.py,sha256=Ftma22lr0BMWpyAD4mDfGNB_9EbjBQ8QUK9e2C14LWI,326
|
|
4
|
+
tinyurl_sdk-0.1.1.dist-info/METADATA,sha256=ce4D0vwMIGU8r-3xfR8M5ib5nSXlDrIdD1QAyZGi1xE,1881
|
|
5
|
+
tinyurl_sdk-0.1.1.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
6
|
+
tinyurl_sdk-0.1.1.dist-info/licenses/LICENSE,sha256=ESYyLizI0WWtxMeS7rGVcX3ivMezm-HOd5WdeOh-9oU,1056
|
|
7
|
+
tinyurl_sdk-0.1.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|