Files
htwkalender/backend/service/fetch/v3/fetchProfs.go
2024-03-24 00:15:49 +01:00

103 lines
2.4 KiB
Go

package v3
import (
"encoding/json"
"log/slog"
"net/http"
)
/*
{
"hydra:member": [
{
"@context": "string",
"@id": "string",
"@type": "string",
"id": "string",
"kuerzel": "string",
"nachname": "string",
"fakultaet": "https://example.com/",
"bezeichnung": "string",
"bezeichnungOne": "string"
}
],
"hydra:totalItems": 0,
"hydra:view": {
"@id": "string",
"type": "string",
"hydra:first": "string",
"hydra:last": "string",
"hydra:previous": "string",
"hydra:next": "string"
},
"hydra:search": {
"@type": "string",
"hydra:template": "string",
"hydra:variableRepresentation": "string",
"hydra:mapping": [
{
"@type": "string",
"variable": "string",
"property": "string",
"required": true
}
]
}
}
*/
// professor Model for fetching json data
type Professors struct {
TotalItems int `json:"hydra:totalItems"`
Professors []Professor `json:"hydra:member"`
}
type Professor struct {
ID string `json:"id"`
ShortCut string `json:"kuerzel"`
LastName string `json:"nachname"`
Faculty string `json:"fakultaet"`
Description string `json:"bezeichnung"`
Description1 string `json:"bezeichnungOne"`
}
func parseProfs(url string, client *http.Client) (Professors, error) {
// the url is paginated, so we need to fetch all pages
// example url: https://luna.htwk-leipzig.de/api/dozierende?page=1&itemsPerPage=100
// the itemsPerPage is set to 100, so we need to fetch all pages until we get an empty response
var fetchedProfs Professors
var itemsPerPage = 999
responses, err := paginatedFetch(url, itemsPerPage, client)
if err != nil {
slog.Error("Error while fetching paginated api", err)
return fetchedProfs, err
}
for _, response := range responses {
var hydra hydraResponse
err = json.Unmarshal([]byte(response), &hydra)
if err != nil {
slog.Error("Error while unmarshalling hydra response", err, url)
return fetchedProfs, err
}
var professors Professors
err = json.Unmarshal([]byte(response), &professors)
if err != nil {
slog.Error("Error while unmarshalling professors", err)
return fetchedProfs, err
}
fetchedProfs.TotalItems = professors.TotalItems
fetchedProfs.Professors = append(fetchedProfs.Professors, professors.Professors...)
}
return fetchedProfs, nil
}