Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
itminedu
synnefo
Commits
47266d5a
Commit
47266d5a
authored
Feb 27, 2013
by
Ilias Tsitsimpis
Committed by
Christos Stavrakakis
Mar 12, 2013
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement `authenticate' function
parent
eaa2650e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
24 deletions
+48
-24
snf-astakos-client/astakosclient/__init__.py
snf-astakos-client/astakosclient/__init__.py
+48
-24
No files found.
snf-astakos-client/astakosclient/__init__.py
View file @
47266d5a
...
...
@@ -42,6 +42,54 @@ import objpool.http
logger
=
logging
.
getLogger
(
__name__
)
# --------------------------------------------------------------------
# Astakos client API
# A simple retry decorator
def
retry
(
howmany
):
def
decorator
(
func
):
def
f
(
*
args
,
**
kwargs
):
attemps
=
0
while
True
:
try
:
return
func
(
*
args
,
**
kwargs
)
except
Exception
as
e
:
is_last_attempt
=
attemps
==
howmany
-
1
if
is_last_attempt
:
raise
e
if
e
.
args
:
status
=
e
[
0
]
# In case of Unauthorized response
# or Not Found return immediately
if
status
==
401
or
status
==
404
:
raise
e
attemps
+=
1
return
f
return
decorator
@
retry
(
3
)
def
authenticate
(
token
,
astakos_url
,
usage
=
False
,
use_pool
=
False
):
"""Check if user is an authenticated Astakos user
Keyword arguments:
token -- user's token (string)
astakos_url -- i.e https://accounts.example.com (string)
usage -- return usage information for user (boolean)
use_pool -- use objpool for http requests (boolean)
In case of success return user informations (json parsed format).
Otherwise raise an Exception.
"""
# XXX: use something like url.join
astakos_url
=
astakos_url
.
rstrip
(
"/"
)
authentication_url
=
astakos_url
+
"/im/authenticate?"
if
usage
:
authentication_url
+=
"usage=1,"
return
_callAstakos
(
token
,
authentication_url
,
use_pool
=
use_pool
)
# --------------------------------------------------------------------
# Private functions
def
_scheme_to_class
(
scheme
):
...
...
@@ -111,27 +159,3 @@ def _callAstakos(token, url, headers={}, body=None,
if
status
<
200
or
status
>=
300
:
raise
Exception
(
status
,
data
)
return
simplejson
.
loads
(
unicode
(
data
))
# ----------------------------
# A simple retry decorator
def
retry
(
howmany
):
def
decorator
(
func
):
def
f
(
*
args
,
**
kwargs
):
attemps
=
0
while
True
:
try
:
return
func
(
*
args
,
**
kwargs
)
except
Exception
as
e
:
is_last_attempt
=
attemps
==
howmany
-
1
if
is_last_attempt
:
raise
e
if
e
.
args
:
status
=
e
[
0
]
# In case of Unauthorized response
# or Not Found return immediately
if
status
==
401
or
status
==
404
:
raise
e
attemps
+=
1
return
f
return
decorator
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment