Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
RWTH.nb
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
RWTHjupyter
RWTH.nb
Commits
c94e96ab
Commit
c94e96ab
authored
4 years ago
by
Steffen Vogel
Browse files
Options
Downloads
Patches
Plain Diff
add new helpers for creating sharable links
parent
29840f3a
No related branches found
No related tags found
No related merge requests found
Pipeline
#386169
failed
4 years ago
Stage: test
Stage: deploy
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
rwth_nb/misc/share.py
+66
-0
66 additions, 0 deletions
rwth_nb/misc/share.py
tests/share.py
+20
-0
20 additions, 0 deletions
tests/share.py
with
86 additions
and
0 deletions
rwth_nb/misc/share.py
0 → 100644
+
66
−
0
View file @
c94e96ab
import
os
import
requests
import
urllib
base_url
=
'
https://jupyter.rwth-aachen.de
'
hub_api_url
=
f
'
{
base_url
}
/hub/api
'
shareable_prefix
=
'
shareable
'
def
get_hub_token
(
user
,
note
=
None
,
expires_in
=
None
):
token
=
os
.
environ
[
'
JUPYTERHUB_API_TOKEN
'
]
body
=
{}
if
expires_in
:
body
[
'
expires_in
'
]
=
expires_in
if
note
:
body
[
'
note
'
]
=
note
r
=
requests
.
post
(
f
'
{
hub_api_url
}
/users/
{
user
}
/tokens?
'
,
headers
=
{
'
Authorization
'
:
'
Bearer
'
+
token
},
json
=
body
)
r
.
raise_for_status
()
return
r
.
json
()[
'
token
'
]
def
get_shareable_url
(
user
=
None
,
path
=
None
,
expires_in
=
24
*
60
*
60
,
note
=
''
):
if
user
is
None
:
user
=
os
.
environ
[
'
JUPYTERHUB_USER
'
]
token
=
get_hub_token
(
user
,
note
=
f
'
{
shareable_prefix
}
:
{
path
}
:
{
note
}
'
,
expires_in
=
expires_in
)
path
=
urllib
.
parse
.
quote
(
path
)
return
f
'
{
base_url
}
/user/
{
user
}
/lab/tree/
{
path
}
?token=
{
token
}
'
def
get_shareable_tokens
(
user
=
None
):
if
user
is
None
:
user
=
os
.
environ
[
'
JUPYTERHUB_USER
'
]
token
=
os
.
environ
[
'
JUPYTERHUB_API_TOKEN
'
]
r
=
requests
.
get
(
f
'
{
hub_api_url
}
/users/
{
user
}
/tokens?
'
,
headers
=
{
'
Authorization
'
:
'
Bearer
'
+
token
})
r
.
raise_for_status
()
tokens
=
[]
for
token
in
r
.
json
()[
'
api_tokens
'
]:
if
token
[
'
note
'
].
startswith
(
shareable_prefix
+
'
:
'
):
tokens
.
append
(
token
)
return
tokens
def
revoke_shareable_token
(
token_id
,
user
=
None
):
if
user
is
None
:
user
=
os
.
environ
[
'
JUPYTERHUB_USER
'
]
token
=
os
.
environ
[
'
JUPYTERHUB_API_TOKEN
'
]
r
=
requests
.
delete
(
f
'
{
hub_api_url
}
/users/
{
user
}
/tokens/
{
token_id
}
'
,
headers
=
{
'
Authorization
'
:
'
Bearer
'
+
token
})
r
.
raise_for_status
()
This diff is collapsed.
Click to expand it.
tests/share.py
0 → 100644
+
20
−
0
View file @
c94e96ab
import
rwth_nb.misc.notebook
as
nb
import
rwth_nb.misc.share
as
share
from
pprint
import
pprint
url
=
share
.
get_shareable_url
(
path
=
'
gdet3/GDET3 Äquivalenter Tiefpass.ipynb
'
,
note
=
'
access for my colleage at IKS
'
,
expires_in
=
24
*
60
*
60
)
print
(
url
)
# List all shareable tokens
tokens
=
share
.
get_shareable_tokens
()
pprint
(
tokens
)
# Revoke the new token
share
.
revoke_shareable_token
(
tokens
[
0
][
'
id
'
])
# List all tokens again
tokens
=
share
.
get_shareable_tokens
()
pprint
(
tokens
)
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