Skip to content

Commit

Permalink
v1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
fareskato committed Mar 3, 2024
1 parent 31d1c3a commit 7488102
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
6 changes: 3 additions & 3 deletions fkhttp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)
Expand All @@ -26,15 +27,14 @@ func HttpGet[T any](t T, url string) (*T, *http.Response, error) {
return response, res, nil
}

func HttpShopifyRequestWithHeaders(method string, ctx context.Context, url, token string, payload []byte) (*http.Response, error) {
func HttpShopifyRequestWithHeaders(method string, ctx context.Context, url string, payload []byte) (*http.Response, error) {
req, err := http.NewRequest(method, url, bytes.NewBuffer(payload))
if err != nil {
return nil, err
}
// set headers
req.Header.Set("X-Shopify-Access-Token", token)
req.Header.Set("Content-Type", "application/json")
// req.Header.Set("Content-Length", fmt.Sprint(len(payload)))
req.Header.Set("Content-Length", fmt.Sprint(len(payload)))
// context
req = req.WithContext(ctx)
// send the request
Expand Down
16 changes: 11 additions & 5 deletions products.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,28 @@ func (s Shopify) GetAllShopifyProducts(options ShopifyProductsFetchOptions) ([]s
return products, nil
}

func (s Shopify) PushProductImagesToShopify(ctx context.Context, token string, id int, images []ShopifyProductImage) error {
func (s Shopify) PushProductImagesToShopify(ctx context.Context, id int, images []ShopifyProductImage) error {
// prepare data
payload, err := json.Marshal(map[string]interface{}{"product": map[string]interface{}{"id": id, "images": images}})
data := map[string]interface{}{
"product": map[string]interface{}{
"id": id,
"images": images,
},
}
payload, err := json.Marshal(data)
if err != nil {
return err
}
url := fmt.Sprintf("%s/products/%d.json", s.InitStoreUrl(), id)
resp, err := fkhttp.HttpShopifyRequestWithHeaders(http.MethodPut, ctx, url, token, payload)
resp, err := fkhttp.HttpShopifyRequestWithHeaders(http.MethodPut, ctx, url, payload)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}

func (s Shopify) PushProductCostToshopify(ctx context.Context, token string, iiid int, cost string) error {
func (s Shopify) PushProductCostToshopify(ctx context.Context, iiid int, cost string) error {
// prepare data
data := map[string]interface{}{
"inventory_item": map[string]interface{}{
Expand All @@ -112,7 +118,7 @@ func (s Shopify) PushProductCostToshopify(ctx context.Context, token string, iii
return err
}
url := fmt.Sprintf("%s/inventory_items/%d.json", s.InitStoreUrl(), iiid)
resp, err := fkhttp.HttpShopifyRequestWithHeaders(http.MethodPut, ctx, url, token, payload)
resp, err := fkhttp.HttpShopifyRequestWithHeaders(http.MethodPut, ctx, url, payload)
if err != nil {
return err
}
Expand Down

0 comments on commit 7488102

Please sign in to comment.