Skip to content

handle post methods in /add location instead of the root #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added src/.DS_Store
Binary file not shown.
24 changes: 24 additions & 0 deletions src/action_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import logging
from flask import Flask
from flask import jsonify
from flask import redirect
from flask import request

# Setup a basic logger.
logging.basicConfig()
logger = logging.getLogger()
# Log at debug level by default.
logger.setLevel(logging.DEBUG)

def action_add_url(url,short_code,shortened_urls):
if short_code in shortened_urls:
logger.debug(
"failed to create shortened URL, %s already exists",
short_code,
)
return error(ERROR_ALREADY_EXISTS), 400

logger.debug(
"creating shortened URL, %s -> %s", url, short_code)
shortened_urls[short_code] = url
return jsonify({}), 200
35 changes: 15 additions & 20 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from flask import jsonify
from flask import redirect
from flask import request
from action_utils import *

app = Flask(__name__)

Expand All @@ -19,35 +20,29 @@ def reset_shortened_urls():
global shortened_urls
shortened_urls = dict()

# Setup a basic logger.
logging.basicConfig()
logger = logging.getLogger()
# Log at debug level by default.
logger.setLevel(logging.DEBUG)

@app.route("/", methods=["GET", "POST"])

@app.route("/", methods=["GET"])
def create_short_url():
if request.method == "POST":
return post_short_url()
if request.method == "GET":
return get_short_url()
raise Exception("unsupported method type")

def post_short_url():
#this method used to ADD shortende urls
@app.route("/add", methods=["POST"])
def add_short_url():
req = request.get_json()
url = req["url"]
short_code = req["short_code"]
if short_code in shortened_urls:
logger.debug(
"failed to create shortened URL, %s already exists",
short_code,
)
return error(ERROR_ALREADY_EXISTS), 400

logger.debug(
"creating shortened URL, %s -> %s", url, short_code)
shortened_urls[short_code] = url
return jsonify({}), 200
return action_add_url(url,short_code,shortened_urls)

#this method used to MODIFY shortende urls
# @app.route("/modify", methods=["POST"])
# def add_short_url():
# req = request.get_json()
# url = req["url"]
# short_code = req["short_code"]
# return action_modify_url(url,short_code,shortened_urls)

def get_short_url():
short_code = request.args["short_code"]
Expand Down