codecov[bot]: # [Codecov](https://codecov.io/gh/Stranger6667/Flask-Postmark/pull/9?src=pr&el=h1) Report
> Merging [#9](https://codecov.io/gh/Stranger6667/Flask-Postmark/pull/9?src=pr&el=desc) into [master](https://codecov.io/gh/Stranger6667/Flask-Postmark/commit/a717b345eb0a59aa10fcc5464dfd3b8d9e151824?src=pr&el=desc) will **increase** coverage by `8.41%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/Stranger6667/Flask-Postmark/pull/9/graphs/tree.svg?src=pr&width=650&token=u3UDepU7Mx&height=150)](https://codecov.io/gh/Stranger6667/Flask-Postmark/pull/9?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master       #9      +/-   ##
==========================================
+ Coverage   88.46%   96.87%   +8.41%     
==========================================
  Files           2        2              
  Lines          26       32       +6     
  Branches        4        5       +1     
==========================================
+ Hits           23       31       +8     
+ Partials        3        1       -2
```


| [Impacted Files](https://codecov.io/gh/Stranger6667/Flask-Postmark/pull/9?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [flask\_postmark/core.py](https://codecov.io/gh/Stranger6667/Flask-Postmark/pull/9?src=pr&el=tree#diff-Zmxhc2tfcG9zdG1hcmsvY29yZS5weQ==) | `96.66% <100%> (+9.16%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Stranger6667/Flask-Postmark/pull/9?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/Stranger6667/Flask-Postmark/pull/9?src=pr&el=footer). Last update [a717b34...95135e4](https://codecov.io/gh/Stranger6667/Flask-Postmark/pull/9?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

diff --git a/docs/changelog.rst b/docs/changelog.rst
index e39741d..25e60ab 100644
--- a/docs/changelog.rst
+++ b/docs/changelog.rst
@@ -10,6 +10,7 @@ Added
 ~~~~~
 
 - Register the extension on the app. `#6`_
+- Use ``self.app`` as a fallback. `#5`_
 
 0.1.0 - 2017-05-11
 ------------------
@@ -20,3 +21,4 @@ Added
 
 
 .. _#6: https://github.com/Stranger6667/Flask-Postmark/issues/6
+.. _#5: https://github.com/Stranger6667/Flask-Postmark/issues/5
diff --git a/flask_postmark/core.py b/flask_postmark/core.py
index ccbdd8a..d1fd990 100644
--- a/flask_postmark/core.py
+++ b/flask_postmark/core.py
@@ -5,6 +5,7 @@ from postmarker.core import PostmarkClient
 
 
 class Postmark(object):
+    app = None
 
     def __init__(self, app=None):
         self.app = app
@@ -15,6 +16,11 @@ class Postmark(object):
         app.extensions['postmark'] = self
         app.teardown_appcontext(self.teardown)
 
+    def _get_app(self):
+        if not current_app and self.app:
+            return self.app
+        return current_app
+
     def teardown(self, exception):
         ctx = stack.top
         if hasattr(ctx, 'postmark_client'):
@@ -25,7 +31,8 @@ class Postmark(object):
         ctx = stack.top
         if ctx is not None:
             if not hasattr(ctx, 'postmark_client'):
-                ctx.postmark_client = PostmarkClient.from_config(current_app.config, is_uppercase=True)
+                app = self._get_app()
+                ctx.postmark_client = PostmarkClient.from_config(app.config, is_uppercase=True)
             return ctx.postmark_client
 
     def send(self, *args, **kwargs):

Don't store app unless you fall back to it
In `__init__`, you set `self.app = app`, but you never use this value. The pattern for this is to fall back to it if `current_app` is not set, as a convenience to not need an app context when not using deferred init. Some devs prefer to exclude that and only allow `with app.app_context()`.

It's up to you how you want to handle this. Either continue to require a context and remove `self.app`, or use something like the following:

~~~python
class Postmark:
    app = None

    def _get_app(self):
        if not current_app and self.app:
            return self.app

        return current_app

    def session(self):
        self._get_app().config
~~~
    
diff --git a/tests/conftest.py b/tests/conftest.py
index 6c44173..12d4e3e 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -24,6 +24,10 @@ def app(server_token, postmark_request):
         postmark.send(**data)
         return make_response()
 
+    @app.route('/is_same_client', methods=['POST'])
+    def is_same_client():
+        return json.dumps(postmark.client is postmark.client)
+
     @app.route('/send_batch', methods=['POST'])
     def send_batch():
         data = request.get_json()
diff --git a/tests/test_postmark.py b/tests/test_postmark.py
index 8142fa1..ffd268a 100644
--- a/tests/test_postmark.py
+++ b/tests/test_postmark.py
@@ -44,8 +44,19 @@ class TestPostmark:
         }
         assert data == [expected, expected]
 
+    def test_is_same_client(self):
+        assert self.post('/is_same_client', DATA) is True
+
     def test_empty_app(self, app):
         assert len(app.teardown_appcontext_funcs) == 1
         postmark = Postmark()
         postmark.init_app(app)
         assert len(app.teardown_appcontext_funcs) == 2
+
+    def test_get_app(self, app):
+        postmark = Postmark(app)
+        assert postmark._get_app() is app
+
+    def test_no_context(self, app):
+        postmark = Postmark(app)
+        assert postmark.client is None

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest pytest-cov mock
apt-get update apt-get install -y gcc
3.6
docs/requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
attrs==22.2.0
certifi==2021.5.30
charset-normalizer==2.0.12
click==8.0.4
coverage==6.2
dataclasses==0.8
Flask==2.0.3
-e git+https://github.com/Stranger6667/Flask-Postmark.git@a717b345eb0a59aa10fcc5464dfd3b8d9e151824#egg=Flask_Postmark
idna==3.10
importlib-metadata==4.8.3
iniconfig==1.1.1
itsdangerous==2.0.1
Jinja2==3.0.3
MarkupSafe==2.0.1
mock==5.2.0
packaging==21.3
pluggy==1.0.0
postmarker==1.0
py==1.11.0
pyparsing==3.1.4
pytest==7.0.1
pytest-cov==4.0.0
requests==2.27.1
tomli==1.2.3
typing_extensions==4.1.1
urllib3==1.26.20
Werkzeug==2.0.3
zipp==3.6.0

name: Flask-Postmark
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2021.5.30=py36h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.3=he6710b0_2
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=21.2.2=py36h06a4308_0
  - python=3.6.13=h12debd9_1
  - readline=8.2=h5eee18b_0
  - setuptools=58.0.4=py36h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.37.1=pyhd3eb1b0_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - attrs==22.2.0
      - charset-normalizer==2.0.12
      - click==8.0.4
      - coverage==6.2
      - dataclasses==0.8
      - flask==2.0.3
      - idna==3.10
      - importlib-metadata==4.8.3
      - iniconfig==1.1.1
      - itsdangerous==2.0.1
      - jinja2==3.0.3
      - markupsafe==2.0.1
      - mock==5.2.0
      - packaging==21.3
      - pluggy==1.0.0
      - postmarker==1.0
      - py==1.11.0
      - pyparsing==3.1.4
      - pytest==7.0.1
      - pytest-cov==4.0.0
      - requests==2.27.1
      - tomli==1.2.3
      - typing-extensions==4.1.1
      - urllib3==1.26.20
      - werkzeug==2.0.3
      - zipp==3.6.0
prefix: /opt/conda/envs/Flask-Postmark

tests/test_postmark.py::TestPostmark::test_get_app
tests/test_postmark.py::TestPostmark::test_send
tests/test_postmark.py::TestPostmark::test_token tests/test_postmark.py::TestPostmark::test_send_batch tests/test_postmark.py::TestPostmark::test_is_same_client tests/test_postmark.py::TestPostmark::test_empty_app tests/test_postmark.py::TestPostmark::test_no_context
flask_postmark/core.py docs/changelog.rst
flask_postmark/core.py docs/changelog.rst