-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
73 lines (52 loc) · 2.05 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from flask import Flask, request, Response, json, redirect, url_for
import hashlib
from dicttoxml import dicttoxml
# Initialization of Flask application, you can add here a secret key
app = Flask(__name__)
app.secret_key = "super secret key"
# Dictionary initialization
url_storage = {}
# Shorten endpoint
@app.route('/api/v1/shorten/<path:subpath>', methods=['POST'])
def shorten(subpath):
url = subpath
# Check if the URL starts with https or http
if not url.startswith(("http://", "https://")):
return Response("http:// or https:// needed ", 400)
# Hash the URL with SHA256
hs = hashlib.sha256(url.encode('utf-8')).hexdigest()
# Store it to the dictionary
url_storage.update({hs[0:10]:url})
# Create the shortened URL
short_url = "https://domain.ltd/" + hs[0:10]
# Create the data to be sent as response
data = {"url" : short_url}
# Return the XML Response if needed
if request.content_type == 'application/xml':
xmldata = dicttoxml(data, custom_root='data', attr_type=False)
return Response(xmldata, mimetype='application/xml')
# Standard JSON response
return Response(json.dumps(data), mimetype='application/json')
# Lookup endpoint creation
@app.route('/api/v1/lookup/<identifier>', methods=['GET'])
def lookup(identifier):
# Lookup of the key to the dictionary
try:
lookup_key = url_storage[identifier]
data = {"original_url" : lookup_key}
except KeyError:
return Response("No url found!", 400)
# XML Response if needed
if request.content_type == 'application/xml':
xmldata = dicttoxml(data, custom_root='data', attr_type=False)
return Response(xmldata, mimetype='application/xml')
# Standard JSON response
return Response(json.dumps(data), mimetype='application/json')
# Redirect endpoint
@app.route('/<id>', methods=['GET'])
def original(id):
try:
original_url = url_storage[id]
return redirect(original_url)
except KeyError:
return Response("No url found!", 400)