Basic proxy that adds your CARTO API key to any requests and forwards them to CARTO
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 requests | |
from flask import Flask, request | |
BASE_URL = "https://my_account.carto.com" # Or CDN, or on-prem | |
API_KEY = "my_api_key" | |
VERIFY_SSL_CERT = True # Set to False if you don't want the proxy to verify CARTO's cert | |
app = Flask(__name__) | |
@app.route('/', defaults={'path': ''}) | |
@app.route('/<path:path>') | |
def add_api_key(path): | |
# Add API key to every forwarded request | |
args = dict(request.args) | |
args["api_key"] = API_KEY | |
# Forward request to CARTO | |
response = requests.get(BASE_URL + request.path, params=args, verify=VERIFY_SSL_CERT) | |
# We want to forward CARTO's response headers. | |
# However, response from CARTO may come in chunks and it's much easier forward the whole thing | |
try: | |
del(response.headers["Content-Encoding"]) | |
except KeyError: | |
pass | |
try: | |
del(response.headers["Transfer-Encoding"]) | |
except KeyError: | |
pass | |
# Send CARTO's response (as a whole) back to the browser | |
return (response.content, response.status_code, response.headers.items()) | |
if __name__ == "__main__": | |
# Client code should point to "https://localhost:5000/whatever" (or equivalent) instead of "https://my_account.carto.com/whatever" | |
# Using a self-signed cert here | |
# In order to use real certs, do app.run(ssl_context=('path.to/server.crt', 'path.to/server.key')) | |
app.run(ssl_context="adhoc") |
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
Flask | |
requests | |
pyOpenSSL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment