from flask import Flask, request, make_response, Response

app = Flask(__name__)


@app.route('/')
def index():
    return "<h1>Welcome</h1>"


# We can also return some responses (like 404 page not found and co)
@app.route('/hello', methods=['GET'])
def hello():
    response = make_response('Hello World')
    response.status_code = 202
    response.headers['content-type'] = 'application/octet-stream'
    return response


@app.route('/test_methodes', methods=['GET', 'POST'])
def test_methodes():
    if request.method == 'GET':
        return f"You made a GET request\n"
    if request.method == 'POST':
        return f"You made a POST request\n"
    else:
        return f"You will never see this message\n"


# This is a url Processor
@app.route('/greet/<name>')
def greet(name):
    return f"Hello {name}"


# This is a url Processor
@app.route('/add/<int:number1>/<int:number2>')
def add(number1, number2):
    return f'{number1} + {number2} = {number1+number2}'


# Handle parameters :
@app.route('/handle_url_params')
def handle_url_params():
    if 'greeting' in request.args.keys() and 'name' in request.args.keys():
        greeting = request.args['greeting']
        name = request.args.get('name')
        return f'{greeting}, {name}'
    else:
        return f'some params are missing'


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)
