This repository contains very basic code for the Python class. It's a messaging API that allows the user to send and receive messages. It uses Flask as the web framework and a (not-recommended) in-memory database for storage. This is the take-home project we send to prospective engineers.
The API runs in a web server on your local machine. You can hit that web server with a browser, but for a little more capability (and because this is a python class), we'll be using a python library called requests to interact with the web server.
There are two "endpoints" to this api:
/get-messages/<user id>This endpoint will retrieve all messages for the given user id, so for example your request might look likehttp://localhost:8080/get-messages/Valto get all messages for "Val"./post-messageThis endpoint is how put put new messages into the system. You'll send data along with that request to tell the system what to store.
All of the commands here need to be run on the command line. The instructions here are for Mac OS. I'll need help to figure out what they should be on Windows. I recommend using iTerm on a Mac, but it's not strictly necessary.
- Python3.9 at a minimum
- Virtual environment created and requirements installed
python3 -m venv venv(This creates a copy of python in a directory calledvenvwhere you can install libraries without messing with the system-level libraries. Think of it as a temporary python install. If you ever need to reset everything, just delete the directory and reinstall with this command.). venv/bin/activate(This "turns on" that temporary python. To turn it back off again, rundeactivate).pip install -r requirements.txt(This installs the necessary libraries to run this code - specifically Flask and requests)
FLASK_APP=message_server/app.py flask run -h localhost -p 8080 (this tells flask to use the code in message_server/app.py as its application code and also says to respond on localhost which is your local machine and on port 8080)
To interact with the server, we're using a library called requests which is installed with the pip install command in the prerequisites. It allows you to make web requests to a web server programatically. You should run the server in one window and then open a new window, go to the directory you've got the code in, activate your virtual environment (. venv/bin/activate), then run python in interactive mode: python.
There is a lot around Flask I'm currently not going over (especially what the @app.route stuff is in the code). Effectively, those lines of code just tell Flask how to route the request. We can cover in more detail another time.
Unit tests are stored in the tests directory. To run:
PYTHONPATH=$PYTHONPATH:. pytest
- Switch to the directory where you pulled the code
- Activate the virtual environment
. venv/bin/activate - Start python
python - Import the requests library -
import requests
requests.post('http://localhost:8080/post-message', data='{"fromUser": "From", "toUser": "To", "message": "Very first message"}')
There's a bit to go through here:
requests.postis making a "POST" request to your server. We'll go into a little more detail later on what that means./post-messagetells Flask where to go with your request - in this case, thepost_messagefunction inmessage_server/__init__.pydata={...}is the data to send in your request. It's effectively sending a message that contains a "fromUser" - "From", a "toUser" - "To", and a text "message". Feel free to play around with these values. I will note, however, that changing or removing "toUser" will cause issues.
resp = requests.get('http://localhost:8080/get-messages/To')
Here's what this is saying:
requests.getis making a "GET" request to your server.get-messagestells Flask where to go with your request - in this case, theget_messagesfunction inmessage_server/__init__.pyTois the "toUser" specified in the message that was posted earlier- The response is stored in the
respvariable. To see the actual content of the response, just typeresp.content.