Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
E
esap-userprofile-python-client
Manage
Activity
Members
Labels
Plan
Wiki
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ASTRON SDC
ESCAPE WP5
esap-userprofile-python-client
Commits
0d68fd3b
Commit
0d68fd3b
authored
3 years ago
by
Klaas Kliffen
Browse files
Options
Downloads
Patches
Plain Diff
Add JH API method for requesting tokens
parent
61bb8edf
No related branches found
No related tags found
1 merge request
!8
SDC-307 Cern dlaas notebook
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
shopping_client/shopping_client.py
+35
-7
35 additions, 7 deletions
shopping_client/shopping_client.py
with
35 additions
and
7 deletions
shopping_client/shopping_client.py
+
35
−
7
View file @
0d68fd3b
import
base64
import
getpass
import
json
import
logging
import
time
import
urllib.parse
from
os
import
getenv
...
...
@@ -10,10 +11,13 @@ from warnings import warn
import
pandas
as
pd
import
requests
logger
=
logging
.
getLogger
(
__name__
)
class
shopping_client
:
endpoint
=
"
esap-api/accounts/user-profiles/
"
audience
=
"
rucio
"
# Audience used by ESAP, might be configurable later
def
__init__
(
self
,
...
...
@@ -41,7 +45,10 @@ class shopping_client:
self
.
basket
=
None
def
get_basket
(
self
,
convert_to_pandas
:
bool
=
False
,
reload
:
bool
=
False
,
filter_archives
:
bool
=
False
self
,
convert_to_pandas
:
bool
=
False
,
reload
:
bool
=
False
,
filter_archives
:
bool
=
False
,
)
->
Union
[
list
,
pd
.
DataFrame
,
None
]:
"""
Retrieve the shopping basket for a user.
Prompts for access token if one was not supplied to constructor.
...
...
@@ -117,12 +124,14 @@ class shopping_client:
item_data
=
json
.
loads
(
item
[
"
item_data
"
])
for
connector
in
self
.
connectors
:
if
"
archive
"
in
item_data
and
item_data
[
"
archive
"
]
==
connector
.
archive
:
if
(
"
archive
"
in
item_data
and
item_data
[
"
archive
"
]
==
connector
.
archive
):
filtered_items
.
append
(
item
)
return
filtered_items
def
_basket_to_pandas
(
self
):
if
len
(
self
.
connectors
):
converted_basket
=
{
...
...
@@ -146,12 +155,31 @@ class shopping_client:
return
self
.
basket
def
_get_token
(
self
):
# Try to get token from Rucio OIDC file (when running in CERN DLaaS notebook)
# Generic JH token method using authstate
jh_api_uri
=
getenv
(
"
JUPYTERHUB_API_URL
"
)
jh_api_token
=
getenv
(
"
JUPYTERHUB_API_TOKEN
"
)
# Fallback to older rucio file
token_fn
=
getenv
(
"
RUCIO_OIDC_FILE_NAME
"
)
if
token_fn
is
not
None
:
try
:
if
all
((
jh_api_token
,
jh_api_uri
)):
res
=
requests
.
get
(
f
"
{
jh_api_uri
}
/user
"
,
headers
=
{
"
Authorization
"
:
f
"
token
{
jh_api_token
}
"
},
)
self
.
token
=
res
.
json
()[
"
auth_state
"
][
"
exchanged_tokens
"
][
self
.
audience
]
except
KeyError
:
logger
.
warning
(
"
JupyterHub without Authstate enabled
"
)
# Try to get token from Rucio OIDC file (when running in CERN DLaaS notebook)
if
self
.
token
is
None
and
token_fn
is
not
None
:
with
open
(
token_fn
)
as
token_file
:
self
.
token
=
token_file
.
readline
()
else
:
elif
self
.
token
is
None
:
self
.
token
=
getpass
.
getpass
(
"
Enter your ESAP access token:
"
)
if
self
.
token
is
None
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment