Skip to content

Instantly share code, notes, and snippets.

@evandiewald
Created July 28, 2021 19:47
Show Gist options
  • Save evandiewald/80cd6ae6ca78ddf23740cedb32d69c7c to your computer and use it in GitHub Desktop.
Save evandiewald/80cd6ae6ca78ddf23740cedb32d69c7c to your computer and use it in GitHub Desktop.
The /fruits endpoint from our FastAPI-based server
# /fruits endpoint from main.py
@app.post("/fruits")
async def fruits(request: Request):
body = await request.json()
access_key_id = urllib.parse.quote_plus(body['AccessKeyId'])
secret_key_id = urllib.parse.quote_plus(body['SecretAccessKey'])
session_token = urllib.parse.quote_plus(body['SessionToken'])
uri = f"mongodb+srv://{access_key_id}:{secret_key_id}@cluster0.bjrye.mongodb.net/myFirstDatabase" \
f"?authSource=%24external&authMechanism=MONGODB-AWS&retryWrites=true&w=majority" \
f"&authMechanismProperties=AWS_SESSION_TOKEN:{session_token}"
mongo_client = pymongo.MongoClient(uri)
mongo_db = mongo_client.test
fruit_list = []
fruits = mongo_db['marketplace'].find({}, {'_id': 0, 'name': 1, 'price': 1, 'quantity': 1})
for fruit in fruits:
fruit_list.append(fruit)
return JSONResponse({"fruits": json.dumps(fruit_list)})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment