# HG changeset patch # User jodogne # Date 1358944187 -3600 # Node ID 107da6a8c63002cfccf125bd963ca457160da4b4 # Parent 795ce0fa8e8ad7daca58be94d561a353420f405e credentials in python rest toolbox diff -r 795ce0fa8e8a -r 107da6a8c630 Resources/Samples/Python/RestToolbox.py --- a/Resources/Samples/Python/RestToolbox.py Wed Jan 23 10:21:59 2013 +0100 +++ b/Resources/Samples/Python/RestToolbox.py Wed Jan 23 13:29:47 2013 +0100 @@ -2,12 +2,24 @@ import json from urllib import urlencode +_credentials = None + +def SetCredentials(username, password): + global _credentials + _credentials = (username, password) + +def _SetupCredentials(h): + global _credentials + if _credentials != None: + h.add_credentials(_credentials[0], _credentials[1]) + def DoGet(uri, data = {}): d = '' if len(data.keys()) > 0: d = '?' + urlencode(data) h = httplib2.Http() + _SetupCredentials(h) resp, content = h.request(uri + d, 'GET') if not (resp.status in [ 200 ]): raise Exception(resp.status) @@ -20,6 +32,7 @@ def _DoPutOrPost(uri, method, data, contentType): h = httplib2.Http() + _SetupCredentials(h) if isinstance(data, str): body = data @@ -45,7 +58,9 @@ def DoDelete(uri): h = httplib2.Http() + _SetupCredentials(h) resp, content = h.request(uri, 'DELETE') + if not (resp.status in [ 200 ]): raise Exception(resp.status) else: