Skip to content

Commit fdc155e

Browse files
committed
Make API version more flexible
Allow the use of API versions that are not previsouly defined
1 parent 20c9be9 commit fdc155e

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
== Unreleased
2+
- 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))
23

34
== Version 12.6.0
45

shopify/api_version.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ def coerce_to_version(cls, version):
1717
try:
1818
return cls.versions[version]
1919
except KeyError:
20+
# Dynamically create a new Release object if version string is not found
21+
if Release.FORMAT.match(version):
22+
return Release(version)
2023
raise VersionNotFoundError
2124

2225
@classmethod
@@ -39,6 +42,7 @@ def define_known_versions(cls):
3942
cls.define_version(Release("2024-01"))
4043
cls.define_version(Release("2024-04"))
4144
cls.define_version(Release("2024-07"))
45+
cls.define_version(Release("2024-10"))
4246

4347
@classmethod
4448
def clear_defined_versions(cls):

test/api_version_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ def test_coerce_to_version_raises_with_string_that_does_not_match_known_version(
2929
with self.assertRaises(shopify.VersionNotFoundError):
3030
shopify.ApiVersion.coerce_to_version("crazy-name")
3131

32+
def test_coerce_to_version_creates_new_release_on_the_fly(self):
33+
new_version = "2025-01"
34+
coerced_version = shopify.ApiVersion.coerce_to_version(new_version)
35+
36+
self.assertIsInstance(coerced_version, shopify.Release)
37+
self.assertEqual(coerced_version.name, new_version)
38+
self.assertEqual(
39+
coerced_version.api_path("https://test.myshopify.com"),
40+
f"https://test.myshopify.com/admin/api/{new_version}",
41+
)
42+
43+
# Verify that the new version is not added to the known versions
44+
self.assertNotIn(new_version, shopify.ApiVersion.versions)
45+
3246

3347
class ReleaseTest(TestCase):
3448
def test_raises_if_format_invalid(self):

test/session_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,16 @@ def normalize_url(self, url):
288288
scheme, netloc, path, query, fragment = urllib.parse.urlsplit(url)
289289
query = "&".join(sorted(query.split("&")))
290290
return urllib.parse.urlunsplit((scheme, netloc, path, query, fragment))
291+
292+
def test_session_with_coerced_version(self):
293+
future_version = "2030-01"
294+
session = shopify.Session("test.myshopify.com", future_version, "token")
295+
self.assertEqual(session.api_version.name, future_version)
296+
self.assertEqual(
297+
session.api_version.api_path("https://test.myshopify.com"),
298+
f"https://test.myshopify.com/admin/api/{future_version}",
299+
)
300+
301+
def test_session_with_invalid_version(self):
302+
with self.assertRaises(shopify.VersionNotFoundError):
303+
shopify.Session("test.myshopify.com", "invalid-version", "token")

0 commit comments

Comments
 (0)