Skip to content
Snippets Groups Projects
Verified Commit ec6503cb authored by Jonas Leder's avatar Jonas Leder
Browse files

use JSON for API and make short optional

parent 35b2df03
Branches
No related tags found
No related merge requests found
Pipeline #8419 passed
import imp
from flask import jsonify, render_template from flask import jsonify, render_template
from sqlite3 import connect from sqlite3 import connect
from newurl import gen_short
def api_get(request, url_scheme, s, sAPI, passwordProtected): def api_get(request, url_scheme, s, sAPI, passwordProtected):
"returns the decoumentation for the API" "returns the decoumentation for the API"
...@@ -14,27 +16,25 @@ def api_post(request, domain, sAPI, passwordProtected, password): ...@@ -14,27 +16,25 @@ def api_post(request, domain, sAPI, passwordProtected, password):
"Handles all api requests" "Handles all api requests"
username = "error" username = "error"
try: try:
username = sAPI.loads(request.form['apikey']) username = sAPI.loads(request.json['apikey'])
print(username) print(username)
except: except:
pass pass
try: if "short" in request.json:
short = request.form['short'] short = request.json['short']
except: else:
return jsonify( short = gen_short(domain[0])
status="1",
message="short link is missing"
)
try: try:
longURL = request.form['long'] longURL = request.json['long']
except: except:
return jsonify( return jsonify(
status="2", status="2",
message="long url is missing" message="long url is missing"
) )
if "short" in request.json:
domain_ = short.split("/")[0] domain_ = short.split("/")[0]
if not domain_ in domain: if not domain_ in domain:
return jsonify( return jsonify(
...@@ -44,7 +44,7 @@ def api_post(request, domain, sAPI, passwordProtected, password): ...@@ -44,7 +44,7 @@ def api_post(request, domain, sAPI, passwordProtected, password):
if passwordProtected: if passwordProtected:
try: try:
pw = request.form["password"] pw = request.json["password"]
if( pw != password): if( pw != password):
return jsonify( return jsonify(
status="5", status="5",
...@@ -75,7 +75,8 @@ def api_post(request, domain, sAPI, passwordProtected, password): ...@@ -75,7 +75,8 @@ def api_post(request, domain, sAPI, passwordProtected, password):
) )
return jsonify( return jsonify(
status="0", status="0",
message="ok" message="ok",
url="https://{}/{}".format(domain[0], short)
) )
else: else:
return jsonify( return jsonify(
... ...
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
{% endif %} {% endif %}
<h2>Make a request</h2> <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> <ul>
<li>short: the short URL you want to get. example: {{domain}}/example</li> <li>short: the short URL you want to get. example: {{domain}}/example</li>
<li>long: the long URL you want to short</li> <li>long: the long URL you want to short</li>
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
<li>password: this value contains the password, that is needed to short a link.</li> <li>password: this value contains the password, that is needed to short a link.</li>
{% endif %} {% endif %}
</ul> </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> <h2>Responses from Server</h2>
<p>The response from the server is always made in json format it includes the following arguments:</p> <p>The response from the server is always made in json format it includes the following arguments:</p>
... ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment