Skip to content

Commit

Permalink
add apiclient pointer to guest-cart struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Hermsi1337 committed Mar 31, 2019
1 parent 30afb96 commit 7c7d6a2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func main() {
})

// update your cart with the desired items
err = cart.AddItems(apiClient, products)
err = cart.AddItems(products)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -87,7 +87,7 @@ func main() {
}

// estimate shipping carrier for our cart
availableCarrier, err := cart.EstimateShippingCarrier(apiClient, *sAddr)
availableCarrier, err := cart.EstimateShippingCarrier(*sAddr)
if err != nil {
panic(err)
}
Expand All @@ -104,7 +104,7 @@ func main() {
}

// add shipping info to cart
err = cart.AddShippingInformation(apiClient, *payLoad)
err = cart.AddShippingInformation(*payLoad)
if err != nil {
panic(err)
}
Expand All @@ -125,7 +125,7 @@ func main() {
desiredPaymentMethod := paymentMethods[0]

// create the order
orderID, err := cart.CreateOrder(apiClient, desiredPaymentMethod)
orderID, err := cart.CreateOrder(desiredPaymentMethod)
if err != nil {
panic(err)
}
Expand Down
3 changes: 2 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ func (apiClient *ApiClient) CreateGuestCard() (GuestCart, error) {
return cart, fmt.Errorf("unexpected statuscode '%v' - response: '%v'", resp.StatusCode(), resp)
}

cart.ApiClient = apiClient
quoteID := mayTrimSurroundingQuotes(resp.String())
cart.QuoteID = quoteID
cart.Detailed, err = cart.GetDetails(apiClient)
cart.Detailed, err = cart.GetDetails()
if err != nil {
return cart, err
}
Expand Down
37 changes: 19 additions & 18 deletions guest-cart.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
)

type GuestCart struct {
QuoteID string
Detailed types.DetailedCart
QuoteID string
Detailed types.DetailedCart
ApiClient *ApiClient
}

func (cart *GuestCart) GetDetails(apiClient *ApiClient) (types.DetailedCart, error) {
func (cart *GuestCart) GetDetails() (types.DetailedCart, error) {
var detailedCart = &types.DetailedCart{}
httpClient := apiClient.httpClient
httpClient := cart.ApiClient.httpClient
resp, err := httpClient.R().SetResult(detailedCart).Get(guestCarts + "/" + cart.QuoteID)
if err != nil {
return *detailedCart, fmt.Errorf("error while getting detailed cart object from magento2-api: '%v'", err)
Expand All @@ -25,8 +26,8 @@ func (cart *GuestCart) GetDetails(apiClient *ApiClient) (types.DetailedCart, err
return *detailedCart, err
}

func (cart *GuestCart) UpdateSelf(apiClient *ApiClient) error {
newDetails, err := cart.GetDetails(apiClient)
func (cart *GuestCart) UpdateSelf() error {
newDetails, err := cart.GetDetails()
if err != nil {
return fmt.Errorf("error while updating the cart object from magento2-api: '%v'", err)
}
Expand All @@ -35,9 +36,9 @@ func (cart *GuestCart) UpdateSelf(apiClient *ApiClient) error {
return nil
}

func (cart *GuestCart) AddItems(apiClient *ApiClient, items []types.Item) error {
func (cart *GuestCart) AddItems(items []types.Item) error {
endpoint := guestCarts + "/" + cart.QuoteID + guestCartsItems
httpClient := apiClient.httpClient
httpClient := cart.ApiClient.httpClient

type PayLoad struct {
CartItem types.Item `json:"cartItem"`
Expand All @@ -56,17 +57,17 @@ func (cart *GuestCart) AddItems(apiClient *ApiClient, items []types.Item) error
}
}

err := cart.UpdateSelf(apiClient)
err := cart.UpdateSelf()
if err != nil {
return err
}

return nil
}

func (cart *GuestCart) EstimateShippingCarrier(apiClient *ApiClient, addrInfo types.Address) ([]types.Carrier, error) {
func (cart *GuestCart) EstimateShippingCarrier(addrInfo types.Address) ([]types.Carrier, error) {
endpoint := guestCarts + "/" + cart.QuoteID + guestCartsShippingCosts
httpClient := apiClient.httpClient
httpClient := cart.ApiClient.httpClient

type PayLoad struct {
Address types.Address `json:"address"`
Expand Down Expand Up @@ -94,9 +95,9 @@ func (cart *GuestCart) EstimateShippingCarrier(apiClient *ApiClient, addrInfo ty
return *shippingCosts, nil
}

func (cart *GuestCart) AddShippingInformation(apiClient *ApiClient, addrInfo types.AddressInformation) error {
func (cart *GuestCart) AddShippingInformation(addrInfo types.AddressInformation) error {
endpoint := guestCarts + "/" + cart.QuoteID + guestCartsShippingInformation
httpClient := apiClient.httpClient
httpClient := cart.ApiClient.httpClient

type PayLoad struct {
AddressInformation types.AddressInformation `json:"addressInformation"`
Expand All @@ -113,17 +114,17 @@ func (cart *GuestCart) AddShippingInformation(apiClient *ApiClient, addrInfo typ
return fmt.Errorf("unexpected statuscode '%v' - response: '%v'", resp.StatusCode(), resp)
}

err = cart.UpdateSelf(apiClient)
err = cart.UpdateSelf()
if err != nil {
return err
}

return nil
}

func (cart *GuestCart) EstimatePaymentMethods(apiClient *ApiClient) ([]types.PaymentMethod, error) {
func (cart *GuestCart) EstimatePaymentMethods() ([]types.PaymentMethod, error) {
endpoint := guestCarts + "/" + cart.QuoteID + guestCartsPaymentMethods
httpClient := apiClient.httpClient
httpClient := cart.ApiClient.httpClient

paymentMethods := &[]types.PaymentMethod{}

Expand All @@ -143,9 +144,9 @@ func (cart *GuestCart) EstimatePaymentMethods(apiClient *ApiClient) ([]types.Pay
return *paymentMethods, nil
}

func (cart *GuestCart) CreateOrder(apiClient *ApiClient, paymentMethod types.PaymentMethod) (types.OrderID, error) {
func (cart *GuestCart) CreateOrder(paymentMethod types.PaymentMethod) (types.OrderID, error) {
endpoint := guestCarts + "/" + cart.QuoteID + guestCartsOrder
httpClient := apiClient.httpClient
httpClient := cart.ApiClient.httpClient

type PayLoad struct {
PaymentMethod types.PaymentMethodCode `json:"paymentMethod"`
Expand Down

0 comments on commit 7c7d6a2

Please sign in to comment.