flask-shopify-utils 0.3.3__tar.gz → 0.3.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (18) hide show
  1. {flask_shopify_utils-0.3.3 → flask_shopify_utils-0.3.4}/PKG-INFO +2 -2
  2. {flask_shopify_utils-0.3.3 → flask_shopify_utils-0.3.4}/pyproject.toml +3 -2
  3. {flask_shopify_utils-0.3.3 → flask_shopify_utils-0.3.4}/src/flask_shopify_utils/__init__.py +3 -3
  4. {flask_shopify_utils-0.3.3 → flask_shopify_utils-0.3.4}/src/flask_shopify_utils/utils.py +25 -15
  5. flask_shopify_utils-0.3.4/tests/test_get_version.py +119 -0
  6. {flask_shopify_utils-0.3.3 → flask_shopify_utils-0.3.4}/.gitignore +0 -0
  7. {flask_shopify_utils-0.3.3 → flask_shopify_utils-0.3.4}/LICENSE +0 -0
  8. {flask_shopify_utils-0.3.3 → flask_shopify_utils-0.3.4}/README.md +0 -0
  9. {flask_shopify_utils-0.3.3 → flask_shopify_utils-0.3.4}/src/flask_shopify_utils/cli/__init__.py +0 -0
  10. {flask_shopify_utils-0.3.3 → flask_shopify_utils-0.3.4}/src/flask_shopify_utils/cli/main.py +0 -0
  11. {flask_shopify_utils-0.3.3 → flask_shopify_utils-0.3.4}/src/flask_shopify_utils/model.py +0 -0
  12. {flask_shopify_utils-0.3.3 → flask_shopify_utils-0.3.4}/tests/conftest.py +0 -0
  13. {flask_shopify_utils-0.3.3 → flask_shopify_utils-0.3.4}/tests/test_alpha.py +0 -0
  14. {flask_shopify_utils-0.3.3 → flask_shopify_utils-0.3.4}/tests/test_cli_main.py +0 -0
  15. {flask_shopify_utils-0.3.3 → flask_shopify_utils-0.3.4}/tests/test_default_routes.py +0 -0
  16. {flask_shopify_utils-0.3.3 → flask_shopify_utils-0.3.4}/tests/test_gdpr_routes.py +0 -0
  17. {flask_shopify_utils-0.3.3 → flask_shopify_utils-0.3.4}/tests/test_graphql_cli.py +0 -0
  18. {flask_shopify_utils-0.3.3 → flask_shopify_utils-0.3.4}/tests/test_ui_routes.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: flask-shopify-utils
3
- Version: 0.3.3
3
+ Version: 0.3.4
4
4
  Summary: A Flask extension for Shopify app development
5
5
  Project-URL: Homepage, https://github.com/leocxy/flask-shopify-utils
6
6
  Project-URL: Bug Tracker, https://github.com/leocxy/flask-shopify-utils/issues
@@ -25,10 +25,10 @@ Requires-Dist: flask<4
25
25
  Requires-Dist: psutil<8
26
26
  Requires-Dist: pyjwt<3
27
27
  Requires-Dist: python-dateutil<3
28
- Requires-Dist: pytz==2026.1.post1
29
28
  Requires-Dist: requests<3
30
29
  Requires-Dist: sgqlc<19
31
30
  Requires-Dist: simplejson<4
31
+ Requires-Dist: tzdata
32
32
  Provides-Extra: dev
33
33
  Requires-Dist: build<2; extra == 'dev'
34
34
  Requires-Dist: flake8<8; extra == 'dev'
@@ -30,13 +30,14 @@ dependencies = [
30
30
  "Flask-SQLAlchemy < 4",
31
31
  "sgqlc < 19",
32
32
  "psutil < 8",
33
- "pytz == 2026.1.post1",
34
33
  "requests < 3",
35
34
  "Cerberus < 2",
36
35
  "PyJWT < 3",
37
36
  "simplejson < 4",
38
37
  "click < 9",
39
- "python-dateutil < 3"
38
+ "python-dateutil < 3",
39
+ # pytz is deprecated and replace with zoneinfo.ZoneInfo + tzdata
40
+ "tzdata"
40
41
  ]
41
42
  dynamic = ["version"]
42
43
 
@@ -20,6 +20,7 @@ from urllib.parse import urlencode
20
20
  from base64 import b64encode
21
21
  from contextlib import contextmanager
22
22
  from sys import platform
23
+ from zoneinfo import ZoneInfo
23
24
  # Third-party Library
24
25
  from flask import Flask, request, g, jsonify, Response, current_app, Blueprint, redirect, render_template, \
25
26
  make_response, url_for
@@ -27,10 +28,9 @@ from jinja2 import TemplateNotFound
27
28
  from jwt import encode as jwt_encode, decode as jwt_decode, \
28
29
  ExpiredSignatureError, InvalidAudienceError, InvalidSignatureError, InvalidTokenError
29
30
  from cerberus.validator import Validator
30
- from pytz import timezone
31
31
  from flask_shopify_utils.utils import get_version, GraphQLClient
32
32
 
33
- __version__ = '0.3.3'
33
+ __version__ = '0.3.4'
34
34
 
35
35
  current_time_func = None
36
36
  sqlalchemy_instance = None
@@ -74,7 +74,7 @@ class ShopifyUtil:
74
74
  app.config.setdefault('BACKEND_PATH', path.join(app.config.get('ROOT_PATH'), 'backend'))
75
75
  app.config.setdefault('TEMPORARY_PATH', path.join(app.config.get('BACKEND_PATH'), 'tmp'))
76
76
  app.config.setdefault('API_VERSION', get_version())
77
- app.config.setdefault('TIMEZONE', timezone('Pacific/Auckland'))
77
+ app.config.setdefault('TIMEZONE', ZoneInfo('Pacific/Auckland'))
78
78
  app.config.setdefault('SHOPIFY_API_SECRET', 'CUSTOM_APP_SECRET')
79
79
  app.config.setdefault('SHOPIFY_API_KEY', 'CUSTOM_APP_KEY')
80
80
  app.config.setdefault('BYPASS_VALIDATE', 0)
@@ -6,8 +6,10 @@
6
6
  # @Author : Leo Chen<leo.cxy88@gmail.com>
7
7
  # @Date : 27/05/23 3:16 pm
8
8
  """
9
+ from re import match as re_match
9
10
  from os import environ
10
11
  from datetime import datetime
12
+ from dateutil.relativedelta import relativedelta
11
13
  from time import sleep
12
14
  from sgqlc.operation import Operation
13
15
  from sgqlc.endpoint.http import HTTPEndpoint
@@ -16,33 +18,41 @@ from urllib3.util.retry import Retry
16
18
  from requests.adapters import HTTPAdapter
17
19
  from typing import Tuple, Union
18
20
 
21
+ VERSION_KEY = 'API_VERSION'
22
+
19
23
 
20
24
  def get_version(version: str = None) -> str:
21
25
  """ Get Shopify API Version """
22
26
  # Get Latest version of GraphQL
23
27
  today = datetime.today()
24
- month = int(today.strftime('%-m'))
28
+ month = int(today.strftime('%m'))
25
29
  for v in [10, 7, 4, 1]:
26
30
  if month >= v:
27
31
  month = v
28
32
  break
29
- latest_version = int('{}{:02d}'.format(today.strftime('%Y'), month))
30
- version = environ.get('API_VERSION', '2023-04') if not version else version
33
+ latest_date = int('{}{:02d}'.format(today.strftime('%Y'), month))
34
+ earliest_date = int('{}{:02d}'.format((today - relativedelta(years=1)).strftime('%Y'), month))
35
+ latest_version = '{}-{:02d}'.format(today.strftime('%Y'), month)
36
+
37
+ # validate the version format: "xxxx-xx", all digits, first digit 1-9
38
+ force = False
31
39
  if version:
32
- version = int(version.replace('-', ''))
33
- # check version exists or not
34
- if version > latest_version:
35
- version = latest_version
36
- else:
37
- # Check deprecate
38
- if latest_version - 100 > version:
39
- version = latest_version
40
+ val = version
41
+ force = True
40
42
  else:
41
- version = latest_version
42
- version = str(version)
43
- version = '{}-{}'.format(version[:4], version[-2:])
43
+ val = environ.get(VERSION_KEY, '2026-04')
44
+ if not re_match(r'^[1-9]\d{3}-\d{2}$', val):
45
+ raise ValueError('Invalid API version format: {}'.format(val))
46
+ val_date = int(val.replace('-', ''))
47
+
48
+ # Check if the version is within the valid range
49
+ if val_date > latest_date or val_date < earliest_date:
50
+ version = latest_version if force is False else val
51
+ else:
52
+ version = val
53
+
44
54
  # Set Environment variable
45
- environ['API_VERSION'] = version
55
+ environ[VERSION_KEY] = version
46
56
  return version
47
57
 
48
58
 
@@ -0,0 +1,119 @@
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ # @Project : flask-shopify-utils
5
+ # @File : test_get_version.py
6
+ # @Author : Leo Chen<leo.cxy88@gmail.com>
7
+ # @Date : 27/05/23 3:16 pm
8
+ """
9
+ from os import environ
10
+ from datetime import datetime
11
+ from unittest import TestCase
12
+ from unittest.mock import patch
13
+ from pytest import fixture, raises, mark
14
+ from flask_shopify_utils.utils import get_version, VERSION_KEY
15
+
16
+
17
+ @fixture(scope='function')
18
+ def test() -> TestCase:
19
+ """ TestCase helper + keep the `API_VERSION` environment variable untouched """
20
+ origin = environ.get(VERSION_KEY)
21
+ environ.pop(VERSION_KEY, None)
22
+ case = TestCase()
23
+ case.maxDiff = None
24
+ yield case
25
+ environ.pop(VERSION_KEY, None)
26
+ if origin is not None:
27
+ environ[VERSION_KEY] = origin
28
+
29
+
30
+ def freeze(year: int, month: int, day: int = 15):
31
+ """ Patch `datetime` within the utils module, so `datetime.today()` is predictable """
32
+ return patch('flask_shopify_utils.utils.datetime', **{'today.return_value': datetime(year, month, day)})
33
+
34
+
35
+ @mark.parametrize('month, expected', [
36
+ (1, '2025-01'), (2, '2025-01'), (3, '2025-01'),
37
+ (4, '2025-04'), (5, '2025-04'), (6, '2025-04'),
38
+ (7, '2025-07'), (8, '2025-07'), (9, '2025-07'),
39
+ (10, '2025-10'), (11, '2025-10'), (12, '2025-10'),
40
+ ])
41
+ def test_latest_version_per_month(test, month, expected) -> None:
42
+ """ The latest version always falls back to the Jan/Apr/Jul/Oct release """
43
+ with freeze(2025, month):
44
+ # `2000-01` is way too old, so the latest version will be used
45
+ environ[VERSION_KEY] = '2000-01'
46
+ test.assertEqual(expected, get_version())
47
+
48
+
49
+ @mark.parametrize('value', ['abc', '202601', '2026-1', '26-01', '0000-01', '2026-01-01', ' 2026-01'])
50
+ def test_invalid_format(test, value) -> None:
51
+ """ Invalid version format raises `ValueError` """
52
+ with raises(ValueError) as err:
53
+ get_version(value)
54
+ test.assertEqual('Invalid API version format: {}'.format(value), str(err.value))
55
+ # the environment variable should not be updated
56
+ test.assertIsNone(environ.get(VERSION_KEY))
57
+
58
+
59
+ @mark.parametrize('value', ['', 'abc', '202601', '0000-01'])
60
+ def test_invalid_format_from_environment_variable(test, value) -> None:
61
+ """ Invalid version format from the environment variable raises `ValueError` """
62
+ environ[VERSION_KEY] = value
63
+ with raises(ValueError) as err:
64
+ get_version()
65
+ test.assertEqual('Invalid API version format: {}'.format(value), str(err.value))
66
+
67
+
68
+ def test_default_value(test) -> None:
69
+ """ Without the environment variable, the hardcoded default `2026-04` is used """
70
+ with freeze(2026, 4):
71
+ test.assertEqual('2026-04', get_version())
72
+ # the environment variable should be updated
73
+ test.assertEqual('2026-04', environ.get(VERSION_KEY))
74
+
75
+
76
+ def test_environment_variable(test) -> None:
77
+ """ A valid version from the environment variable is respected """
78
+ environ[VERSION_KEY] = '2025-04'
79
+ with freeze(2025, 10):
80
+ test.assertEqual('2025-04', get_version())
81
+ test.assertEqual('2025-04', environ.get(VERSION_KEY))
82
+
83
+
84
+ def test_environment_variable_out_of_range(test) -> None:
85
+ """ Too old / too new version from the environment variable falls back to the latest one """
86
+ with freeze(2025, 10):
87
+ # older than 1 year
88
+ environ[VERSION_KEY] = '2024-07'
89
+ test.assertEqual('2025-10', get_version())
90
+ # newer than the latest release
91
+ environ[VERSION_KEY] = '2026-01'
92
+ test.assertEqual('2025-10', get_version())
93
+ test.assertEqual('2025-10', environ.get(VERSION_KEY))
94
+
95
+
96
+ def test_environment_variable_boundary(test) -> None:
97
+ """ Both boundaries(latest / earliest) are valid values """
98
+ with freeze(2025, 10):
99
+ environ[VERSION_KEY] = '2025-10'
100
+ test.assertEqual('2025-10', get_version())
101
+ environ[VERSION_KEY] = '2024-10'
102
+ test.assertEqual('2024-10', get_version())
103
+
104
+
105
+ def test_force_version(test) -> None:
106
+ """ The given version wins over the environment variable """
107
+ environ[VERSION_KEY] = '2025-04'
108
+ with freeze(2025, 10):
109
+ test.assertEqual('2025-07', get_version('2025-07'))
110
+ test.assertEqual('2025-07', environ.get(VERSION_KEY))
111
+
112
+
113
+ def test_force_version_out_of_range(test) -> None:
114
+ """ The given version is used as-is, even if it is out of the valid range """
115
+ with freeze(2025, 10):
116
+ test.assertEqual('2020-01', get_version('2020-01'))
117
+ test.assertEqual('2020-01', environ.get(VERSION_KEY))
118
+ test.assertEqual('2099-10', get_version('2099-10'))
119
+ test.assertEqual('2099-10', environ.get(VERSION_KEY))