Compare commits

..

3 Commits

Author SHA1 Message Date
Hazel Noack
b0b4cc4eb3 do the actual request 2025-07-03 11:47:15 +02:00
Hazel Noack
a1e06ee718 added json keys to structs 2025-07-03 11:39:58 +02:00
Hazel Noack
5461bded96 added data models of diyhrtmarketplace 2025-07-03 11:23:36 +02:00
3 changed files with 81 additions and 1 deletions

30
backend/diyhrt_fetch.go Normal file
View File

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

41
backend/diyhrt_models.go Normal file
View File

@ -0,0 +1,41 @@
package backend
type ActiveIngredient struct {
Name string `json:"name"`
Ester string `json:"ester"`
DisplayName string `json:"display_name"`
}
type Product struct {
Id int `json:"id"`
Name string `json:"name"`
Image string `json:"image"`
ActiveIngredient ActiveIngredient `json:"active_ingredient"`
}
type Store struct {
Id int `json:"id"`
Name string `json:"name"`
Url string `json:"url"`
Description string `json:"description"`
ShipsFromCountry string `json:"ships_from_country"`
ShipsToCountry string `json:"ships_to_country"`
ServiceStatus string `json:"service_status"`
ServiceStatusNotes string `json:"service_status_notes"`
PaymentMethods string `json:"payment_methods"`
CategoryName string `json:"category_name"`
}
type Listing struct {
Id int `json:"id"`
ProductName string `json:"product_name"`
StoreName string `json:"store_name"`
Price string `json:"price"`
PriceCurrency string `json:"price_currency"`
State string `json:"state"`
InStock bool `json:"in_stock"`
Url string `json:"url"`
PricingPerUnit string `json:"pricing_per_unit"`
Product Product `json:"product"`
Store Store `json:"store"`
}

11
main.go
View File

@ -1,7 +1,16 @@
package main package main
import "fmt" import (
"fmt"
"gitea.elara.ws/Hazel/transfem-startpage/backend"
)
func main() { func main() {
fmt.Println("running transfem startpage") fmt.Println("running transfem startpage")
listings, _ := backend.GetListings()
for _, l := range listings {
fmt.Println(l)
}
} }