MP 8: Course-Weather Microservice

Extra Credit: Completed and turned in via git before October 29, 2021 at 11:59pm
Due Date: Completed and turned in via git before October 31, 2021 at 11:59pm
Points: MP 8 is worth 50 points

Overview

As the weather gets cooler this semester, what if there was a way to check the weather for the next time your class meets? In this MP, you will build a microservice architecture application where you will build the middleware piece of the application that returns the weather the next time your class meets.

To do this, you will:

  • Interact with a microservice that we have provided (courses-microservice) via a web API,
  • Interact with the National Weather Service’s web API to get the forecasted weather, and
  • Interact with the provided frontend to report the weather

MP8 Overview Session

The MP8 Overview Session was held on Monday, Oct. 25 at 6:00pm.

Initial Files

In your CS 240 directory, merge the initial starting files with the following commands:

git fetch release
git merge release/mp8 -m "Merging initial files"

Python Libraries

This MP uses a few new Python libraries, specifically: pandas, python-dotenv and requests. You will need to use either py -m pip install <library> or pip install <library> if you get an error that the specifically library is not found.

Machine Problem

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:

  • You should accept any reasonable course identification (CS 240, cs240, or other such combinations). This will be provided to your microservice as via a FORM submission field course.

  • You must use the provided courses-microservice as a web API (see section below) to get the course meeting days and time.

  • You must use the National Weather Service API to get the weather forecast https://www.weather.gov/documentation/services-web-api.
    • Use (40.1125,-88.2284) as the GPS coordinates for all locations.
    • You must get the forecast for the specific hour when the course begins to meet (not the high temperature for the day).
    • See the courses-microservice section below on how to use the requests library to make a request to another API server, except now your URL will be the National Weather Service.
  • You must report the data back in a response JSON with the following structure with a 200 response:
      {
        "course": "CS 240",
        "nextCourseMeeting": "2021-10-26 12:30:00",
        "forecastTime": "2021-10-26 12:00:00",
        "temperature": 60,
        "shortForecast": "Mostly Sunny"
      }
    
  • On any sort of error, respond with a 400 response (ex: when the course cannot be found).

  • 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.

    • You should not account for holidays, the end of semester, or anything else. You should assume the next course meeting is the next datetime that occurs after the current datetime when the course would meet.

Courses Microservice

To help you on this task, the courses-microservice directory provides a complete and fully functional microservice. For 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/240/:

{
  "course": "CS 240",
  "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"
}

In Python, this JSON can be retrieved as a Python dictionary using the following code using the requests library:

# Ensure `subject` contains the subject (ex: "CS") and `number` contains the course number (ex: 240).
server_url = os.getenv('COURSES_MICROSERVICE_URL')
r = requests.get(f'{server_url}/{subject}/{number}/')
course_gpa_data = r.json()

# Use the result in your code (I'm just printing to console here, but you'll use it for your result):
print(course_gpa_data)

The provided .env already sets up the courses microservice to run on :24000 and the COURSES_MICROSERVICE_URL contains the location of that server (127.0.0.1:24000). Once you launch the courses-microservice, you can visit it with your web browser to view results. For example:

Testing Your Application

A frontend has been provided for you to test your web application. To run your service:

  • Launch the courses-microservice from the courses-microservice directory
  • In another terminal, launch the mp8 app
  • Connect to http://127.0.0.1:5000/ to view your app!

Testing Suggestions

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:

  • CS 225 is a good test case since it starts at 11:00am and uses the forecast for the exact time the class starts (11:00am).
  • CS 240 is another good test case since we start at 12:30pm and the forecasts are for a one hour period (ex: 12:00pm - 1:00pm).

Submit

When you have completed your program, double-check that your server runs as expected by the specifications above. When you are ready, submit the code via the following git commands:

git add -u
git commit -m "MP submission"
git push origin master

You can verify your code was successfully submitted by viewing your git repo on your github account: https://github.com.