Skip to content

Commit

Permalink
Fix to new 'Bearer' in header
Browse files Browse the repository at this point in the history
  • Loading branch information
tylern4 committed Apr 8, 2022
1 parent 0188316 commit 03d64f8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
30 changes: 23 additions & 7 deletions python/SuperfacilityAPI/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

from typing import Dict, List
from authlib.integrations.requests_client import OAuth2Session, OAuthError
from authlib.integrations.requests_client import (
OAuth2Session,
OAuthError
)
from authlib.oauth2.rfc7523 import PrivateKeyJWT
import requests
import sys
Expand All @@ -11,9 +14,20 @@
from pathlib import Path

# Configurations in differnt files
from .error_warnings import permissions_warning, warning_fourOfour, no_client
from .error_warnings import (
permissions_warning,
warning_fourOfour,
no_client,
FourOfourException,
NoClientException,
)
from .api_version import API_VERSION
from .nersc_systems import NERSC_DEFAULT_COMPUTE, nersc_systems, nersc_compute, nersc_filesystems
from .nersc_systems import (
NERSC_DEFAULT_COMPUTE,
nersc_systems,
nersc_compute,
nersc_filesystems,
)


class SuperfacilityAPI:
Expand Down Expand Up @@ -79,7 +93,7 @@ def __renew_token(self):
# We've already got the session just fetch a new token
if self.session is not None:
self.access_token = self.session.fetch_token()['access_token']
self.headers['Authorization'] = self.access_token
self.headers['Authorization'] = f'Bearer {self.access_token}'

token_url = "https://oidc.nersc.gov/c2id/token"

Expand Down Expand Up @@ -110,7 +124,7 @@ def __renew_token(self):
print(f"Oauth error {e}\nMake sure your api key is still active in iris.nersc.gov", file=sys.stderr)
exit(2)
# Builds the header with the access token for requests
self.headers['Authorization'] = self.access_token
self.headers['Authorization'] = f'Bearer {self.access_token}'

def __generic_request(self, sub_url: str, header: Dict = None) -> Dict:
"""PRIVATE: Used to make a GET request to the api given a fully qualified sub url.
Expand Down Expand Up @@ -142,13 +156,15 @@ def __generic_request(self, sub_url: str, header: Dict = None) -> Dict:
if status == 404:
print(warning_fourOfour.format(
self.base_url+sub_url), file=sys.stderr)
return warning_fourOfour.format(self.base_url+sub_url)
raise FourOfourException(f"404 not found {self.base_url+sub_url}")

if self.access_token is None:
warning = no_client
print(warning, file=sys.stderr)
raise NoClientException("warning")
else:
warning = permissions_warning
print(warning, file=sys.stderr)

return warning

json_resp = resp.json()
Expand Down
5 changes: 3 additions & 2 deletions python/SuperfacilityAPI/bin/sfapi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3

import sys
from SuperfacilityAPI import SuperfacilityAPI

import click
Expand Down Expand Up @@ -100,7 +101,7 @@ def roles(ctx):
ret = [{k: oj[k] for k in ['repo_name', 'id', 'iris_role', 'description']} for oj in ret]
mypprint(ret)
except Exception as e:
mypprint(ret)
print(ret, file=sys.stderr)


@cli.command()
Expand All @@ -121,7 +122,7 @@ def projects(ctx):
df[['repo_name', 'id', 'description', 'Hours Left [%]', 'Project Hours Left [%]']])

except Exception as e:
mypprint(ret)
print(ret, file=sys.stderr)


@cli.command()
Expand Down
12 changes: 12 additions & 0 deletions python/SuperfacilityAPI/error_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@
#############################
"""


class FourOfourException(Exception):
pass


class NoClientException(Exception):
pass


class PermissionsException(Exception):
pass

0 comments on commit 03d64f8

Please sign in to comment.