Code Monkey home page Code Monkey logo

exercises's Introduction

sweeft digital challenges

Build Status

Project Installation

Without Doker

URL shortener requires Python3 v3.6+ to run.

Install the dependencies and start the server.

cd project
pip install -r requirements.txt
python app.py

Note: Since I use sqlite3 we don't need to configure a database, project comes with the db file, anyways project comes with the SQL schema, that can be used to build everything, but still it's not needed to run the project

Note: you can use test_url_shortener.py file located in the project folder to test project against various UNITTEST cases

Docker

Url Shortener is very easy to install and deploy in a Docker container.

By default, the Docker will expose port 8080, so change this within the Dockerfile if necessary. When ready, simply use the Dockerfile to build the image.

cd project
docker build -t url-shortener .

This will create the url shortener image and pull in the necessary dependencies.

Once done, run the Docker image and map the port to whatever you wish on your host. In this example, we simply map port 5000 of the host to port 5000 of the Docker (or whatever port was exposed in the Dockerfile):

docker run -d -p 5000:5000 url-shortener

Verify the deployment by navigating to your server address in your preferred browser.

127.0.0.1:5000/url_shortener

How to use

Url shortener is a simple app that can be used to shorten sizes of URL's

It can provide several kind of functionalities, such as shorten the URL, have custom URL option for a premium clients, get statistical data about all of the URL's, such as a counters and ability to delete URL's after certain days

API call example

URL shortener gives user ability to provide data as a JSON as well as a normal HTTP request, let's start with the cURL example


Example 1:


Let's provide JSON data to API to shorten some urls ๐Ÿ˜Ž

curl --location --request POST 'http://127.0.0.1:5000/shorten_url' \
--header 'Content-Type: application/json' \
--data-raw '{
    "url": "http://google.com"
}'

In this case API will return:

{
    "original_url": "http://google.com",
    "short_url": "http://127.0.0.1:5000/url/fDo=",
    "success": true
}

Example 2


In case if we want to have custom URL we have to use special key value pair, see the example below

{
    "url": "http://google.com",
    "premium": true,
    "custom_url": "unclegrandpa"
}

Which will return:

{
    "original_url": "http://google.com",
    "short_url": "http://127.0.0.1:5000/url/unclegrandpa",
    "success": true
}

lets move on to the HTTP response example


Example 1:


<Own IP adress>/shorten_url?url=https://fox.com

Here we can see that API accepts input from user directly from the address bar :)

and here is what it receives

{
    "original_url": "http://google.com",
    "short_url": "http://127.0.0.1:5000/url/unclegrandpa",
    "success": true
}

as we can see returned data is pretty much same to the first cURL option

Example 2


let's see example in case of a premium user

http://127.0.0.1:5000/shorten_url?url=https://fox.com&premium=true&custom_url=love_the_news

which returns:

{
    "original_url": "https://fox.com",
    "short_url": "http://127.0.0.1:5000/url/love_the_news",
    "success": true
}

In case if we are interested in stats we have special API for it which can be accessed by both cURL and normal HTTP request

Let's start with the cURL variant


Example 1


Here we request for a stats about certain shortened URL

curl --location --request POST 'http://127.0.0.1:5000/stats/' \
--header 'Content-Type: application/json' \
--data-raw '{
    "stats": "http://127.0.0.1:5000/url/love_the_news"
}'

and as a result we get:

{
    "days_left": 30,
    "visitors": 3
}

which consists of visitors counter and also of a total days left till the deletion of shown URL

Example 2


and in the end let's see some stats API examples in case if we use a HTTP request
http://127.0.0.1:5000/stats/?url=http://127.0.0.1:5000/url/love_the_news

Which will return same result as it was in case of cURL:


{
    "days_left": 30,
    "visitors": 3
}

Have a nice day/night ;)


License

MIT

Free Software, Hell Yeah!

exercises's People

Contributors

michael-malania avatar

Watchers

 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.