APIs to convert long URLs to a shortened format which can be easily shared with everyone.
The short URL would be of the following format - http://domainname.com/hash/
fetch/short-url/
This API will accept a long URL, verify whether it is a valid URL and returns a short URL for it.
Note: http://www.hackerearth.com/challenges is a valid URL but hackerearth.com is not a valid URL.
Request Body -
{
"long_url": "https://www.hackerearth.com/challenge/hiring/hackerearth-python-developer-hiring-challenge/"
}
Response Data -
{
"short_url": "hck.re/7t654y3w",
"status": "OK",
"status_codes": []
}
If the action is successful, the status will be OK and status_code will an empty list. Else, the status will be FAILED.
If the URL is not a valid URL, then the following response will be returned -
{
"status": "FAILED",
"status_codes": ["INVALID_URLS"]
}
fetch/long-url/
This API will accept a short URL, verify whether it is a valid short URL and returns the corresponding long URL for it.
Here, a valid short URL means it is a URL generated by the URL Shortener.
Request Body -
{
"short_url": "hck.re/7t654y3w"
}
Response Data -
{
"long_url": "https://www.hackerearth.com/challenge/hiring/hackerearth-python-developer-hiring-challenge/",
"status": "OK",
"status_codes": []
}
If the action is successful, the status will be OK and status_code will an empty list. Else, the status will be FAILED.
If the URL is not a valid short URL, then the following response will be returned -
{
"status": "FAILED",
"status_codes": ["SHORT_URLS_NOT_FOUND"]
}
The endpoint for this will be -
fetch/short-urls/
This API will accept a list of long URLs, verify whether they are valid URLs and return generated short URLs for them. Valid URLs follow the same concept as explained above.
Request Body -
{
"long_urls": ["https://www.hackerearth.com/challenge/hiring/hackerearth-python-developer-hiring-challenge/"]
}
Response Data -
{
"short_urls": {"https://www.hackerearth.com/challenge/hiring/hackerearth-python-developer-hiring-challenge/": "hck.re/er345t6"},
"invalid_urls" : [],
"status": "OK",
"status_codes": []
}
If the action is successful, the status will be OK and status_code will an empty list. Else, the status will be FAILED.
Example of a FAILED request -
{
"long_urls": ["hackerearth.com/challenge/hiring/hackerearth-python-developer-hiring-challenge/", "https://www.google.com/"]
}
Expected Response -
{
"invalid_urls" : ["hackerearth.com/challenge/hiring/hackerearth-python-developer-hiring-challenge/"],
"status": "FAILED",
"status_codes": ["INVALID_URLS"]
}
fetch/long-urls/
This API will accept a list of short URLs, verify whether they are valid short URLs and return generated short URLs for them.
Valid short URLs follow the same concept as explained above.
Request Body -
{
"short_urls": ["hck.re/er345t6"]
}
Response Data -
{
"long_urls": {"hck.re/er345t6": "https://www.hackerearth.com/challenge/hiring/hackerearth-python-developer-hiring-challenge/"},
"invalid_urls": [],
"status": "OK",
"status_codes": []
}
If the action is successful, the status will be OK and status_code will an empty list. Else, the status will be FAILED.
Example for a failed request -
If two URLs hck.re/qw3245r and hck.re/rt4533e are given to the API out of which only the first one is a valid short URL, then the following response is expected -
{
"invalid_urls": ["hck.re/rt4533e"],
"status": "FAILED",
"status_codes": ["SHORT_URLS_NOT_FOUND"]
}
{short_url_hash}/
If someone accesses the short URL, this API will be accessed and they will be redirected to the actual URL.
A 302 Response Code i.e redirect is expected.
fetch/count/
This API will return the number of times a short URL was accessed.
Request Body -
{
"short_url": "hck.re/er345t6"
}
Response Data -
{
"count": "3",
"status": 'OK',
"status_codes": "[]"
}
If the action is successful, the status will be OK and status_code will an empty list. Else, the status will be FAILED.
clean-urls/
This API will clear the database of all the previous test data. This needs to be implemented to make sure that database does not face any integrity error in multiple iterations of the tests,
200 - Request successful
404 - Page not found
500 - Server not found
302 - Redirect
INVALID_URLS - Invalid long URLs are given to the data
SHORT_URLS_NOT_FOUND - Short URLs does not exist
BAD_DATA - Data not provided in the specified format or arguments are missing
OK - Request executed successfully
FAILED - Request failed
- Django 2.0
- Python3
git clone https://github.com/satheesh1997/drf-urlshortener-interview.git
clone the project to a folder and move to that folder
cd <folder_name>
Install the requirements
pip install -r requirements.txt
Migrate the database and start the server
python3 manage.py makemigrations && python3 manage.py migrate
Start the Server
python3 manage.py runserver
--
Server will running on port 8000.