GoSkola24API/internal/requests/get_schools.go
2024-12-06 21:43:00 +01:00

121 lines
3.8 KiB
Go

/*
GoSkola24API
Copyright (C) 2024, Zervó Zadachin
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License version 3
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License version 3 for more details.
This program incorporates external libraries for certain functionalities.
These libraries are covered by their respective licenses, and their usage
agreements are as outlined in their respective documentation or source
code.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// I'm aware this code is really weird and not optimised. I changed approach multiple times in the middle of it and am too lazy to fix it. It works though.
package requests
import (
"errors"
"git.zervo.org/zervo/GoSkola24API/internal/types"
"git.zervo.org/zervo/GoSkola24API/internal/utility"
pubtypes "git.zervo.org/zervo/GoSkola24API/pkg/goskola24api/types"
)
func GetSchools(api_host string) (_result []pubtypes.School, _error error) {
request := types.RequestSchools{
GetTimetableViewerUnitsRequest: types.RequestSchoolsHost{
HostName: api_host,
},
}
response, err := utility.Request(request, "services/skola24/get/timetable/viewer/units")
if err != nil {
return nil, errors.New("could not get schools: " + err.Error())
}
// Assert the response type to a map[string]interface{}
responseMap, ok := response.(map[string]interface{})
if !ok {
return nil, errors.New("unexpected response type")
}
// Assert getTimetableViewerUnitsResponse to a type
timetableMap, ok := responseMap["getTimetableViewerUnitsResponse"].(map[string]interface{})
if !ok {
return nil, errors.New("missing or invalid \"getTimetableViewerUnitsResponse\" field in response")
}
// Get hostname
hostname, ok := timetableMap["hostName"].(string)
if !ok {
return nil, errors.New("missing or invalid \"hostName\" field in response")
}
// Extract units as []interface{}
unitsRaw, ok := timetableMap["units"].([]interface{})
if !ok {
return nil, errors.New("missing or invalid \"units\" field in response")
}
// Convert units into a proper type
var units []pubtypes.School
for _, unitRaw := range unitsRaw {
unitMap, ok := unitRaw.(map[string]interface{})
if !ok {
return nil, errors.New("unexpected unit format")
}
// Extract fields from each unit
unitGuid, _ := unitMap["unitGuid"].(string)
unitId, _ := unitMap["unitId"].(string)
allowCalendarExport, _ := unitMap["allowCalendarExport"].(bool)
anonMap, _ := unitMap["anonymous"].(map[string]interface{})
// Same for anonymous units
hasClasses, _ := anonMap["classes"].(bool)
hasCourses, _ := anonMap["courses"].(bool)
hasGroups, _ := anonMap["groups"].(bool)
hasResources, _ := anonMap["resources"].(bool)
hasRooms, _ := anonMap["rooms"].(bool)
hasStudents, _ := anonMap["students"].(bool)
hasSubjects, _ := anonMap["subjects"].(bool)
hasTeachers, _ := anonMap["teachers"].(bool)
availableData := pubtypes.DataAvailability{
HasClasses: hasClasses,
HasCourses: hasCourses,
HasGroups: hasGroups,
HasResources: hasResources,
HasRooms: hasRooms,
HasStudents: hasStudents,
HasSubjects: hasSubjects,
HasTeachers: hasTeachers,
}
// Map to the struct
unit := pubtypes.School{
Name: unitId,
SchoolId: unitGuid,
HostName: hostname,
AllowCalendarExport: allowCalendarExport,
AvailableData: availableData,
}
units = append(units, unit)
}
return units, nil
}