From 4570ed20cdafd48876cf8121c630f333b337fb70 Mon Sep 17 00:00:00 2001 From: Hazel Noack Date: Thu, 3 Jul 2025 12:32:19 +0200 Subject: [PATCH] fixed fetching of optional fields --- backend/diyhrt_fetch.go | 36 ++++++++++++++++++++++++-------- backend/diyhrt_models.go | 44 ++++++++++++++++++++-------------------- main.go | 7 ++++++- 3 files changed, 56 insertions(+), 31 deletions(-) diff --git a/backend/diyhrt_fetch.go b/backend/diyhrt_fetch.go index 7e3b2d6..491da28 100644 --- a/backend/diyhrt_fetch.go +++ b/backend/diyhrt_fetch.go @@ -3,26 +3,46 @@ package backend import ( "encoding/json" "fmt" + "io" "net/http" "os" + "time" ) const endpoint = "https://diyhrt.market/api/listings" func GetListings() ([]Listing, error) { apiKey := os.Getenv("API_KEY") - - // 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 + if apiKey == "" { + return nil, fmt.Errorf("API_KEY environment variable not set") } + // Create HTTP client + client := &http.Client{Timeout: 10 * time.Second} + + // Create request + req, err := http.NewRequest("GET", endpoint+"?api_token="+apiKey, nil) + if err != nil { + return nil, fmt.Errorf("creating request failed: %w", err) + } + + // Send request + resp, err := client.Do(req) + if err != nil { + return nil, fmt.Errorf("request failed: %w", err) + } + defer resp.Body.Close() + + // Check status code + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + return nil, fmt.Errorf("unexpected status code %d: %s", resp.StatusCode, string(body)) + } + + // Decode response var listings []Listing if err := json.NewDecoder(resp.Body).Decode(&listings); err != nil { - return []Listing{}, err + return nil, fmt.Errorf("decoding failed: %w", err) } return listings, nil diff --git a/backend/diyhrt_models.go b/backend/diyhrt_models.go index e64986c..40c5559 100644 --- a/backend/diyhrt_models.go +++ b/backend/diyhrt_models.go @@ -1,41 +1,41 @@ package backend type ActiveIngredient struct { - Name string `json:"name"` - Ester string `json:"ester,omitempty"` - DisplayName string `json:"display_name,omitempty"` + Name string `json:"name"` + Ester *string `json:"ester,omitempty"` + DisplayName *string `json:"display_name,omitempty"` } type Product struct { - Id int `json:"id,omitempty"` + Id *int `json:"id,omitempty"` Name string `json:"name"` - Image string `json:"image,omitempty"` + Image *string `json:"image,omitempty"` ActiveIngredient ActiveIngredient `json:"active_ingredient"` } type Store struct { - Id int `json:"id,omitempty"` - Name string `json:"name"` - Url string `json:"url"` - Description string `json:"description"` - ShipsFromCountry string `json:"ships_from_country,omitempty"` - ShipsToCountry string `json:"ships_to_country,omitempty"` - ServiceStatus string `json:"service_status"` - ServiceStatusNotes string `json:"service_status_notes,omitempty"` - PaymentMethods string `json:"payment_methods,omitempty"` - CategoryName string `json:"category_name,omitempty"` + Id *int `json:"id,omitempty"` + Name string `json:"name"` + Url string `json:"url"` + Description string `json:"description"` + ShipsFromCountry *string `json:"ships_from_country,omitempty"` + ShipsToCountry *string `json:"ships_to_country,omitempty"` + ServiceStatus string `json:"service_status"` + ServiceStatusNotes *string `json:"service_status_notes,omitempty"` + PaymentMethods []string `json:"payment_methods,omitempty"` + CategoryName *string `json:"category_name,omitempty"` } type Listing struct { - Id int `json:"id,omitempty"` - ProductName string `json:"product_name,omitempty"` + Id *int `json:"id,omitempty"` + ProductName *string `json:"product_name,omitempty"` StoreName string `json:"store_name"` - Price string `json:"price,omitempty"` - PriceCurrency string `json:"price_currency,omitempty"` - State string `json:"state,omitempty"` + Price *string `json:"price,omitempty"` + PriceCurrency *string `json:"price_currency,omitempty"` + State *string `json:"state,omitempty"` InStock bool `json:"in_stock"` - Url string `json:"url,omitempty"` - PricingPerUnit string `json:"pricing_per_unit,omitempty"` + Url *string `json:"url,omitempty"` + PricingPerUnit *string `json:"pricing_per_unit,omitempty"` Product Product `json:"product"` Store Store `json:"store"` } diff --git a/main.go b/main.go index 627d35e..c2aa951 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,12 @@ import ( func main() { fmt.Println("running transfem startpage") - listings, _ := backend.GetListings() + listings, err := backend.GetListings() + if err != nil { + fmt.Println(err) + return + } + for _, l := range listings { fmt.Println(l) }