mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender.git
synced 2025-08-13 15:13:51 +02:00
feat:#39 added module and prof fetch
This commit is contained in:
102
backend/service/fetch/v3/fetchProfs.go
Normal file
102
backend/service/fetch/v3/fetchProfs.go
Normal file
@@ -0,0 +1,102 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user