Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
PEDASI
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
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
Research Software Group
PEDASI
Commits
dac77740
Commit
dac77740
authored
Feb 13, 2019
by
James Graham
Browse files
Options
Downloads
Patches
Plain Diff
Move determine_auth_method into data connector base class
parent
0445b33e
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
api/tests.py
+2
-2
2 additions, 2 deletions
api/tests.py
datasources/connectors/base.py
+34
-0
34 additions, 0 deletions
datasources/connectors/base.py
datasources/forms.py
+2
-1
2 additions, 1 deletion
datasources/forms.py
datasources/models.py
+1
-35
1 addition, 35 deletions
datasources/models.py
with
39 additions
and
38 deletions
api/tests.py
+
2
−
2
View file @
dac77740
...
...
@@ -6,7 +6,7 @@ from django.test import Client, TestCase
from
rest_framework.authtoken.models
import
Token
from
rest_framework.test
import
APIClient
from
datasources
import
models
from
datasources
import
connectors
,
models
class
RootApiTest
(
TestCase
):
...
...
@@ -499,7 +499,7 @@ class DataSourceApiHyperCatTest(TestCase):
url
=
cls
.
test_url
,
api_key
=
cls
.
api_key
,
plugin_name
=
cls
.
plugin_name
,
auth_method
=
models
.
DataSource
.
determine_auth_method
(
cls
.
test_url
,
cls
.
api_key
)
auth_method
=
connectors
.
BaseDataConnector
.
determine_auth_method
(
cls
.
test_url
,
cls
.
api_key
)
)
def
setUp
(
self
):
...
...
This diff is collapsed.
Click to expand it.
datasources/connectors/base.py
+
34
−
0
View file @
dac77740
...
...
@@ -100,6 +100,40 @@ class BaseDataConnector(metaclass=plugin.Plugin):
def
request_count
(
self
):
return
self
.
_request_counter
.
count
()
@staticmethod
def
determine_auth_method
(
url
:
str
,
api_key
:
str
)
->
AuthMethod
:
"""
Determine which authentication method to use to access the data source.
Test each known authentication method in turn until one succeeds.
:param url: URL to authenticate against
:param api_key: API key to use for authentication
:return: First successful authentication method
"""
# If not using an API key - can't require auth
if
not
api_key
:
return
AuthMethod
.
NONE
for
auth_method_id
,
auth_function
in
REQUEST_AUTH_FUNCTIONS
.
items
():
try
:
# Can we get a response using this auth method?
if
auth_function
is
None
:
response
=
requests
.
get
(
url
)
else
:
response
=
requests
.
get
(
url
,
auth
=
auth_function
(
api_key
,
''
))
response
.
raise_for_status
()
return
auth_method_id
except
requests
.
exceptions
.
HTTPError
:
pass
# None of the attempted authentication methods was successful
raise
requests
.
exceptions
.
ConnectionError
(
'
Could not authenticate against external API
'
)
def
get_metadata
(
self
,
params
:
typing
.
Optional
[
typing
.
Mapping
[
str
,
str
]]
=
None
):
"""
...
...
This diff is collapsed.
Click to expand it.
datasources/forms.py
+
2
−
1
View file @
dac77740
...
...
@@ -24,7 +24,8 @@ class DataSourceForm(forms.ModelForm):
cleaned_data
=
super
().
clean
()
try
:
cleaned_data
[
'
auth_method
'
]
=
models
.
DataSource
.
determine_auth_method
(
# TODO construct and actual data connector instance here
cleaned_data
[
'
auth_method
'
]
=
connectors
.
BaseDataConnector
.
determine_auth_method
(
cleaned_data
[
'
url
'
],
cleaned_data
[
'
api_key
'
]
)
...
...
This diff is collapsed.
Click to expand it.
datasources/models.py
+
1
−
35
View file @
dac77740
...
...
@@ -404,7 +404,7 @@ class DataSource(BaseAppDataModel):
# Is the authentication method set?
auth_method
=
AuthMethod
(
self
.
auth_method
)
if
not
auth_method
:
auth_method
=
self
.
determine_auth_method
(
self
.
url
,
self
.
api_key
)
auth_method
=
plugin
.
determine_auth_method
(
self
.
url
,
self
.
api_key
)
# Inject function to get authenticated request
auth_class
=
REQUEST_AUTH_FUNCTIONS
[
auth_method
]
...
...
@@ -475,40 +475,6 @@ class DataSource(BaseAppDataModel):
result
=
'
\n
'
.
join
(
lines
)
return
result
@staticmethod
def
determine_auth_method
(
url
:
str
,
api_key
:
str
)
->
AuthMethod
:
"""
Determine which authentication method to use to access the data source.
Test each known authentication method in turn until one succeeds.
:param url: URL to authenticate against
:param api_key: API key to use for authentication
:return: First successful authentication method
"""
# If not using an API key - can't require auth
if
not
api_key
:
return
AuthMethod
.
NONE
for
auth_method_id
,
auth_function
in
REQUEST_AUTH_FUNCTIONS
.
items
():
try
:
# Can we get a response using this auth method?
if
auth_function
is
None
:
response
=
requests
.
get
(
url
)
else
:
response
=
requests
.
get
(
url
,
auth
=
auth_function
(
api_key
,
''
))
response
.
raise_for_status
()
return
auth_method_id
except
requests
.
exceptions
.
HTTPError
:
pass
# None of the attempted authentication methods was successful
raise
requests
.
exceptions
.
ConnectionError
(
'
Could not authenticate against external API
'
)
def
get_absolute_url
(
self
):
return
reverse
(
'
datasources:datasource.detail
'
,
kwargs
=
{
'
pk
'
:
self
.
pk
})
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