Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
beat
beat.cmdline
Commits
57b9b5f3
Commit
57b9b5f3
authored
Oct 23, 2019
by
Samuel GAIST
Browse files
[webapi] Remove use of django rest_framework client use for testing
parent
6103410d
Changes
1
Hide whitespace changes
Inline
Side-by-side
beat/cmdline/common.py
View file @
57b9b5f3
...
...
@@ -649,12 +649,12 @@ def edit_local_file(prefix, editor, asset_type, name):
return
0
def
make_webapi
(
c
):
def
make_webapi
(
c
onfig
):
"""Instantiates an usable web-api proxy using the command-line configuration
Parameters:
c (object): The command-line configuration object, from which this function
c
onfig
(object): The command-line configuration object, from which this function
will extract the ``platform``, ``user`` and ``token`` parameters.
...
...
@@ -664,63 +664,9 @@ def make_webapi(c):
"""
if
c
.
platform
.
startswith
(
"django://"
):
from
.webapi
import
WebAPI
# returns a Django RestFramework API client that looks like our WebAPI
from
rest_framework.test
import
APIClient
from
rest_framework.authtoken.models
import
Token
class
APIClientContext
(
APIClient
):
def
__init__
(
self
,
platform
,
user
,
token
,
*
args
,
**
kwargs
):
self
.
platform
=
platform
super
(
APIClientContext
,
self
).
__init__
(
*
args
,
**
kwargs
)
self
.
user
=
user
self
.
token
=
token
# sets-up authorization token on all requests, if user is set
if
self
.
user
is
not
None
:
token
=
Token
.
objects
.
get
(
user__username
=
user
)
self
.
credentials
(
HTTP_AUTHORIZATION
=
"Token "
+
token
.
key
)
def
is_anonymous
(
self
):
"""Tells if the user has not set credentials for the platform"""
return
self
.
token
is
None
def
__enter__
(
self
):
return
self
def
__exit__
(
self
,
*
exc
):
pass
def
_message
(
self
,
asset_type
,
url
,
data
=
None
):
if
data
:
data
=
simplejson
.
dumps
(
data
)
retval
=
getattr
(
super
(
APIClientContext
,
self
),
asset_type
)(
url
,
data
,
content_type
=
"application/json"
)
return
(
retval
.
status_code
,
retval
.
content
)
def
get
(
self
,
url
,
data
=
None
):
return
self
.
_message
(
"get"
,
url
,
data
)
def
put
(
self
,
url
,
data
=
None
):
return
self
.
_message
(
"put"
,
url
,
data
)
def
post
(
self
,
url
,
data
=
None
):
return
self
.
_message
(
"post"
,
url
,
data
)
def
delete
(
self
,
url
,
data
=
None
):
return
self
.
_message
(
"delete"
,
url
,
data
)
return
APIClientContext
(
c
.
platform
,
c
.
user
,
c
.
token
)
else
:
# sets up our own client
from
.webapi
import
WebAPI
return
WebAPI
(
c
.
platform
,
c
.
user
,
c
.
token
)
return
WebAPI
(
config
.
platform
,
config
.
user
,
config
.
token
)
def
check_one
(
prefix
,
asset_type
,
name
):
...
...
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment