changeset 340:61f6a3d66b85

changes loop sample
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 17 Jan 2013 15:54:02 +0100
parents 639272ef7615
children b51c67f28b33
files Resources/Samples/Python/ChangesLoop.py Resources/Samples/Python/RestToolbox.py
diffstat 2 files changed, 109 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/Samples/Python/ChangesLoop.py	Thu Jan 17 15:54:02 2013 +0100
@@ -0,0 +1,42 @@
+#!/usr/bin/python
+
+import time
+import sys
+import RestToolbox
+
+if len(sys.argv) != 3:
+    print("""
+Sample script that continuously monitors the arrival of new DICOM
+images into Orthanc (through the Changes API).
+
+Usage: %s [hostname] [HTTP port]
+For instance: %s localhost 8042
+""" % (sys.argv[0], sys.argv[0]))
+    exit(-1)
+
+URL = 'http://%s:%d' % (sys.argv[1], int(sys.argv[2]))
+
+
+def NewInstanceReceived(path):
+    print 'New instance received: "%s"' % path
+
+
+URL = 'http://localhost:8042'
+current = 0
+while True:
+    r = RestToolbox.DoGet(URL + '/changes', {
+            'since' : current,
+            'limit' : 4 
+            })
+
+    for change in r['Changes']:
+        if change['ChangeType'] == 'NewInstance':
+            path = change['Path']
+            NewInstanceReceived(path)
+            RestToolbox.DoDelete(URL + path)
+
+    current = r['Last']
+
+    if r['Done']:
+        print "Everything has been processed: Waiting..."
+        time.sleep(1)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/Samples/Python/RestToolbox.py	Thu Jan 17 15:54:02 2013 +0100
@@ -0,0 +1,67 @@
+import hashlib
+import httplib2
+import json
+import os.path
+import Image
+import zipfile
+from urllib import urlencode
+
+def DoGet(uri, data = {}):
+    d = ''
+    if len(data.keys()) > 0:
+        d = '?' + urlencode(data)
+
+    h = httplib2.Http()
+    resp, content = h.request(uri + d, 'GET')
+    if not (resp.status in [ 200 ]):
+        raise Exception(resp.status)
+    else:
+        try:
+            return json.loads(content)
+        except:
+            return content
+
+
+def _DoPutOrPost(uri, method, data, contentType):
+    h = httplib2.Http()
+
+    if isinstance(data, str):
+        body = data
+        if len(contentType) != 0:
+            headers = { 'content-type' : contentType }
+    else:
+        body = json.dumps(data)
+        headers = { 'content-type' : 'application/json' }
+    
+    resp, content = h.request(
+        uri, method,
+        body = body,
+        headers = headers)
+
+    if not (resp.status in [ 200, 302 ]):
+        raise Exception(resp.status)
+    else:
+        try:
+            return json.loads(content)
+        except:
+            return content
+
+
+def DoDelete(uri):
+    h = httplib2.Http()
+    resp, content = h.request(uri, 'DELETE')
+    if not (resp.status in [ 200 ]):
+        raise Exception(resp.status)
+    else:
+        try:
+            return json.loads(content)
+        except:
+            return content
+
+
+def DoPut(uri, data = {}, contentType = ''):
+    return _DoPutOrPost(uri, 'PUT', data, contentType)
+
+
+def DoPost(uri, data = {}, contentType = ''):
+    return _DoPutOrPost(uri, 'POST', data, contentType)