forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding WithRetry option (bold-commerce#87)
* adding WithRetry option * refactor usagecharge test, added go 1.14 and bionic (bold-commerce#97) Thank you very much for this contribution.
- Loading branch information
1 parent
1dad68b
commit 6272228
Showing
4 changed files
with
248 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package goshopify | ||
|
||
import "fmt" | ||
|
||
// Option is used to configure client with options | ||
type Option func(c *Client) | ||
|
||
// WithVersion optionally sets the api-version if the passed string is valid | ||
func WithVersion(apiVersion string) Option { | ||
return func(c *Client) { | ||
pathPrefix := defaultApiPathPrefix | ||
if len(apiVersion) > 0 && apiVersionRegex.MatchString(apiVersion) { | ||
pathPrefix = fmt.Sprintf("admin/api/%s", apiVersion) | ||
} | ||
c.apiVersion = apiVersion | ||
c.pathPrefix = pathPrefix | ||
} | ||
} | ||
|
||
func WithRetry(retries int) Option { | ||
return func(c *Client) { | ||
c.retries = retries | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package goshopify | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestWithVersion(t *testing.T) { | ||
c := NewClient(app, "fooshop", "abcd", WithVersion(testApiVersion)) | ||
expected := fmt.Sprintf("admin/api/%s", testApiVersion) | ||
if c.pathPrefix != expected { | ||
t.Errorf("WithVersion client.pathPrefix = %s, expected %s", c.pathPrefix, expected) | ||
} | ||
} | ||
|
||
func TestWithVersionNoVersion(t *testing.T) { | ||
c := NewClient(app, "fooshop", "abcd", WithVersion("")) | ||
expected := "admin" | ||
if c.pathPrefix != expected { | ||
t.Errorf("WithVersion client.pathPrefix = %s, expected %s", c.pathPrefix, expected) | ||
} | ||
} | ||
|
||
func TestWithoutVersionInInitiation(t *testing.T) { | ||
c := NewClient(app, "fooshop", "abcd") | ||
expected := "admin" | ||
if c.pathPrefix != expected { | ||
t.Errorf("WithVersion client.pathPrefix = %s, expected %s", c.pathPrefix, expected) | ||
} | ||
} | ||
|
||
func TestWithVersionInvalidVersion(t *testing.T) { | ||
c := NewClient(app, "fooshop", "abcd", WithVersion("9999-99b")) | ||
expected := "admin" | ||
if c.pathPrefix != expected { | ||
t.Errorf("WithVersion client.pathPrefix = %s, expected %s", c.pathPrefix, expected) | ||
} | ||
} | ||
|
||
func TestWithRetry(t *testing.T) { | ||
c := NewClient(app, "fooshop", "abcd", WithRetry(5)) | ||
expected := 5 | ||
if c.retries != expected { | ||
t.Errorf("WithRetry client.retries = %d, expected %d", c.retries, expected) | ||
} | ||
} |