ucp
This commit is contained in:
@@ -5,7 +5,9 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"git.k6n.net/mats/go-cart-actor/pkg/checkout"
|
||||
@@ -61,17 +63,52 @@ func (s *CheckoutServer) handleCreateCheckout(w http.ResponseWriter, r *http.Req
|
||||
return
|
||||
}
|
||||
|
||||
// Initialize checkout with cart reference.
|
||||
// Fetch cart state from cart service
|
||||
cartBaseURL := os.Getenv("CART_INTERNAL_URL")
|
||||
if cartBaseURL == "" {
|
||||
cartBaseURL = "http://cart:8080"
|
||||
}
|
||||
url := fmt.Sprintf("%s/cart/byid/%s", cartBaseURL, cartId.String())
|
||||
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
cartReq, err := http.NewRequestWithContext(r.Context(), "GET", url, nil)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "failed to create cart request: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
profile := os.Getenv("UCP_AGENT_PROFILE")
|
||||
if profile == "" {
|
||||
profile = DefaultUCPAgentProfile
|
||||
}
|
||||
cartReq.Header.Set("UCP-Agent", `profile="`+profile+`"`)
|
||||
|
||||
cartResp, err := client.Do(cartReq)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "failed to fetch cart: "+err.Error())
|
||||
return
|
||||
}
|
||||
defer cartResp.Body.Close()
|
||||
|
||||
if cartResp.StatusCode != http.StatusOK {
|
||||
writeError(w, http.StatusBadRequest, fmt.Sprintf("cart service returned status: %d", cartResp.StatusCode))
|
||||
return
|
||||
}
|
||||
|
||||
cartStateBytes, err := io.ReadAll(cartResp.Body)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "failed to read cart state: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// Initialize checkout with cart reference and state.
|
||||
initMsg := &checkoutMessages.InitializeCheckout{
|
||||
OrderId: "",
|
||||
CartId: uint64(cartId),
|
||||
}
|
||||
if req.Country != "" {
|
||||
// Pass country through the cart state snapshot (minimal).
|
||||
initMsg.CartState = &anypb.Any{
|
||||
TypeUrl: "request/country",
|
||||
Value: []byte(req.Country),
|
||||
}
|
||||
CartState: &anypb.Any{
|
||||
TypeUrl: "type.googleapis.com/cart.CartGrain",
|
||||
Value: cartStateBytes,
|
||||
},
|
||||
}
|
||||
|
||||
if _, err := s.applier.Apply(r.Context(), uint64(checkoutId), initMsg); err != nil {
|
||||
@@ -290,14 +327,22 @@ func (s *CheckoutServer) handleCancelCheckout(w http.ResponseWriter, r *http.Req
|
||||
type OrderHTTPClient struct {
|
||||
baseURL string
|
||||
httpClient *http.Client
|
||||
agentProfile string // UCP-Agent profile URI
|
||||
}
|
||||
|
||||
// NewOrderHTTPClient returns an order applier that creates orders by calling
|
||||
// the order service at baseURL (e.g. "http://order-service:8092").
|
||||
// the order service at baseURL (e.g. "http://order-service:8092"). It reads
|
||||
// the UCP_AGENT_PROFILE env var for the profile URI, falling back to
|
||||
// DefaultUCPAgentProfile from the ucp package.
|
||||
func NewOrderHTTPClient(baseURL string) *OrderHTTPClient {
|
||||
profile := os.Getenv("UCP_AGENT_PROFILE")
|
||||
if profile == "" {
|
||||
profile = DefaultUCPAgentProfile
|
||||
}
|
||||
return &OrderHTTPClient{
|
||||
baseURL: baseURL,
|
||||
httpClient: &http.Client{Timeout: 30 * time.Second},
|
||||
agentProfile: profile,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -381,6 +426,8 @@ func (c *OrderHTTPClient) CreateOrder(ctx context.Context, checkoutID uint64, g
|
||||
return "", fmt.Errorf("order: new request: %w", err)
|
||||
}
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
// Advertise this platform's UCP profile on all outgoing requests.
|
||||
WithUCPAgentOnRequest(httpReq, c.agentProfile)
|
||||
|
||||
resp, err := c.httpClient.Do(httpReq)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user