Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.

Commit 3406679

Browse files
authored
Merge pull request #25 from core-api/client-auth
Use `Client(auth=...)` instead of deprecated `Client(credentials=...)`
2 parents 26cda7b + 6bab024 commit 3406679

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

coreapi_cli/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.0.6"
1+
__version__ = "1.0.7"

coreapi_cli/auth.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from coreapi_cli.compat import urlparse
2+
from requests.auth import AuthBase
3+
4+
5+
class DomainCredentials(AuthBase):
6+
allow_cookies = False
7+
credentials = None
8+
9+
def __init__(self, credentials=None):
10+
self.credentials = credentials
11+
12+
def __call__(self, request):
13+
if not self.credentials:
14+
return request
15+
16+
# Include any authorization credentials relevant to this domain.
17+
url_components = urlparse.urlparse(request.url)
18+
host = url_components.hostname
19+
if host in self.credentials:
20+
request.headers['Authorization'] = self.credentials[host]
21+
return request

coreapi_cli/compat.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import json
2+
3+
try:
4+
# Python 3
5+
JSONDecodeError = json.decoder.JSONDecodeError
6+
except AttributeError:
7+
# Python 2
8+
JSONDecodeError = ValueError
9+
10+
11+
try:
12+
# Python 2
13+
import urlparse # noqa
14+
except ImportError:
15+
# Python 3
16+
import urllib.parse as urlparse # noqa

coreapi_cli/main.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from coreapi.compat import b64encode, force_bytes
22
from coreapi_cli import __version__ as client_version
33
from coreapi_cli import codec_plugins
4+
from coreapi_cli.auth import DomainCredentials
5+
from coreapi_cli.compat import JSONDecodeError
46
from coreapi_cli.display import display
57
from coreapi_cli.debug import DebugSession
68
from coreapi_cli.history import History, dump_history, load_history
@@ -46,7 +48,7 @@ def coerce_key_types(doc, keys):
4648
if isinstance(active, coreapi.Array):
4749
try:
4850
key = int(key)
49-
except:
51+
except ValueError:
5052
pass
5153

5254
# Descend through the document, so we can correctly identify
@@ -78,7 +80,7 @@ def get_client(decoders=None, debug=False):
7880
decoders = list(codec_plugins.decoders.values())
7981

8082
http_transport = coreapi.transports.HTTPTransport(
81-
credentials=credentials, headers=headers, session=session
83+
auth=DomainCredentials(credentials), headers=headers, session=session
8284
)
8385
return coreapi.Client(decoders=decoders, transports=[http_transport])
8486

@@ -268,7 +270,7 @@ def parse_params(ctx, param, tokens):
268270

269271
try:
270272
pair = (field, json.loads(value))
271-
except:
273+
except JSONDecodeError:
272274
if value.startswith('{') or value.startswith('['):
273275
# Guard against malformed composite objects being treated as strings.
274276
raise click.BadParameter('Unclear if parameter "%s" should be interperted as a string or data. Use --data or --string instead.' % field)
@@ -288,7 +290,7 @@ def parse_json(ctx, param, tokens):
288290

289291
try:
290292
pair = (field, json.loads(value))
291-
except:
293+
except JSONDecodeError:
292294
raise click.BadParameter('Could not parse value for data argument "%s"' % field)
293295
ret.append(pair)
294296

0 commit comments

Comments
 (0)