Created
December 25, 2019 14:14
-
-
Save renatoliveira/24e89963d79f8a49bf77fb273c97f0a0 to your computer and use it in GitHub Desktop.
Transform a POST call to a PATCH call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import the required libraries | |
# Flask is the web framework for dealing with web stuff (such as serving the app and handling | |
# the connections) We need to import the main "Flask" to run the app, and also its | |
# request and Response method and class to handle the request properly | |
from flask import Flask, Response, request | |
# requests is a simple http request library to handle... requests. | |
import requests | |
# Base64 is a standard module to help us encode/decode Base 64 strings | |
import base64 | |
# Json is a standar dmodule to help us handle JSON in Python (converting it from/to | |
# dictionaries - which are also known as maps in some other languages) | |
import json | |
# OS is a standard module to handle dealing with the OS directly (we use it just to check | |
# an environment variable at the end of the script) | |
import os | |
# Lets first create the app. This is an empty app which does nothing. | |
# The app will do what we want as we define the methods/routes below, with (for example) | |
# the `app.route` decorator (which specifies the route and allowed methods) | |
app = Flask(__name__) | |
# This route defines that the app can receive POST requests in the `/contact/` endpoint. So | |
# when deployed, if the app is named `quiet-waters-12345`, its Heroku URL will be | |
# `https://quiet-waters-12345.herokuapp.com/` and we should hit that endpoint, adding the | |
# `/contact/` at the end. | |
@app.route('/contact/', methods=['POST']) | |
def contact(): | |
# First lets deserialize the request's JSON data into a dictionary. | |
request_data = request.get_json() | |
# We check if there are the required attributes we need | |
if 'token' in request_data and 'payload' in request_data and 'url' in request_data: | |
try: | |
# We try to decode the payload | |
payload = base64.b64decode(request_data['payload']).decode('utf-8') | |
# Assign the original payload to a new attribute named `original_payload` | |
# in our dictionary | |
request_data['original_payload'] = payload | |
# Define the headers as required by the Azure endpoint | |
headers = { | |
'Authorization': 'Bearer ' + request_data['token'], | |
'Content-Type': 'application/json' | |
} | |
# Try to call external endpoint using the requests library. Note that we | |
# use the `patch` method here. | |
azure_request = requests.patch( | |
url=request_data['url'], | |
data=payload, | |
headers=headers | |
) | |
# When the request is finished, its result is stored in `azure_request`, | |
# which we can use to get the JSON response. | |
result = { | |
"azure_response": azure_request.json() | |
} | |
# We basically dump the request's result into a new Response and we return | |
# it to the service who called us in the first place. | |
resp = Response(json.dumps(result), status=azure_request.status_code, mimetype='applcation/json') | |
return resp | |
except Exception as e: | |
resp = Response(json.dumps({'error': e.args}), status=500, mimetype='applcation/json') | |
# Returns an error response because there is missing data in the payload. | |
return Response(json.dumps({'error':'No token or payload data informed'}), status=400, mimetype='application/json') | |
# Checks if the `IS_HEROKU` variable is set. If it is (in our dyno) then the app is running on | |
# Heroku's cloud. Otherwise it is running locally in our machine, so we want it to run in our | |
# localhost, on port 8080 instead (and with debug mode active). | |
if not os.environ.get('IS_HEROKU', None) and __name__ == '__main__': | |
app.run(host='localhost', port='8080', debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment