bot-mm-assos/assos.go

60 lines
1.4 KiB
Go

/*
Pont Mattermost Portail des assos
Copyright (C) 2021 Romain de Laage
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
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 for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"io"
"log"
"net/http"
"encoding/json"
)
func getArticles(endpoint, forB64 string) []Article {
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
log.Fatalln(err)
}
req.Header.Set("For", forB64)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Fatalln("Error when fetching articles, server returned " + resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
log.Printf("Successfully got articles")
var articles []Article
err = json.Unmarshal(body, &articles)
if err != nil {
log.Fatalln(err)
}
return articles
}