Python Backend Assignment Prompt Write a server that acts as a bit.ly-like URL shortener. The primary interface should be a JSON API that allows the following: Create a random short link for arbitrary...

1 answer below »
Prompt
Write a server that acts as a bit.ly-like URL shortener. The primary interface should
be a JSON API that allows the following:
Create a random short link for arbitrary URLs, e.g., bit.ly/2Fh


Python Backend Assignment Prompt Write a server that acts as a bit.ly-like URL shortener. The primary interface should be a JSON API that allows the following: Create a random short link for arbitrary URLs, e.g., bit.ly/2Fh�hXh Once a random short link has been generated, further requests should generate the same random short link Allow creating custom short links to arbitrary URLs, e.g., bit.ly/my-custom-link Multiple custom short links may map to the same URL Provide a route for returning stats on a given short link, including: When the short link was created How many times the short link has been visited total A count of the number of visits to the short link per day (the exact data structure + formatting of this is up to you) The server should also handle redirecting short links it creates to the original URLs Everything else is up to you: the routes for the API, what its parameters and return values are, how it handles errors. We would be happy to answer questions on any of these things, but you are empowered to make whatever design decisions you like (and ideally explain them!). Submission The goal should be to have a functioning server that can be run locally on a computer. Code should be submitted as a Git repo, with an initial commit when you start working. We don’t expect you to spend more than four hours on this problem, though we won’t strictly time you. At the end it should be as close to deployable as possible. If you come onsite, you should be prepared to demo what you built and review it together. Expectations Readme. Your solution should include a top-level readme which (1) describes how to run your app and (2) explains the design decisions you made while architecting and coding the app.  Code. The final code should be clean and easy to read, extensible, and narrowly scoped to the use-case. We don’t expect any fancy abstractions, or for you to predict what features we’d want to add, but the code shouldn’t be painfully difficult to extend if we needed to either. Architecture + scaling. You should be able to talk through your architecture + explain the issues you would expect to face as you scale your solution. Comments. We expect your code should mostly speak for itself, but encourage commenting anything that might be confusing or nonobvious, as would be needed in a collaborative, deadline-driven environment.  Tests. Key requirements should be tested in a reasonable way. That said we don’t expect a full suite of unit, integration and acceptance tests, nor do you need to practice test-driven-development. Language + technology. You can write the server in any language and using any technologies you choose, including your choice of database. There are no limitations on using open-source libraries, though please explain your choices in the readme. Help. Feel free to use the internet or whatever resources you need. The code itself should, of course, be your own. Extra credit If you have extra time, implement any of the following features for extra credit. You don’t need to do any of these to pass, but implementing any will make it more likely. Deploy the server to an accessible URL Number of “unique” visitors to a given short link (you can define how we track visitors) A “global stats” endpoint, that aggregates some interesting stats across your URL-shortening platform (e.g., number of links/visits per domain, total visits across the site per day, etc.)
Answered Same DayAug 27, 2021

Answer To: Python Backend Assignment Prompt Write a server that acts as a bit.ly-like URL shortener. The...

Swapnil answered on Aug 28 2021
119 Votes
90251/Code/__init__.py
pass
90251/Code/constants.py
templateEntry = {
'attendees': [],
}
caches = {
}
90251/Code/my_flask.py
import json
import logging
from flask import Flask, Response, redirect
from fla
sk import request
from flask_cors import CORS
from gplz.demo import shorten
demo = Flask(__name__)
demo.config.from_object('flask_config')
CORS(demo)
logging.getLogger('flask_cors').level = logging.DEBUG
@demo.route('/ops/shorten', methods=['POST'])
def handle_shorten():
data = request.get_json()
url = data.get('url', 'NO URL')
res = shorten.shorten(url)
return Response(json.dumps(res), mimetype='application/json')
@demo.route('/ops/custom', methods=['POST'])
def handle_custom():
data = request.get_json()
url = data.get('url', 'NO URL')
shortcode = data.get('shortcode', 'NO SHORTCODE')
res = shorten.custom(url, shortcode)
return Response(json.dumps(res), mimetype='application/json')
@demo.route('/ops/lookup', methods=['POST'])
def handle_lookup():
data = request.get_json()
url = data.get('shortcode', 'NO URL')
res = shorten.lookup(url)
return Response(json.dumps(res['url']),
mimetype='application/json')
@demo.route('/ops/dump', methods=['GET'])
def handle_dump():
res = [dict(e) for e in shorten.dump()]
return Response(json.dumps(res), mimetype='application/json')
@demo.route('/', methods=['GET'])
def handle_redirect(path):
res = shorten.lookup(path)
url = res['url']
return redirect(url)
if __name__ == "__main__":
demo.run()
90251/Code/shorten.py
from urllib.parse import urlparse
import hashlib
import datetime
import string
import random
random.seed('x')
URLLEN = 10
CHOICES = string.printable[:62]
_cache = {}
_shortcodes = {}
def new_shortcode():
while True:
shortcode = ''.join([random.choice(CHOICES) for e in range(URLLEN)])
if shortcode not in _shortcodes:
return shortcode
def new_sha(url):
m = hashlib.sha256()
m.update(url.encode('utf8'))
sha = m.digest()
return sha
def check_url(url):
res = urlparse(url)
if not res.scheme or not...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here