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'
import requests
import pickle
import json
def http_resp(fn):
def wrapped(*args, **kwargs):
......@@ -18,9 +19,15 @@ class Nca3Client(object):
self.base_url = url
self.s = requests.Session()
self.s.auth = (http_basic_user, http_basic_pass)
self.s.headers = {'content-type': 'application/json'}
self.drupal_user = username
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):
url = '{b}/user'.format(b=self.base_url)
......@@ -34,13 +41,26 @@ class Nca3Client(object):
pickle.dump(self.s.cookies, open(self.cookie_jar, 'wb'))
return resp
def get_all_captions(self):
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
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})
Supports Markdown
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