[Patchew-devel] [PATCH 1/2] Changes in Patchew cli to use rest-api
Shubham Jain
shubhamjain7495 at gmail.com
Thu Jun 28 19:07:05 UTC 2018
- Disabled CSRF authentication until OAuth authentication, since DRF leads to csrf error while handling sessionauthentication.
- Added general rest_api function to make request to apis and get response.
---
api/rest.py | 11 +++++++++--
patchew-cli | 35 +++++++++++++++++++++++++++++++++++
2 files changed, 44 insertions(+), 2 deletions(-)
diff --git a/api/rest.py b/api/rest.py
index 084dbbc..8961811 100644
--- a/api/rest.py
+++ b/api/rest.py
@@ -25,6 +25,12 @@ from rest_framework.response import Response
import rest_framework
from mbox import addr_db_to_rest, MboxMessage
from rest_framework.parsers import JSONParser, BaseParser
+from rest_framework.authentication import SessionAuthentication
+
+class CsrfExemptSessionAuthentication(SessionAuthentication):
+
+ def enforce_csrf(self, request):
+ return # To not perform the csrf check previously happening
SEARCH_PARAM = 'q'
@@ -140,7 +146,8 @@ class ProjectsViewSet(viewsets.ModelViewSet):
queryset = Project.objects.all().order_by('id')
serializer_class = ProjectSerializer
permission_classes = (PatchewPermission,)
-
+ authentication_classes = (CsrfExemptSessionAuthentication, )
+
@action(methods=['post'], detail=True, permission_classes=[ImportPermission])
def update_project_head(self, request, pk=None):
"""
@@ -414,7 +421,7 @@ class MessagesViewSet(BaseMessageViewSet):
serializer_class = MessageSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
parser_classes = (JSONParser, MessagePlainTextParser, )
-
+ authentication_classes = (CsrfExemptSessionAuthentication,)
def create(self, request, *args, **kwargs):
m = MboxMessage(request.data['mbox'])
projects = [p for p in Project.objects.all() if p.recognizes(m)]
diff --git a/patchew-cli b/patchew-cli
index 174d1e6..c53526b 100755
--- a/patchew-cli
+++ b/patchew-cli
@@ -92,6 +92,41 @@ class SubCommand(object):
r = None
return r
+ def rest_api_do(self, url_cmd, request_method='get', content_type=None, data=None):
+ logging.debug("API call '%s':" % url_cmd)
+ logging.debug("data:\n%s" % data)
+ cookie = http.cookiejar.MozillaCookieJar(COOKIE_FILENAME)
+ try:
+ cookie.load()
+ except IOError:
+ pass
+ except http.cookiejar.LoadError:
+ print("Error while loading cookie", COOKIE_FILENAME)
+ pass
+ handler = urllib.request.HTTPCookieProcessor(cookie)
+ opener = urllib.request.build_opener(handler)
+ if url_cmd.startswith("http"):
+ url = url_cmd
+ else:
+ url = self.base_url + '/api/v1/' + url_cmd + '/'
+ if data is None:
+ post_data = ""
+ else:
+ post_data = data
+ req = urllib.request.Request(url, data=bytes(post_data, encoding="utf-8"))
+ if content_type is not None:
+ req.add_header('Content-Type', content_type)
+ req.get_method = lambda: request_method.upper()
+ resp = opener.open(req)
+ cookie.save(ignore_discard=True, ignore_expires=True)
+ respdata = resp.read()
+ logging.debug("Server response:\n%s" % (respdata or "<empty>"))
+ if respdata:
+ r = json.loads(respdata.decode("utf-8"))
+ else:
+ r = None
+ return r
+
def do(self, args, argv):
"""Do command"""
print("Not implemented")
--
2.15.1 (Apple Git-101)
More information about the Patchew-devel
mailing list