itfactory-tm / thomas-bot

Thomas Bot, the friendly Discord bot
GNU Affero General Public License v3.0
9 stars 11 forks source link

About the menu API #111

Closed wispae closed 3 years ago

wispae commented 3 years ago

API Introduction and basics

Some information about the menus on the sharepoint site of Thomas More. Initially we thought that the only way to retrieve it was to go through the painful process of the sharepoint API. Luckily, after some investigation it turned out that the menu was being retrieved from another external website, without requiring any authentication.

This happens through the https://tmmenumanagement.azurewebsites.net/api/Menu/ API. To use this API, just append the campus name to this URL (e.g. /api/Menu/Geel)

As of the writing of this post, the pandemic is still ongoing, the restaurants closed, and the API returns an empty JSON list. Because of this we currently do not know what the actual response looks like. Most likely, every campus will either return different data or a different response format. Keep this in mind when developing further.

Lastly, in order to find the name to use in the API, we can make use of the "network" tab in the browser developer tools panel. Navigate to the page containing the menu and watch out for a GET-request to the tmmenumanagement site. The suffix there will tell you what to use in the code.

Note: It seems some locations use "undefined" to send a request to the API. It is currently assumed they either do not currently provide their menu through the API, or use a different endpoint altogether

Alternative API endpoint and general structure

In the introduction part, we saw that the general endpoint for retrieving the menu of the current day /Menu/Campus is. However, if we want to find the menu for the entire week, the endpoint /WeekMenu/ can be used.

This will return the entire menu for the entire week. However, there are several details worth noting about the data that is returned;

Although most of this is easily addressed, the fact that random ID's have to be used to access the useful data gives us a massive headache.

Or to quote Maartje:

Gonna merge for now will attempt to improve it myself later but since the JSON is for sure haunted by a ghost it won't be easy

Current implementation

Because directly parsing the JSON to an easily readable format is not currently possible (see the above random ID's) we have to work around this. The current implementation is a bit dodgy, as we parse the JSON as slice of a map of an interface. Because of this we can access the data bit by bit, always converting part of it to a Go useable format. However this means that we are accessing our data in a very unsafe way.

This being said, after we get past the unsafe part of the data parsing, it is possible to convert the days into a known struct format. These are then bundled in a slice to be read when creating the Discord embed.

For further information, you can contact me