changeset 346:107da6a8c630

credentials in python rest toolbox
author jodogne
date Wed, 23 Jan 2013 13:29:47 +0100
parents 795ce0fa8e8a
children a9752f88400c
files Resources/Samples/Python/RestToolbox.py
diffstat 1 files changed, 15 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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: