/* 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 . */ package utility import ( "bytes" "encoding/json" "errors" "fmt" "io" "net/http" "git.zervo.org/zervo/GoSkola24API/internal/types" ) func Request(data any, endpoint string) (_response interface{}, _error error) { const xscope string = "8a22163c-8662-4535-9050-bc5e1923df48" // Might need to change this var url string = fmt.Sprintf("https://web.skola24.se/api/%s", endpoint) // Enconde JSON jsonRequest, err := json.Marshal(data) if err != nil { return nil, errors.New("could not encode json: " + err.Error()) } // Create the POST request postRequest, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonRequest)) postRequest.Header.Add("Content-Type", "application/json") postRequest.Header.Add("X-Scope", xscope) if err != nil { return nil, errors.New("could not create request: " + err.Error()) } // Send the POST request response, err := http.DefaultClient.Do(postRequest) if err != nil { return nil, errors.New("request failed: " + err.Error()) } // Read body defer response.Body.Close() bodyBytes, _ := io.ReadAll(response.Body) // Unmarshal response var responseData types.BaseResponse json.Unmarshal(bodyBytes, &responseData) // Return as raw bytes (let consumer do type assertion) return responseData.Data, nil }