Fetch Leap API token.
(*, config_file, profile, token_type, json_output, raw_output, output)
| 1046 | @raw_output |
| 1047 | @standardized_output |
| 1048 | def get(*, config_file, profile, token_type, json_output, raw_output, output): |
| 1049 | """Fetch Leap API token.""" |
| 1050 | |
| 1051 | config = validate_config_v1(load_config(config_file=config_file, profile=profile)) |
| 1052 | |
| 1053 | with LeapAuthFlow.from_config_model(config) as flow: |
| 1054 | token_key = token_type.replace('-', '_') |
| 1055 | if not flow.token or not token_key in flow.token: |
| 1056 | raise CLIError('Token not found. Please run "dwave auth login".', code=100) |
| 1057 | |
| 1058 | token_pretty = token_type.replace('-', ' ').capitalize() |
| 1059 | token_val = flow.token[token_key] |
| 1060 | output(f"{token_pretty}: {{%s}}" % token_key, **{token_key: token_val}) |
| 1061 | output(raw=token_val) |
| 1062 | |
| 1063 | if token_type == 'access-token': |
| 1064 | expires_at = flow.token.get('expires_at') |
| 1065 | if not expires_at: |
| 1066 | return |
| 1067 | expires_at = int(expires_at) |
| 1068 | expired = expires_at < epochnow() |
| 1069 | |
| 1070 | output("Expires at: {expires_at_iso}Z (timestamp={expires_at}) ({is_valid})", |
| 1071 | expires_at_iso=datetime.utcfromtimestamp(expires_at).isoformat(), |
| 1072 | expires_at=expires_at, |
| 1073 | is_valid="expired" if expired else "valid") |
| 1074 | |
| 1075 | if expired: |
| 1076 | output('\nTo refresh the token, please run "dwave auth refresh".') |
| 1077 | |
| 1078 | |
| 1079 | @auth.command() |
nothing calls this directly
no test coverage detected