Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 2bec4f8

Browse files
feat: URL shortener service that engineers can run via this repo or copy/paste to their own projects.
1 parent e51b067 commit 2bec4f8

File tree

6 files changed

+84
-0
lines changed

6 files changed

+84
-0
lines changed

projects/url_shortener/app/__init__.py

Whitespace-only changes.

projects/url_shortener/app/main.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import os
2+
import sys
3+
4+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
5+
6+
from app.models.url_shortener import UrlShortenerValidation
7+
from app.services.url_shortener import URLShortener
8+
9+
10+
def run_url_shortener():
11+
entered_url = input("Enter the URL you want to shorten: ")
12+
13+
# Validate the entered URL
14+
url_validation = UrlShortenerValidation(url=entered_url)
15+
16+
# Extract the validated URL and convert it to string
17+
validated_url = str(url_validation.url)
18+
19+
# Pass the string URL to URLShortener
20+
url_shortener = URLShortener(url=validated_url)
21+
22+
result = url_shortener.shorten_url()
23+
if isinstance(result, str):
24+
print(f"Shortened URL: {result}")
25+
else:
26+
print(f"Error occurred: {result}")
27+
return result
28+
29+
if __name__ == "__main__":
30+
run_url_shortener()
31+
# run_url_shortener_with_other_library()
32+
33+
# def run_url_shortener_with_other_library():
34+
# entered_url = input("Enter the URL you want to shorten: ")
35+
36+
# # Validate the entered URL
37+
# url_validation = UrlShortenerValidation(url=entered_url)
38+
39+
# # Extract the validated URL and convert it to string
40+
# validated_url = str(url_validation.url)
41+
42+
# # Pass the string URL to URLShortener
43+
# url_shortener = URLShortener(url=validated_url, shortener=None) # enter the shortener you want to use
44+
45+
# result = url_shortener.shorten_url()
46+
# if isinstance(result, str):
47+
# print(f"Shortened URL: {result}")
48+
# else:
49+
# print(f"Error occurred: {result}")
50+
# return result

projects/url_shortener/app/models/__init__.py

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from pydantic import BaseModel, HttpUrl
2+
3+
4+
class UrlShortenerValidation(BaseModel):
5+
url: HttpUrl

projects/url_shortener/app/services/__init__.py

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from configparser import ParsingError
2+
from typing import Optional, Union
3+
4+
import pyshorteners
5+
from pydantic import BaseModel, PrivateAttr, ValidationError
6+
7+
8+
class URLShortener(BaseModel):
9+
url: str
10+
_shortener: Optional[pyshorteners.Shortener] = PrivateAttr()
11+
12+
class Config:
13+
arbitrary_types_allowed = True
14+
15+
def __init__(self, **data):
16+
shortener = data.pop("shortener", None) or pyshorteners.Shortener()
17+
super().__init__(**data)
18+
self._shortener = shortener
19+
20+
def shorten_url(self) -> Union[str, Exception]:
21+
if self._shortener is None:
22+
raise ValueError("Shortener is not initialized.")
23+
24+
try:
25+
return str(self._shortener.tinyurl.short(self.url))
26+
except (ParsingError, ValidationError) as error:
27+
return error
28+
except Exception as generic_error:
29+
return generic_error

0 commit comments

Comments
 (0)