do the actual request

This commit is contained in:
Hazel Noack 2025-07-03 11:47:15 +02:00
parent a1e06ee718
commit b0b4cc4eb3
2 changed files with 11 additions and 6 deletions

View File

@ -1,6 +1,7 @@
package backend
import (
"encoding/json"
"fmt"
"net/http"
"os"
@ -8,19 +9,22 @@ import (
const endpoint = "https://diyhrt.market/api/listings"
func GetListings() []Listing {
func GetListings() ([]Listing, error) {
apiKey := os.Getenv("API_KEY")
fmt.Println(apiKey)
// why put api key in url parameter
req, err := http.NewRequest("GET", endpoint+"?api_token="+apiKey, nil)
resp, err := http.NewRequest("GET", endpoint+"?api_token="+apiKey, nil)
if err != nil {
fmt.Print(err.Error())
return []Listing{}
return []Listing{}, err
}
fmt.Println(req.Body.Read())
var listings []Listing
if err := json.NewDecoder(resp.Body).Decode(&listings); err != nil {
return []Listing{}, err
}
return []Listing{}
return listings, nil
}

View File

@ -9,7 +9,8 @@ import (
func main() {
fmt.Println("running transfem startpage")
for _, l := range backend.GetListings() {
listings, _ := backend.GetListings()
for _, l := range listings {
fmt.Println(l)
}
}