This is an archived copy of a previous semester's site.
Please see the current semester's site.
In this MP you will both build and use web service APIs. In particular,
/weather
and one request body field, course
/weatherCache
The code comes with a simple web front-end that lets you interact with your completed service through your web browser.
In your CS 340 directory, merge the initial starting files with the following commands:
git fetch release
git merge release/mp7 --allow-unrelated-histories -m "Merging initial files"
This MP uses several Python libraries. You can install all required libraries by running:
py -m pip install -r requirements.txt
python3 -m pip install -r requirements.txt
(Note the commands are identical, except that Windows requires the use of py
to invoke Python while macOS requires python3
.)
We have fully implemented a course meeting time microservice for you. You can find it in the courses_microservice
directory. This, like the microservice you will implement, uses flask to automate many of the tedious parts of running a webserver.
cd
into the courses_microservice
directory and then do one of the following:
Use the command-line flask runner
py -m flask run --port 34000
python3 -m flask run --port 34000
Use the included development app runner, using the provided .env
to pick the port:
py app.py
python3 app.py
Either way, it should print out something like
Running on http://127.0.0.1:34000/ (Press CTRL+C to quit)
and then wait for you to use it.
On a GET
request to the path /<subject>/<number>/
, the microservice will respond with a JSON object with three keys: course
, Start Time
and Days of Week
. For example, the following is the result of a request to /CS/340/
:
{
"course": "CS 340",
"Days of Week": "TR",
"Start Time": "12:30 PM"
}
If the microservice is unable to find the course, it will return a 404
and JSON with an error
key:
{
"course": "CS 300",
"error": "No course data available for CS 300"
}
Assuming you’ve run flask on port 34000, you can open a browser and try this out by visiting
You can also use it with the requests
library, which is what the microservice you implement will do.
Your task for this MP is to complete the flask app.py
route for POST /weather
that will respond with the weather forecast for the next meeting of the class provided in the request. Specifically:
Accept course identification in the form submission field course
.
Accept course identification in several forms (CS 340
, cs340
, or other such combinations). You may assume it is an all-alphabetic course mnemonic followed by a 3-digit course number.
Use the provided courses_microservice
via web API requests to get the course meeting days and time.
You must use the environmental variable COURSES_MICROSERVICE_URL
to find the location of the courses_microservice
.
You can find COURSES_MICROSERVICE_URL
via the following Python code:
= os.getenv('COURSES_MICROSERVICE_URL') server_url
The value of this variable will be http://127.0.0.1:34000
(no trailing slash).
Use the National Weather Service API to get the weather forecast https://www.weather.gov/documentation/services-web-api.
Cache calls to the National Weather Service API. In particular, if I ask for the same course twice in a row the second must not contact the API, instead remembering how to answer the query without that.
Use (40.11,-88.24) as the GPS coordinates for all locations.
Get the forecast for the specific hour when the course begins to meet (not the high temperature for the day).
You will need to write logic to calculate the next course meeting relative to the current datetime. The Python command datetime.now()
will provide you with a datetime
object with the current time.
Use the requests
library to make a request to the API.
You must report the data back in a response JSON with the following structure with a 200
response:
{
"course": "CS 340",
"nextCourseMeeting": "2022-10-25 02:00:00",
"forecastTime": "2022-10-25 02:00:00",
"temperature": 60,
"shortForecast": "Mostly Sunny"
}
Due to limitations of the weather API, hourly forecast information is only available up to 156 hours (6.5 days) in the future. To address this, if a course’s next meeting time is more than 144 hours (6 days) in the future, you must set the "temperature"
and "shortForecast"
items to the string "forecast unavailable"
:
{
"course": "CS 340",
"nextCourseMeeting": "2022-10-19 02:00:00",
"forecastTime": "2022-10-19 02:00:00",
"temperature": "forecast unavailable",
"shortForecast": "forecast unavailable"
}
On any other sort of error (ex: when the course cannot be found), respond with a 400
response and a JSON object containing the key "error"
and a message describing what went wrong.
Additionally, complete the flask app.py
route for GET /weatherCache
to return a JSON representation of your cache. Specifically,
If the cache is empty, return {}
.
If the cache is not empty, return JSON that includes at least enough information to handle repeat queries without contacting the weather service API. This must include the temperature and short forecast from every previous request, but will likely include more than that.
A frontend has been provided for you to test your web application. To run your service, you must do three things in three different windows:
courses_microservice
from the courses_microservice
directory.mp7
microservice from the mp7
directory.Ensure that your server works for courses many days in the future AND for courses later the same day. (Ex: If you check your service at 9:00am for your 1:00pm class, it should report weather back for the 1:00pm class that same day.)
Test several different courses:
pytest
Only once you have tested it in the web browser and ensured your caching works, you can run automated testing. IMPORTANT: If you run this test suite without caching the NWS forecast, you may be automatically timed out by the NWS server and won’t be able to run tests for several hours.
Once you have made absolutely certain you are using your cached weather forecast for each request after the first, you can run the automated tests:
py -m pytest
python3 -m pytest
This test suite checks the accuracy of your forecasts by contacting a checking service on a course VM. As such, it can only be run on campus, on your VM, or over the university VPN.
Once you have locally passed all the tests, you will need to submit and grade your code. First commit using the standard git commands:
git add -A
git commit -m "MP submission"
git push
The initial grading is done via a manual GitHub Action. You MUST complete this step before the deadline to earn any points for the MP:
ActionTab
mp7 autograding
Run Workflowbutton (located on the blue bar)
Run Workflow(note: this will take upwards of 4 minutes to complete)
Point weights can be found in the autograding results in the GitHub action.