107 lines
3.9 KiB
Go
107 lines
3.9 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/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 data from the anonymous field
|
|
anonMap, _ := unitMap["anonymous"].(map[string]interface{})
|
|
availableData := pubtypes.DataAvailability{
|
|
HasClasses: func() bool { val, _ := anonMap["classes"].(bool); return val }(),
|
|
HasCourses: func() bool { val, _ := anonMap["courses"].(bool); return val }(),
|
|
HasGroups: func() bool { val, _ := anonMap["groups"].(bool); return val }(),
|
|
HasResources: func() bool { val, _ := anonMap["resources"].(bool); return val }(),
|
|
HasRooms: func() bool { val, _ := anonMap["rooms"].(bool); return val }(),
|
|
HasStudents: func() bool { val, _ := anonMap["students"].(bool); return val }(),
|
|
HasSubjects: func() bool { val, _ := anonMap["subjects"].(bool); return val }(),
|
|
HasTeachers: func() bool { val, _ := anonMap["teachers"].(bool); return val }(),
|
|
}
|
|
|
|
// Map to the struct
|
|
unit := pubtypes.School{
|
|
Name: func() string { val, _ := unitMap["unitId"].(string); return val }(),
|
|
SchoolId: func() string { val, _ := unitMap["unitGuid"].(string); return val }(),
|
|
HostName: hostname,
|
|
AllowCalendarExport: func() bool { val, _ := unitMap["allowCalendarExport"].(bool); return val }(),
|
|
AvailableData: availableData,
|
|
}
|
|
|
|
units = append(units, unit)
|
|
}
|
|
|
|
return units, nil
|
|
}
|