diff --git a/app/api.py b/app/api.py index 39328a9eed3aacb89c638a51bb421f1132fd8eb0..0aaa9a607613aeaa1756d7b1117830f59a1433e5 100644 --- a/app/api.py +++ b/app/api.py @@ -1,5 +1,7 @@ +import imp from flask import jsonify, render_template from sqlite3 import connect +from newurl import gen_short def api_get(request, url_scheme, s, sAPI, passwordProtected): "returns the decoumentation for the API" @@ -14,37 +16,35 @@ def api_post(request, domain, sAPI, passwordProtected, password): "Handles all api requests" username = "error" try: - username = sAPI.loads(request.form['apikey']) + username = sAPI.loads(request.json['apikey']) print(username) except: pass - try: - short = request.form['short'] - except: - return jsonify( - status="1", - message="short link is missing" - ) + if "short" in request.json: + short = request.json['short'] + else: + short = gen_short(domain[0]) try: - longURL = request.form['long'] + longURL = request.json['long'] except: return jsonify( status="2", message="long url is missing" ) - domain_ = short.split("/")[0] - if not domain_ in domain: - return jsonify( - status="3", - message="domain for short link is not in allowed domain list" - ) + if "short" in request.json: + domain_ = short.split("/")[0] + if not domain_ in domain: + return jsonify( + status="3", + message="domain for short link is not in allowed domain list" + ) if passwordProtected: try: - pw = request.form["password"] + pw = request.json["password"] if( pw != password): return jsonify( status="5", @@ -75,7 +75,8 @@ def api_post(request, domain, sAPI, passwordProtected, password): ) return jsonify( status="0", - message="ok" + message="ok", + url="https://{}/{}".format(domain[0], short) ) else: return jsonify( diff --git a/app/templates/apiDocs.html b/app/templates/apiDocs.html index 7c05bab08e7c5f1beff823453e92bc7fa0a1aceb..ff726c37489e27223b2a2968c1f2929e43e2d8e6 100644 --- a/app/templates/apiDocs.html +++ b/app/templates/apiDocs.html @@ -14,7 +14,7 @@ {% endif %} <h2>Make a request</h2> - <p>The usage of the API is very simple, you have to make a push request to {{domain}}/user/api which have to include the folowing Arguments:</p> + <p>The usage of the API is very simple, you have to make a POST request to {{domain}}/user/api which have to include the folowing Arguments as JSON Body:</p> <ul> <li>short: the short URL you want to get. example: {{domain}}/example</li> <li>long: the long URL you want to short</li> @@ -23,7 +23,7 @@ <li>password: this value contains the password, that is needed to short a link.</li> {% endif %} </ul> - <p>example API call with curl: <i>curl -d "{% if apikey %}apikey={{apikey}}&{% endif %}short={{domain}}/example&long=http://example.com" -X POST {{url_scheme}}://{{domain}}/user/api</i> + <p>Except long all parameters are optional.</p> <h2>Responses from Server</h2> <p>The response from the server is always made in json format it includes the following arguments:</p>