Skip to content

Commit 4149cb9

Browse files
committed
Merge branch 'trs/authorization-command'
2 parents fa4d83c + 344f60d commit 4149cb9

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ development source code and as such may not be routinely kept up to date.
1313

1414
# __NEXT__
1515

16+
## Development
17+
18+
* A new `nextstrain authorization` command makes it easier to generate direct
19+
requests to nextstrain.org's web API using the same credentials as the CLI.
20+
([#229](https://github.com/nextstrain/cli/pull/229))
21+
1622

1723
# 5.0.0.dev1 (25 October 2022)
1824

doc/commands/authorization.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
========================
2+
nextstrain authorization
3+
========================
4+
5+
.. argparse::
6+
:module: nextstrain.cli
7+
:func: make_parser
8+
:prog: nextstrain
9+
:path: authorization

doc/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Table of Contents
5252
commands/login
5353
commands/logout
5454
commands/whoami
55+
commands/authorization
5556
commands/version
5657

5758
.. toctree::

nextstrain/cli/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from types import SimpleNamespace
1515

1616
from .argparse import HelpFormatter, register_commands, register_default_command
17-
from .command import build, view, deploy, remote, shell, update, setup, check_setup, login, logout, whoami, version, init_shell, debugger
17+
from .command import build, view, deploy, remote, shell, update, setup, check_setup, login, logout, whoami, version, init_shell, authorization, debugger
1818
from .debug import DEBUGGING
1919
from .errors import NextstrainCliError
2020
from .util import warn
@@ -78,6 +78,7 @@ def make_parser():
7878
whoami,
7979
version,
8080
init_shell,
81+
authorization,
8182
debugger,
8283
]
8384

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Produce an Authorization header appropriate for nextstrain.org's web API.
3+
4+
This is a development tool unnecessary for normal usage. It's useful for
5+
directly making API requests to nextstrain.org with ``curl`` or similar
6+
commands. For example::
7+
8+
curl -si https://nextstrain.org/whoami \\
9+
--header "Accept: application/json" \\
10+
--header @<(nextstrain authorization)
11+
12+
Exits with an error if no one is logged in.
13+
"""
14+
from ..authn import current_user
15+
from ..errors import UserError
16+
17+
18+
def register_parser(subparser):
19+
parser = subparser.add_parser("authorization", help = "Print an HTTP Authorization header")
20+
return parser
21+
22+
23+
def run(opts):
24+
user = current_user()
25+
26+
if not user:
27+
raise UserError("Not logged in.")
28+
29+
print(f"Authorization: {user.http_authorization}")

0 commit comments

Comments
 (0)