Repository: Stranger6667/Flask-Postmark

Issue:
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
~~~
    

Discussion:
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).


Patch:
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):


Test patch:
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