diff options
| author | Bryan Newbold <bnewbold@robocracy.org> | 2019-01-08 16:28:27 -0800 | 
|---|---|---|
| committer | Bryan Newbold <bnewbold@robocracy.org> | 2019-01-08 16:28:27 -0800 | 
| commit | 16f2e78298dbd2231f5f337ea17c89a6a131a052 (patch) | |
| tree | 6e72581e625e73c97cbab72d0f9c35665c99e5d7 /python/fatcat_tools/api_auth.py | |
| parent | eb40a5f274f3608db34309cfd16739a7642ef5e7 (diff) | |
| parent | ffb721f90c5d97ee80885209bf45feb85ca9625c (diff) | |
| download | fatcat-16f2e78298dbd2231f5f337ea17c89a6a131a052.tar.gz fatcat-16f2e78298dbd2231f5f337ea17c89a6a131a052.zip  | |
Merge branch 'bnewbold-crude-auth'
Fixed a conflict in:
  python/fatcat_export.py
Diffstat (limited to 'python/fatcat_tools/api_auth.py')
| -rw-r--r-- | python/fatcat_tools/api_auth.py | 40 | 
1 files changed, 40 insertions, 0 deletions
diff --git a/python/fatcat_tools/api_auth.py b/python/fatcat_tools/api_auth.py new file mode 100644 index 00000000..c49051f6 --- /dev/null +++ b/python/fatcat_tools/api_auth.py @@ -0,0 +1,40 @@ + +import os, sys +import fatcat_client +from fatcat_client.rest import ApiException + + +def public_api(host_uri): +    """ +    Note: unlike the authenticated variant, this helper might get called even +    if the API isn't going to be used, so it's important that it doesn't try to +    actually connect to the API host or something. +    """ +    conf = fatcat_client.Configuration() +    conf.host = host_uri +    return fatcat_client.DefaultApi(fatcat_client.ApiClient(conf)) + +def authenticated_api(host_uri, token=None): +    """ +    Note: if this helper is called, it's implied that an actual API connection +    is needed, so it does try to connect and verify credentials. +    """ + +    conf = fatcat_client.Configuration() +    conf.host = host_uri +    if not token: +        token = os.environ['FATCAT_API_AUTH_TOKEN'] +    if not token: +        sys.stderr.write( +            'This client requires a fatcat API token (eg, in env var FATCAT_API_AUTH_TOKEN)\n') +        sys.exit(-1) + +    conf.api_key["Authorization"] = token +    conf.api_key_prefix["Authorization"] = "Bearer" +    api = fatcat_client.DefaultApi(fatcat_client.ApiClient(conf)) + +    # verify up front that auth is working +    api.auth_check() + +    return api +  | 
