Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add context.Context propagation #257

Merged
merged 6 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions abandoned_checkout.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package goshopify

import (
"context"
"fmt"
"time"

Expand All @@ -13,7 +14,7 @@ const abandonedCheckoutsBasePath = "checkouts"
// of the Shopify API.
// See: https://shopify.dev/docs/api/admin-rest/latest/resources/abandoned-checkouts
type AbandonedCheckoutService interface {
List(interface{}) ([]AbandonedCheckout, error)
List(context.Context, interface{}) ([]AbandonedCheckout, error)
}

// AbandonedCheckoutServiceOp handles communication with the checkout related methods of
Expand Down Expand Up @@ -83,9 +84,9 @@ type SmsMarketingConsent struct {
}

// Get abandoned checkout list
func (s *AbandonedCheckoutServiceOp) List(options interface{}) ([]AbandonedCheckout, error) {
func (s *AbandonedCheckoutServiceOp) List(ctx context.Context, options interface{}) ([]AbandonedCheckout, error) {
path := fmt.Sprintf("/%s.json", abandonedCheckoutsBasePath)
resource := new(AbandonedCheckoutsResource)
err := s.client.Get(path, resource, options)
err := s.client.Get(ctx, path, resource, options)
return resource.AbandonedCheckouts, err
}
4 changes: 2 additions & 2 deletions abandoned_checkout_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package goshopify

import (
"context"
"fmt"
"reflect"
"testing"
Expand All @@ -21,7 +22,7 @@ func TestAbandonedCheckoutList(t *testing.T) {
),
)

abandonedCheckouts, err := client.AbandonedCheckout.List(nil)
abandonedCheckouts, err := client.AbandonedCheckout.List(context.Background(), nil)
if err != nil {
t.Errorf("AbandonedCheckout.List returned error: %v", err)
}
Expand All @@ -30,5 +31,4 @@ func TestAbandonedCheckoutList(t *testing.T) {
if !reflect.DeepEqual(abandonedCheckouts, expected) {
t.Errorf("AbandonedCheckout.List returned %+v, expected %+v", abandonedCheckouts, expected)
}

}
8 changes: 5 additions & 3 deletions access_scopes.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package goshopify

import "context"

type AccessScopesService interface {
List(interface{}) ([]AccessScope, error)
List(context.Context, interface{}) ([]AccessScope, error)
}

type AccessScope struct {
Expand All @@ -20,9 +22,9 @@ type AccessScopesServiceOp struct {
}

// List gets access scopes based on used oauth token
func (s *AccessScopesServiceOp) List(options interface{}) ([]AccessScope, error) {
func (s *AccessScopesServiceOp) List(ctx context.Context, options interface{}) ([]AccessScope, error) {
path := "oauth/access_scopes.json"
resource := new(AccessScopesResource)
err := s.client.Get(path, resource, options)
err := s.client.Get(ctx, path, resource, options)
return resource.AccessScopes, err
}
3 changes: 2 additions & 1 deletion access_scopes_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package goshopify

import (
"context"
"fmt"
"reflect"
"testing"
Expand All @@ -18,7 +19,7 @@ func TestAccessScopesServiceOp_List(t *testing.T) {
httpmock.NewBytesResponder(200, loadFixture("access_scopes.json")),
)

scopeResponse, err := client.AccessScopes.List(nil)
scopeResponse, err := client.AccessScopes.List(context.Background(), nil)
if err != nil {
t.Errorf("AccessScopes.List returned an error: %v", err)
}
Expand Down
25 changes: 13 additions & 12 deletions applicationcharge.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package goshopify

import (
"context"
"fmt"
"time"

Expand All @@ -13,10 +14,10 @@ const applicationChargesBasePath = "application_charges"
// ApplicationCharge endpoints of the Shopify API.
// See https://help.shopify.com/api/reference/billing/applicationcharge
type ApplicationChargeService interface {
Create(ApplicationCharge) (*ApplicationCharge, error)
Get(int64, interface{}) (*ApplicationCharge, error)
List(interface{}) ([]ApplicationCharge, error)
Activate(ApplicationCharge) (*ApplicationCharge, error)
Create(context.Context, ApplicationCharge) (*ApplicationCharge, error)
Get(context.Context, int64, interface{}) (*ApplicationCharge, error)
List(context.Context, interface{}) ([]ApplicationCharge, error)
Activate(context.Context, ApplicationCharge) (*ApplicationCharge, error)
}

type ApplicationChargeServiceOp struct {
Expand Down Expand Up @@ -51,29 +52,29 @@ type ApplicationChargesResource struct {
}

// Create creates new application charge.
func (a ApplicationChargeServiceOp) Create(charge ApplicationCharge) (*ApplicationCharge, error) {
func (a ApplicationChargeServiceOp) Create(ctx context.Context, charge ApplicationCharge) (*ApplicationCharge, error) {
path := fmt.Sprintf("%s.json", applicationChargesBasePath)
resource := &ApplicationChargeResource{}
return resource.Charge, a.client.Post(path, ApplicationChargeResource{Charge: &charge}, resource)
return resource.Charge, a.client.Post(ctx, path, ApplicationChargeResource{Charge: &charge}, resource)
}

// Get gets individual application charge.
func (a ApplicationChargeServiceOp) Get(chargeID int64, options interface{}) (*ApplicationCharge, error) {
func (a ApplicationChargeServiceOp) Get(ctx context.Context, chargeID int64, options interface{}) (*ApplicationCharge, error) {
path := fmt.Sprintf("%s/%d.json", applicationChargesBasePath, chargeID)
resource := &ApplicationChargeResource{}
return resource.Charge, a.client.Get(path, resource, options)
return resource.Charge, a.client.Get(ctx, path, resource, options)
}

// List gets all application charges.
func (a ApplicationChargeServiceOp) List(options interface{}) ([]ApplicationCharge, error) {
func (a ApplicationChargeServiceOp) List(ctx context.Context, options interface{}) ([]ApplicationCharge, error) {
path := fmt.Sprintf("%s.json", applicationChargesBasePath)
resource := &ApplicationChargesResource{}
return resource.Charges, a.client.Get(path, resource, options)
return resource.Charges, a.client.Get(ctx, path, resource, options)
}

// Activate activates application charge.
func (a ApplicationChargeServiceOp) Activate(charge ApplicationCharge) (*ApplicationCharge, error) {
func (a ApplicationChargeServiceOp) Activate(ctx context.Context, charge ApplicationCharge) (*ApplicationCharge, error) {
path := fmt.Sprintf("%s/%d/activate.json", applicationChargesBasePath, charge.ID)
resource := &ApplicationChargeResource{}
return resource.Charge, a.client.Post(path, ApplicationChargeResource{Charge: &charge}, resource)
return resource.Charge, a.client.Post(ctx, path, ApplicationChargeResource{Charge: &charge}, resource)
}
9 changes: 5 additions & 4 deletions applicationcharge_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package goshopify

import (
"context"
"fmt"
"reflect"
"testing"
Expand Down Expand Up @@ -63,7 +64,7 @@ func TestApplicationChargeServiceOp_Create(t *testing.T) {
ReturnURL: "http://super-duper.shopifyapps.com",
}

returnedCharge, err := client.ApplicationCharge.Create(charge)
returnedCharge, err := client.ApplicationCharge.Create(context.Background(), charge)
if err != nil {
t.Errorf("ApplicationCharge.Create returned an error: %v", err)
}
Expand All @@ -81,7 +82,7 @@ func TestApplicationChargeServiceOp_Get(t *testing.T) {
httpmock.NewStringResponder(200, `{"application_charge": {"id":1}}`),
)

charge, err := client.ApplicationCharge.Get(1, nil)
charge, err := client.ApplicationCharge.Get(context.Background(), 1, nil)
if err != nil {
t.Errorf("ApplicationCharge.Get returned an error: %v", err)
}
Expand All @@ -102,7 +103,7 @@ func TestApplicationChargeServiceOp_List(t *testing.T) {
httpmock.NewStringResponder(200, `{"application_charges": [{"id":1},{"id":2}]}`),
)

charges, err := client.ApplicationCharge.List(nil)
charges, err := client.ApplicationCharge.List(context.Background(), nil)
if err != nil {
t.Errorf("ApplicationCharge.List returned an error: %v", err)
}
Expand Down Expand Up @@ -131,7 +132,7 @@ func TestApplicationChargeServiceOp_Activate(t *testing.T) {
Status: "accepted",
}

returnedCharge, err := client.ApplicationCharge.Activate(charge)
returnedCharge, err := client.ApplicationCharge.Activate(context.Background(), charge)
if err != nil {
t.Errorf("ApplicationCharge.Activate returned an error: %v", err)
}
Expand Down
25 changes: 13 additions & 12 deletions asset.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package goshopify

import (
"context"
"fmt"
"time"
)
Expand All @@ -11,10 +12,10 @@ const assetsBasePath = "themes"
// of the Shopify API.
// See: https://help.shopify.com/api/reference/asset
type AssetService interface {
List(int64, interface{}) ([]Asset, error)
Get(int64, string) (*Asset, error)
Update(int64, Asset) (*Asset, error)
Delete(int64, string) error
List(context.Context, int64, interface{}) ([]Asset, error)
Get(context.Context, int64, string) (*Asset, error)
Update(context.Context, int64, Asset) (*Asset, error)
Delete(context.Context, int64, string) error
}

// AssetServiceOp handles communication with the asset related methods of
Expand Down Expand Up @@ -54,36 +55,36 @@ type assetGetOptions struct {
}

// List the metadata for all assets in the given theme
func (s *AssetServiceOp) List(themeID int64, options interface{}) ([]Asset, error) {
func (s *AssetServiceOp) List(ctx context.Context, themeID int64, options interface{}) ([]Asset, error) {
path := fmt.Sprintf("%s/%d/assets.json", assetsBasePath, themeID)
resource := new(AssetsResource)
err := s.client.Get(path, resource, options)
err := s.client.Get(ctx, path, resource, options)
return resource.Assets, err
}

// Get an asset by key from the given theme
func (s *AssetServiceOp) Get(themeID int64, key string) (*Asset, error) {
func (s *AssetServiceOp) Get(ctx context.Context, themeID int64, key string) (*Asset, error) {
path := fmt.Sprintf("%s/%d/assets.json", assetsBasePath, themeID)
options := assetGetOptions{
Key: key,
ThemeID: themeID,
}
resource := new(AssetResource)
err := s.client.Get(path, resource, options)
err := s.client.Get(ctx, path, resource, options)
return resource.Asset, err
}

// Update an asset
func (s *AssetServiceOp) Update(themeID int64, asset Asset) (*Asset, error) {
func (s *AssetServiceOp) Update(ctx context.Context, themeID int64, asset Asset) (*Asset, error) {
path := fmt.Sprintf("%s/%d/assets.json", assetsBasePath, themeID)
wrappedData := AssetResource{Asset: &asset}
resource := new(AssetResource)
err := s.client.Put(path, wrappedData, resource)
err := s.client.Put(ctx, path, wrappedData, resource)
return resource.Asset, err
}

// Delete an asset
func (s *AssetServiceOp) Delete(themeID int64, key string) error {
func (s *AssetServiceOp) Delete(ctx context.Context, themeID int64, key string) error {
path := fmt.Sprintf("%s/%d/assets.json?asset[key]=%s", assetsBasePath, themeID, key)
return s.client.Delete(path)
return s.client.Delete(ctx, path)
}
9 changes: 5 additions & 4 deletions asset_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package goshopify

import (
"context"
"fmt"
"reflect"
"testing"
Expand All @@ -21,7 +22,7 @@ func TestAssetList(t *testing.T) {
),
)

assets, err := client.Asset.List(1, nil)
assets, err := client.Asset.List(context.Background(), 1, nil)
if err != nil {
t.Errorf("Asset.List returned error: %v", err)
}
Expand Down Expand Up @@ -49,7 +50,7 @@ func TestAssetGet(t *testing.T) {
),
)

asset, err := client.Asset.Get(1, "foo/bar.liquid")
asset, err := client.Asset.Get(context.Background(), 1, "foo/bar.liquid")
if err != nil {
t.Errorf("Asset.Get returned error: %v", err)
}
Expand Down Expand Up @@ -78,7 +79,7 @@ func TestAssetUpdate(t *testing.T) {
Value: "content",
}

returnedAsset, err := client.Asset.Update(1, asset)
returnedAsset, err := client.Asset.Update(context.Background(), 1, asset)
if err != nil {
t.Errorf("Asset.Update returned error: %v", err)
}
Expand All @@ -99,7 +100,7 @@ func TestAssetDelete(t *testing.T) {
httpmock.NewStringResponder(200, "{}"),
)

err := client.Asset.Delete(1, "foo/bar.liquid")
err := client.Asset.Delete(context.Background(), 1, "foo/bar.liquid")
if err != nil {
t.Errorf("Asset.Delete returned error: %v", err)
}
Expand Down
11 changes: 7 additions & 4 deletions assigned_fulfillment_order.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package goshopify

import "fmt"
import (
"context"
"fmt"
)

const (
assignedFulfillmentOrderBasePath = "assigned_fulfillment_orders"
Expand All @@ -10,7 +13,7 @@ const (
// of the Shopify API.
// https://shopify.dev/docs/api/admin-rest/2023-07/resources/assignedfulfillmentorder
type AssignedFulfillmentOrderService interface {
Get(interface{}) ([]AssignedFulfillmentOrder, error)
Get(context.Context, interface{}) ([]AssignedFulfillmentOrder, error)
}

type AssignedFulfillmentOrder struct {
Expand Down Expand Up @@ -68,9 +71,9 @@ type AssignedFulfillmentOrderServiceOp struct {
}

// Gets a list of all the fulfillment orders that are assigned to an app at the shop level
func (s *AssignedFulfillmentOrderServiceOp) Get(options interface{}) ([]AssignedFulfillmentOrder, error) {
func (s *AssignedFulfillmentOrderServiceOp) Get(ctx context.Context, options interface{}) ([]AssignedFulfillmentOrder, error) {
path := fmt.Sprintf("%s.json", assignedFulfillmentOrderBasePath)
resource := new(AssignedFulfillmentOrdersResource)
err := s.client.Get(path, resource, options)
err := s.client.Get(ctx, path, resource, options)
return resource.AssignedFulfillmentOrders, err
}
5 changes: 3 additions & 2 deletions assigned_fulfillment_order_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package goshopify

import (
"context"
"fmt"
"reflect"
"testing"
Expand All @@ -25,7 +26,7 @@ func TestAssignedFulfillmentOrderGet(t *testing.T) {

assignedFulfillmentOrderService := &AssignedFulfillmentOrderServiceOp{client: client}

assignedFulfillmentOrders, err := assignedFulfillmentOrderService.Get(nil)
assignedFulfillmentOrders, err := assignedFulfillmentOrderService.Get(context.Background(), nil)
if err != nil {
t.Errorf("AssignedFulfillmentOrder.List returned error: %v", err)
}
Expand All @@ -46,7 +47,7 @@ func TestAssignedFulfillmentOrderGet(t *testing.T) {

// fulfillmentOrderService := &FulfillmentOrderServiceOp{client: client}

// fulfillment, err := fulfillmentOrderService.Get(255858046, nil)
// fulfillment, err := fulfillmentOrderService.Get(context.Background(), 255858046, nil)
// if err != nil {
// t.Errorf("FulfillmentOrder.Get returned error: %v", err)
// }
Expand Down
Loading
Loading