Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
ab799ccc
Commit
ab799ccc
authored
Jun 29, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplify APIClient implementation
parent
664f8c63
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
17 additions
and
57 deletions
+17
-57
rest_framework/authentication.py
+3
-3
rest_framework/test.py
+14
-54
No files found.
rest_framework/authentication.py
View file @
ab799ccc
...
...
@@ -109,14 +109,14 @@ class SessionAuthentication(BaseAuthentication):
"""
# Get the underlying HttpRequest object
http_
request
=
request
.
_request
user
=
getattr
(
http_
request
,
'user'
,
None
)
request
=
request
.
_request
user
=
getattr
(
request
,
'user'
,
None
)
# Unauthenticated, CSRF validation not required
if
not
user
or
not
user
.
is_active
:
return
None
self
.
enforce_csrf
(
http_
request
)
self
.
enforce_csrf
(
request
)
# CSRF passed with authenticated user
return
(
user
,
None
)
...
...
rest_framework/test.py
View file @
ab799ccc
...
...
@@ -86,10 +86,6 @@ class ForceAuthClientHandler(ClientHandler):
self
.
_force_auth_token
=
None
super
(
ForceAuthClientHandler
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
def
force_authenticate
(
self
,
user
=
None
,
token
=
None
):
self
.
_force_auth_user
=
user
self
.
_force_auth_token
=
token
def
get_response
(
self
,
request
):
# This is the simplest place we can hook into to patch the
# request object.
...
...
@@ -108,56 +104,20 @@ class APIClient(APIRequestFactory, DjangoClient):
self
.
_credentials
=
{}
def
credentials
(
self
,
**
kwargs
):
"""
Sets headers that will be used on every outgoing request.
"""
self
.
_credentials
=
kwargs
def
authenticate
(
self
,
user
=
None
,
token
=
None
):
self
.
handler
.
force_authenticate
(
user
,
token
)
def
get
(
self
,
path
,
data
=
{},
follow
=
False
,
**
extra
):
extra
.
update
(
self
.
_credentials
)
response
=
super
(
APIClient
,
self
)
.
get
(
path
,
data
=
data
,
**
extra
)
if
follow
:
response
=
self
.
_handle_redirects
(
response
,
**
extra
)
return
response
def
head
(
self
,
path
,
data
=
{},
follow
=
False
,
**
extra
):
extra
.
update
(
self
.
_credentials
)
response
=
super
(
APIClient
,
self
)
.
head
(
path
,
data
=
data
,
**
extra
)
if
follow
:
response
=
self
.
_handle_redirects
(
response
,
**
extra
)
return
response
def
post
(
self
,
path
,
data
=
None
,
format
=
None
,
content_type
=
None
,
follow
=
False
,
**
extra
):
extra
.
update
(
self
.
_credentials
)
response
=
super
(
APIClient
,
self
)
.
post
(
path
,
data
=
data
,
format
=
format
,
content_type
=
content_type
,
**
extra
)
if
follow
:
response
=
self
.
_handle_redirects
(
response
,
**
extra
)
return
response
def
put
(
self
,
path
,
data
=
None
,
format
=
None
,
content_type
=
None
,
follow
=
False
,
**
extra
):
extra
.
update
(
self
.
_credentials
)
response
=
super
(
APIClient
,
self
)
.
post
(
path
,
data
=
data
,
format
=
format
,
content_type
=
content_type
,
**
extra
)
if
follow
:
response
=
self
.
_handle_redirects
(
response
,
**
extra
)
return
response
def
patch
(
self
,
path
,
data
=
None
,
format
=
None
,
content_type
=
None
,
follow
=
False
,
**
extra
):
extra
.
update
(
self
.
_credentials
)
response
=
super
(
APIClient
,
self
)
.
post
(
path
,
data
=
data
,
format
=
format
,
content_type
=
content_type
,
**
extra
)
if
follow
:
response
=
self
.
_handle_redirects
(
response
,
**
extra
)
return
response
def
delete
(
self
,
path
,
data
=
None
,
format
=
None
,
content_type
=
None
,
follow
=
False
,
**
extra
):
extra
.
update
(
self
.
_credentials
)
response
=
super
(
APIClient
,
self
)
.
post
(
path
,
data
=
data
,
format
=
format
,
content_type
=
content_type
,
**
extra
)
if
follow
:
response
=
self
.
_handle_redirects
(
response
,
**
extra
)
return
response
def
options
(
self
,
path
,
data
=
None
,
format
=
None
,
content_type
=
None
,
follow
=
False
,
**
extra
):
extra
.
update
(
self
.
_credentials
)
response
=
super
(
APIClient
,
self
)
.
post
(
path
,
data
=
data
,
format
=
format
,
content_type
=
content_type
,
**
extra
)
if
follow
:
response
=
self
.
_handle_redirects
(
response
,
**
extra
)
return
response
"""
Forcibly authenticates outgoing requests with the given
user and/or token.
"""
self
.
handler
.
_force_auth_user
=
user
self
.
handler
.
_force_auth_token
=
token
def
request
(
self
,
**
request
):
# Ensure that any credentials set get added to every request.
request
.
update
(
self
.
_credentials
)
return
super
(
APIClient
,
self
)
.
request
(
**
request
)
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