Skip to content

Commit

Permalink
Make API version more flexible
Browse files Browse the repository at this point in the history
Allow the use of API versions that are not previsouly defined
  • Loading branch information
lizkenyon committed Sep 24, 2024
1 parent 20c9be9 commit d228d8e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
== Unreleased
- Remove requirement to use a predefined API version. Now you can use any valid API version string. ([#737](https://github.com/Shopify/shopify_python_api/pull/737))

== Version 12.6.0

Expand Down
4 changes: 4 additions & 0 deletions shopify/api_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def coerce_to_version(cls, version):
try:
return cls.versions[version]
except KeyError:
# Dynamically create a new Release object if version string is not found
if Release.FORMAT.match(version):
return Release(version)
raise VersionNotFoundError

@classmethod
Expand All @@ -39,6 +42,7 @@ def define_known_versions(cls):
cls.define_version(Release("2024-01"))
cls.define_version(Release("2024-04"))
cls.define_version(Release("2024-07"))
cls.define_version(Release("2024-10"))

@classmethod
def clear_defined_versions(cls):
Expand Down
14 changes: 14 additions & 0 deletions test/api_version_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ def test_coerce_to_version_raises_with_string_that_does_not_match_known_version(
with self.assertRaises(shopify.VersionNotFoundError):
shopify.ApiVersion.coerce_to_version("crazy-name")

def test_coerce_to_version_creates_new_release_on_the_fly(self):
new_version = "2025-01"
coerced_version = shopify.ApiVersion.coerce_to_version(new_version)

self.assertIsInstance(coerced_version, shopify.Release)
self.assertEqual(coerced_version.name, new_version)
self.assertEqual(
coerced_version.api_path("https://test.myshopify.com"),
f"https://test.myshopify.com/admin/api/{new_version}",
)

# Verify that the new version is not added to the known versions
self.assertNotIn(new_version, shopify.ApiVersion.versions)


class ReleaseTest(TestCase):
def test_raises_if_format_invalid(self):
Expand Down
13 changes: 13 additions & 0 deletions test/session_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,16 @@ def normalize_url(self, url):
scheme, netloc, path, query, fragment = urllib.parse.urlsplit(url)
query = "&".join(sorted(query.split("&")))
return urllib.parse.urlunsplit((scheme, netloc, path, query, fragment))

def test_session_with_coerced_version(self):
future_version = "2030-01"
session = shopify.Session("test.myshopify.com", future_version, "token")
self.assertEqual(session.api_version.name, future_version)
self.assertEqual(
session.api_version.api_path("https://test.myshopify.com"),
f"https://test.myshopify.com/admin/api/{future_version}",
)

def test_session_with_invalid_version(self):
with self.assertRaises(shopify.VersionNotFoundError):
shopify.Session("test.myshopify.com", "invalid-version", "token")

0 comments on commit d228d8e

Please sign in to comment.