Commit 1bde1223 authored by abuddenberg's avatar abuddenberg
Browse files

Added support for persisting session cookies. Added support for Drupal's silly...

Added support for persisting session cookies. Added support for Drupal's silly little CSRF token to enable write operating through the Services module
parent bdb39175
__author__ = 'abuddenberg' __author__ = 'abuddenberg'
import requests import requests
import pickle
import json
def http_resp(fn): def http_resp(fn):
def wrapped(*args, **kwargs): def wrapped(*args, **kwargs):
...@@ -18,9 +19,15 @@ class Nca3Client(object): ...@@ -18,9 +19,15 @@ class Nca3Client(object):
self.base_url = url self.base_url = url
self.s = requests.Session() self.s = requests.Session()
self.s.auth = (http_basic_user, http_basic_pass) self.s.auth = (http_basic_user, http_basic_pass)
self.s.headers = {'content-type': 'application/json'}
self.drupal_user = username self.drupal_user = username
self.drupal_pass = password self.drupal_pass = password
self.cookie_jar = '/tmp/cookies'
self.s.cookies = pickle.load(open(self.cookie_jar, 'r'))
except Exception, e:
def do_login(self): def do_login(self):
url = '{b}/user'.format(b=self.base_url) url = '{b}/user'.format(b=self.base_url)
...@@ -34,13 +41,26 @@ class Nca3Client(object): ...@@ -34,13 +41,26 @@ class Nca3Client(object):
}, },
allow_redirects=False allow_redirects=False
) )
pickle.dump(self.s.cookies, open(self.cookie_jar, 'wb'))
return resp return resp
@http_resp @http_resp
def get_all_captions(self): def get_all_captions(self):
url = '{b}/gcis/figure-table-captions'.format(b=self.base_url) url = '{b}/gcis/figure-table-captions'.format(b=self.base_url)
resp = self.s.get(url, verify=False) resp = self.s.get(url, verify=False, cookies=self.s.cookies)
return resp return resp
def get_figure(self, nid):
url = '{b}/gcis/node/{nid}'.format(b=self.base_url, nid=nid)
return self.s.get(url, verify=False, cookies=self.s.cookies).json()
def update_figure(self, nid, figure_frag):
url = '{b}/gcis/node/{nid}'.format(b=self.base_url, nid=nid)
token_url = '{b}/services/session/token'.format(b=self.base_url)
token = self.s.get(token_url, verify=False, cookies=self.s.cookies).text
return self.s.put(url, data=json.dumps(figure_frag), verify=False, cookies=self.s.cookies, headers={'X-CSRF-Token': token})
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment