Compare commits

..

No commits in common. "3f359d0c57e99db10ad60120442a459f20497d9a" and "b0b4cc4eb35400523d72686615d37c3c0aa3d282" have entirely different histories.

3 changed files with 28 additions and 52 deletions

View File

@ -3,46 +3,27 @@ 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")
if apiKey == "" {
return nil, fmt.Errorf("API_KEY environment variable not set")
}
fmt.Println(apiKey)
// Create HTTP client
client := &http.Client{Timeout: 10 * time.Second}
// why put api key in url parameter
resp, err := http.NewRequest("GET", endpoint+"?api_token="+apiKey, nil)
// Create request
req, err := http.NewRequest("GET", endpoint+"?api_token="+apiKey, nil)
if err != nil {
return nil, fmt.Errorf("creating request failed: %w", err)
fmt.Print(err.Error())
return []Listing{}, 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 nil, fmt.Errorf("decoding failed: %w", err)
return []Listing{}, err
}
return listings, nil

View File

@ -2,40 +2,40 @@ package backend
type ActiveIngredient struct {
Name string `json:"name"`
Ester string `json:"ester,omitempty"`
DisplayName string `json:"display_name,omitempty"`
Ester string `json:"ester"`
DisplayName string `json:"display_name"`
}
type Product struct {
Id int `json:"id,omitempty"`
Id int `json:"id"`
Name string `json:"name"`
Image string `json:"image,omitempty"`
Image string `json:"image"`
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"`
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,omitempty"`
ProductName string `json:"product_name,omitempty"`
Id int `json:"id"`
ProductName string `json:"product_name"`
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"`
PriceCurrency string `json:"price_currency"`
State string `json:"state"`
InStock bool `json:"in_stock"`
Url string `json:"url,omitempty"`
PricingPerUnit string `json:"pricing_per_unit,omitempty"`
Url string `json:"url"`
PricingPerUnit string `json:"pricing_per_unit"`
Product Product `json:"product"`
Store Store `json:"store"`
}

View File

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