[Patchew-devel] [PATCH v4 1/4] Changes in Patchew cli to use rest-api
Fam Zheng
famz at redhat.com
Tue Jul 10 07:36:54 UTC 2018
On Sat, 07/07 00:02, Shubham Jain wrote:
> - 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.
Please split these to two patches.
> ---
> api/rest.py | 6 ++++++
> patchew-cli | 34 ++++++++++++++++++++++++++++++++++
> 2 files changed, 40 insertions(+)
>
> diff --git a/api/rest.py b/api/rest.py
> index 9ec0ae8..45dab6e 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'
>
> diff --git a/patchew-cli b/patchew-cli
> index 174d1e6..2069885 100755
> --- a/patchew-cli
> +++ b/patchew-cli
> @@ -92,6 +92,40 @@ 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"), method=request_method.upper())
> + if content_type is not None:
> + req.add_header('Content-Type', content_type)
> + 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)
>
> _______________________________________________
> Patchew-devel mailing list
> Patchew-devel at redhat.com
> https://www.redhat.com/mailman/listinfo/patchew-devel
More information about the Patchew-devel
mailing list