66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"git.k6n.net/go-cart-actor/pkg/cart"
|
|
)
|
|
|
|
type CartClient struct {
|
|
httpClient *http.Client
|
|
baseUrl string
|
|
}
|
|
|
|
func NewCartClient(baseUrl string) *CartClient {
|
|
return &CartClient{
|
|
httpClient: &http.Client{Timeout: 10 * time.Second},
|
|
baseUrl: baseUrl,
|
|
}
|
|
}
|
|
|
|
// func (c *CartClient) ApplyMutation(cartId cart.CartId, mutation proto.Message) error {
|
|
// url := fmt.Sprintf("%s/internal/cart/%s/mutation", c.baseUrl, cartId.String())
|
|
// data, err := proto.Marshal(mutation)
|
|
// if err != nil {
|
|
// return err
|
|
// }
|
|
// req, err := http.NewRequest("POST", url, bytes.NewReader(data))
|
|
// if err != nil {
|
|
// return err
|
|
// }
|
|
// req.Header.Set("Content-Type", "application/protobuf")
|
|
// resp, err := c.httpClient.Do(req)
|
|
// if err != nil {
|
|
// return err
|
|
// }
|
|
// defer resp.Body.Close()
|
|
// if resp.StatusCode != http.StatusOK {
|
|
// return fmt.Errorf("cart mutation failed: %s", resp.Status)
|
|
// }
|
|
// return nil
|
|
// }
|
|
|
|
func (s *CartClient) getCartGrain(ctx context.Context, cartId cart.CartId) (*cart.CartGrain, error) {
|
|
// Call cart service to get grain
|
|
url := fmt.Sprintf("%s/cart/byid/%d", s.baseUrl, cartId.String())
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
resp, err := s.httpClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("failed to get cart: %s", resp.Status)
|
|
}
|
|
var grain cart.CartGrain
|
|
err = json.NewDecoder(resp.Body).Decode(&grain)
|
|
return &grain, err
|
|
}
|