Skip to content

Add support for plain text config item #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 3, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ Flask config by the key ``TWITTER``, the configuration looks like::

oauth.init_app(app)

Or looks like that::

app.config['TWITTER_CONSUMER_KEY'] = 'a random string key'
app.config['TWITTER_CONSUMER_SECRET'] = 'a random string secret'

Twitter can get consumer key and secret from the Flask instance now.

You can put all the configuration in ``app.config`` if you like, which
Expand Down
16 changes: 12 additions & 4 deletions flask_oauthlib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,18 @@ def _get_property(self, key, default=False):
return default
return attr
app = self.oauth.app or current_app
config = app.config[self.app_key]
if default is not False:
return config.get(key, default)
return config[key]
if self.app_key in app.config:
# works with dict config
config = app.config[self.app_key]
if default is not False:
return config.get(key, default)
return config[key]
else:
# works with plain text config
config_key = "%s_%s" % (self.app_key, key.upper())
if default is not False:
return app.config.get(config_key, default)
return app.config[config_key]

def make_client(self, token=None):
# request_token_url is for oauth1
Expand Down
19 changes: 19 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,25 @@ def test_lazy_load(self):
assert twitter.authorize_url == 'auth url'
assert twitter.content_type is None

def test_lazy_load_with_plain_text_config(self):
oauth = OAuth()
twitter = oauth.remote_app('twitter', app_key='TWITTER')

app = Flask(__name__)
app.config['TWITTER_CONSUMER_KEY'] = 'twitter key'
app.config['TWITTER_CONSUMER_SECRET'] = 'twitter secret'
app.config['TWITTER_REQUEST_TOKEN_URL'] = 'request url'
app.config['TWITTER_ACCESS_TOKEN_URL'] = 'token url'
app.config['TWITTER_AUTHORIZE_URL'] = 'auth url'

oauth.init_app(app)

assert twitter.consumer_key == 'twitter key'
assert twitter.consumer_secret == 'twitter secret'
assert twitter.request_token_url == 'request url'
assert twitter.access_token_url == 'token url'
assert twitter.authorize_url == 'auth url'

@patch(http_urlopen)
def test_http_request(self, urlopen):
urlopen.return_value = Response(
Expand Down