Giter Club home page Giter Club logo

hck-interview-project's Introduction

Django Url Shotener

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/

Endpoints

1. Shorten Single Url
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"]
}
2. Fetch the long URL for a given short URL
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"]
}
3. Shorten list of URLs

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"]
}
4. Fetch the long URLs for a list of short 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"]
}
5. Short URL Server
{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.

6. Fetch the number of times as short URL was used to access the long URL
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.

6. Clean the database for multiple iterations of tests
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,

Response Codes

200 - Request successful
404 - Page not found
500 - Server not found
302 - Redirect

Status Codes

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

Status

OK - Request executed successfully
FAILED - Request failed

Requirements

  1. Django 2.0
  2. Python3

Installation

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.

hck-interview-project's People

Contributors

satheesh1997 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.