Compare commits
	
		
			3 Commits
		
	
	
		
			b0b4cc4eb3
			...
			3f359d0c57
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					3f359d0c57 | ||
| 
						 | 
					4570ed20cd | ||
| 
						 | 
					3a64b15fb1 | 
@@ -3,27 +3,46 @@ package backend
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const endpoint = "https://diyhrt.market/api/listings"
 | 
					const endpoint = "https://diyhrt.market/api/listings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func GetListings() ([]Listing, error) {
 | 
					func GetListings() ([]Listing, error) {
 | 
				
			||||||
	apiKey := os.Getenv("API_KEY")
 | 
						apiKey := os.Getenv("API_KEY")
 | 
				
			||||||
	fmt.Println(apiKey)
 | 
						if apiKey == "" {
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("API_KEY environment variable not set")
 | 
				
			||||||
	// 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
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 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
 | 
						var listings []Listing
 | 
				
			||||||
	if err := json.NewDecoder(resp.Body).Decode(&listings); err != nil {
 | 
						if err := json.NewDecoder(resp.Body).Decode(&listings); err != nil {
 | 
				
			||||||
		return []Listing{}, err
 | 
							return nil, fmt.Errorf("decoding failed: %w", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return listings, nil
 | 
						return listings, nil
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,40 +2,40 @@ package backend
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
type ActiveIngredient struct {
 | 
					type ActiveIngredient struct {
 | 
				
			||||||
	Name        string `json:"name"`
 | 
						Name        string `json:"name"`
 | 
				
			||||||
	Ester       string `json:"ester"`
 | 
						Ester       string `json:"ester,omitempty"`
 | 
				
			||||||
	DisplayName string `json:"display_name"`
 | 
						DisplayName string `json:"display_name,omitempty"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Product struct {
 | 
					type Product struct {
 | 
				
			||||||
	Id               int              `json:"id"`
 | 
						Id               int              `json:"id,omitempty"`
 | 
				
			||||||
	Name             string           `json:"name"`
 | 
						Name             string           `json:"name"`
 | 
				
			||||||
	Image            string           `json:"image"`
 | 
						Image            string           `json:"image,omitempty"`
 | 
				
			||||||
	ActiveIngredient ActiveIngredient `json:"active_ingredient"`
 | 
						ActiveIngredient ActiveIngredient `json:"active_ingredient"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Store struct {
 | 
					type Store struct {
 | 
				
			||||||
	Id                 int    `json:"id"`
 | 
						Id                 int      `json:"id,omitempty"`
 | 
				
			||||||
	Name               string   `json:"name"`
 | 
						Name               string   `json:"name"`
 | 
				
			||||||
	Url                string   `json:"url"`
 | 
						Url                string   `json:"url"`
 | 
				
			||||||
	Description        string   `json:"description"`
 | 
						Description        string   `json:"description"`
 | 
				
			||||||
	ShipsFromCountry   string `json:"ships_from_country"`
 | 
						ShipsFromCountry   string   `json:"ships_from_country,omitempty"`
 | 
				
			||||||
	ShipsToCountry     string `json:"ships_to_country"`
 | 
						ShipsToCountry     string   `json:"ships_to_country,omitempty"`
 | 
				
			||||||
	ServiceStatus      string   `json:"service_status"`
 | 
						ServiceStatus      string   `json:"service_status"`
 | 
				
			||||||
	ServiceStatusNotes string `json:"service_status_notes"`
 | 
						ServiceStatusNotes string   `json:"service_status_notes,omitempty"`
 | 
				
			||||||
	PaymentMethods     string `json:"payment_methods"`
 | 
						PaymentMethods     []string `json:"payment_methods,omitempty"`
 | 
				
			||||||
	CategoryName       string `json:"category_name"`
 | 
						CategoryName       string   `json:"category_name,omitempty"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Listing struct {
 | 
					type Listing struct {
 | 
				
			||||||
	Id             int     `json:"id"`
 | 
						Id             int     `json:"id,omitempty"`
 | 
				
			||||||
	ProductName    string  `json:"product_name"`
 | 
						ProductName    string  `json:"product_name,omitempty"`
 | 
				
			||||||
	StoreName      string  `json:"store_name"`
 | 
						StoreName      string  `json:"store_name"`
 | 
				
			||||||
	Price          string  `json:"price"`
 | 
						Price          string  `json:"price,omitempty"`
 | 
				
			||||||
	PriceCurrency  string  `json:"price_currency"`
 | 
						PriceCurrency  string  `json:"price_currency,omitempty"`
 | 
				
			||||||
	State          string  `json:"state"`
 | 
						State          string  `json:"state,omitempty"`
 | 
				
			||||||
	InStock        bool    `json:"in_stock"`
 | 
						InStock        bool    `json:"in_stock"`
 | 
				
			||||||
	Url            string  `json:"url"`
 | 
						Url            string  `json:"url,omitempty"`
 | 
				
			||||||
	PricingPerUnit string  `json:"pricing_per_unit"`
 | 
						PricingPerUnit string  `json:"pricing_per_unit,omitempty"`
 | 
				
			||||||
	Product        Product `json:"product"`
 | 
						Product        Product `json:"product"`
 | 
				
			||||||
	Store          Store   `json:"store"`
 | 
						Store          Store   `json:"store"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										7
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								main.go
									
									
									
									
									
								
							@@ -9,7 +9,12 @@ import (
 | 
				
			|||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
	fmt.Println("running transfem startpage")
 | 
						fmt.Println("running transfem startpage")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	listings, _ := backend.GetListings()
 | 
						listings, err := backend.GetListings()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Println(err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, l := range listings {
 | 
						for _, l := range listings {
 | 
				
			||||||
		fmt.Println(l)
 | 
							fmt.Println(l)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user