diff --git a/main.py b/main.py index bddb54223906f9769568e75a4ee6c2ab404b7240..73d2ce67b704c57a5151ba36b69f94340dee212e 100644 --- a/main.py +++ b/main.py @@ -344,26 +344,23 @@ def api(): short = request.form['short'] except: return jsonify( - status="400", - errorCode="1", - Message="short link missing" + status="1", + message="short link missing" ) try: longURL = request.form['long'] except: return jsonify( - status="400", - errorCode="2", - Message="link to short is missing" + status="2", + message="link to short is missing" ) domain = short.split("/")[0] if not domain in domains: return jsonify( - status="400", - errorCode="3", - Message="domain for short link is not in allowed domain list" + status="3", + message="domain for short link is not in allowed domain list" ) with sqlite3.connect('db/urls.db') as conn: #Check if another user already used the short link @@ -383,23 +380,23 @@ def api(): [longURL, short, username] ) return jsonify( - status="200", - Message="ok" + status="0", + message="ok" ) else: return jsonify( - status="400", - errorCode="4", - Message="short url already in use" + status="4", + message="short url already in use" ) @app.route('/user/api', methods=['GET']) def apiDocs(): try: - userID = s.loads(request.cookies.get('userID')) + userID = sAPI.dumps(s.loads(request.cookies.get('userID'))) except: - userID = "not logged in" - return render_template("apiDocs.html", apikey=sAPI.dumps(userID)) + userID = "" + + return render_template("apiDocs.html", apikey=userID, domain=request.headers['Host'], url_scheme=url_scheme) if __name__ == '__main__': diff --git a/templates/apiDocs.html b/templates/apiDocs.html new file mode 100644 index 0000000000000000000000000000000000000000..4d65600a7e13cfdc5eafe44878d6c150270ad58b --- /dev/null +++ b/templates/apiDocs.html @@ -0,0 +1,65 @@ +<head> + <style> + table tr:nth-child(even) { + background-color: #eee; + } + table tr:nth-child(odd) { + background-color: #fff; + } + table th { + background-color: black; + color: white; + } + </style> +</head> +<body> + <h1>Docs for URL shorter API</h1> + {% if apikey %} + <p>You API key is: {{apikey}}</p> + {% endif %} + + <h3>Make a request</h3> + <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> + <ul> + <li>short: the short URL you want to get. example: {{domain}}/example</li> + <li>long: the long URL you want to short</li> + <li>apikey: you can add this argument, if you want to assign this link to your account.</li> + </ul> + <p>example API call with curl: <i>curl -d "apikey={{apikey}}&short={{domain}}/example&long=http://example.com" -X POST {{url_scheme}}://{{domain}}/user/api</i></p> + + <h3>Responses from Server</h3> + <p>The response from the server is always made in json format it includes the following arguments:</p> + <ul> + <li>status: The status code of the reponse. Down there is a table with the posible response codes</li> + <li>message: This argument will do the same like the status code, but it is written out, for reading by human.</li> + </ul> + + <h4>Response codes:</h4> + <table> + <tr> + <th>Code</th> + <th>Meaning</th> + </tr> + <tr> + <td>0</td> + <td>Success</td> + </tr> + <tr> + <td>1</td> + <td>The short URL argument is missing</td> + </tr> + <tr> + <td>2</td> + <td>The long URL is missing.</td> + </tr> + <tr> + <td>3</td> + <td>The doamin for the short URL is not in the list of allowed domains</td> + </tr> + <tr> + <td>4</td> + <td>The short URL is already taken.</td> + </tr> + + </table> +</body> \ No newline at end of file