Skip to content

Commit

Permalink
Made Client public to initialize it before use
Browse files Browse the repository at this point in the history
  • Loading branch information
spolischook committed Mar 18, 2024
1 parent a2dc439 commit cc4cbfb
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/router/handler/shopify/getProduct.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func (h *Handler) GetProduct(c *gin.Context) {
if h.client == nil {
if h.Client == nil {
c.AbortWithStatusJSON(401, gin.H{"error": "unauthorized"})
return
}
Expand All @@ -19,7 +19,7 @@ func (h *Handler) GetProduct(c *gin.Context) {
return
}

p, err := h.client.Product.Get(spId, nil)
p, err := h.Client.Product.Get(spId, nil)
if err != nil {
c.AbortWithStatusJSON(500, gin.H{"error": err.Error()})
return
Expand Down
6 changes: 3 additions & 3 deletions src/router/handler/shopify/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

type Handler struct {
client *goshopify.Client
Client *goshopify.Client
}

func (h *Handler) Login(c *gin.Context) {
Expand Down Expand Up @@ -41,8 +41,8 @@ func (h *Handler) Callback(c *gin.Context) {
return
}

// Create a new API client
h.client = app.NewClient(
// Create a new API Client
h.Client = app.NewClient(
config.ShopName,
token,
goshopify.WithLogger(&goshopify.LeveledLogger{}))
Expand Down
6 changes: 3 additions & 3 deletions src/router/handler/shopify/updateProduct.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func (h *Handler) UpdateProduct(c *gin.Context) {
if h.client == nil {
if h.Client == nil {
c.AbortWithStatusJSON(401, gin.H{"error": "unauthorized"})
return
}
Expand All @@ -19,7 +19,7 @@ func (h *Handler) UpdateProduct(c *gin.Context) {
return
}

p, err := h.client.Product.Get(spId, nil)
p, err := h.Client.Product.Get(spId, nil)
if err != nil {
c.AbortWithStatusJSON(500, gin.H{"error": err.Error()})
return
Expand All @@ -33,7 +33,7 @@ func (h *Handler) UpdateProduct(c *gin.Context) {

aw.UpdateShopifyProduct(p)

p, err = h.client.Product.Update(*p)
p, err = h.Client.Product.Update(*p)
if err != nil {
c.AbortWithStatusJSON(500, gin.H{"error": err.Error()})
return
Expand Down
18 changes: 10 additions & 8 deletions src/router/handler/shopify/updateProducts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"github.com/cherevan.art/src/shopify"
"github.com/gin-gonic/gin"
"net/http"
"time"
)

func (h *Handler) UpdateProducts(c *gin.Context) {
if h.client == nil {
if h.Client == nil {
c.AbortWithStatusJSON(401, gin.H{"error": "unauthorized"})
return
}
Expand All @@ -21,7 +22,7 @@ func (h *Handler) UpdateProducts(c *gin.Context) {
}

// Get all products from Shopify
shopifyProducts, err := shopify.GetAllProducts(h.client)
shopifyProducts, err := shopify.GetAllProducts(h.Client)

// Update products
for _, aw := range existingArtWorks {
Expand All @@ -35,7 +36,8 @@ func (h *Handler) UpdateProducts(c *gin.Context) {
if aw.ShopifyID == sp.ID {
isNew = false
aw.UpdateShopifyProduct(&sp)
sp1, err := h.client.Product.Update(sp)
time.Sleep(time.Second / 2) // prevent exceeding the limit of requests
sp1, err := h.Client.Product.Update(sp)
if err != nil {
c.AbortWithStatusJSON(
500,
Expand All @@ -62,7 +64,7 @@ func (h *Handler) UpdateProducts(c *gin.Context) {
}

// Create product
createdProduct, err := h.client.Product.Create(aw.ShopifyProduct())
createdProduct, err := h.Client.Product.Create(aw.ShopifyProduct())
if err != nil {
c.AbortWithStatusJSON(
500,
Expand All @@ -72,7 +74,7 @@ func (h *Handler) UpdateProducts(c *gin.Context) {
return
}

_, err = h.client.ProductListing.Publish(createdProduct.ID)
_, err = h.Client.ProductListing.Publish(createdProduct.ID)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("error publish %d %s: %s", aw.ID, aw.Title, err.Error())})
return
Expand All @@ -86,13 +88,13 @@ func (h *Handler) UpdateProducts(c *gin.Context) {
}

// Create image
_, err = h.client.Image.Create(createdProduct.ID, aw.ShopifyImage())
_, err = h.Client.Image.Create(createdProduct.ID, aw.ShopifyImage())
if err != nil {
c.AbortWithStatusJSON(http.StatusBadGateway, gin.H{"error": err.Error()})
}

aw.UpdateShopifyProduct(createdProduct)
createdProduct, err = h.client.Product.Update(*createdProduct)
createdProduct, err = h.Client.Product.Update(*createdProduct)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadGateway, gin.H{"error": err.Error()})
}
Expand All @@ -111,7 +113,7 @@ func (h *Handler) UpdateProducts(c *gin.Context) {
// }
//
// glg.Warnf("deleting product %d: %s\n", p.ID, p.Title)
// err := h.client.Product.Delete(p.ID)
// err := h.Client.Product.Delete(p.ID)
// if err != nil {
// c.AbortWithStatusJSON(http.StatusBadGateway, gin.H{"error": err.Error()})
// }
Expand Down
22 changes: 22 additions & 0 deletions src/router/new.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package router

import (
goshopify "github.com/bold-commerce/go-shopify/v3"
"github.com/kpango/glg"

"github.com/cherevan.art/src/router/handler"
"github.com/cherevan.art/src/router/handler/shopify"
sopifyLib "github.com/cherevan.art/src/shopify"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
Expand All @@ -16,6 +20,7 @@ func New() *gin.Engine {
r.LoadHTMLGlob("src/templates/**/*")

shopifyHandler := shopify.Handler{}
attachClient(&shopifyHandler)

r.GET("/", handler.Root)
r.GET("/shopify/login", shopifyHandler.Login)
Expand All @@ -26,3 +31,20 @@ func New() *gin.Engine {

return r
}

func attachClient(h *shopify.Handler) {
config := sopifyLib.Config()

if config.AccessToken == "" {
glg.Warn("No access token for Shopify App, oAuth will be used.")
return // should be logged in as a development app through oAuth
}

app := sopifyLib.NewApp(config)
client := app.NewClient(
config.ShopName,
config.AccessToken,
goshopify.WithLogger(&goshopify.LeveledLogger{}))

h.Client = client
}
1 change: 1 addition & 0 deletions src/shopify/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type config struct {
ApiKey string `env:"SHOPIFY_API_KEY,required"`
ApiSecret string `env:"SHOPIFY_API_SECRET,required"`
RedirectUrl string `env:"SHOPIFY_REDIRECT_URL,required"`
AccessToken string `env:"SHOPIFY_ADMIN_API_ACCESS_TOKEN,required"`
}

func Config() *config {
Expand Down

0 comments on commit cc4cbfb

Please sign in to comment.