#!/usr/bin/python
# -- coding: utf-8 --

#
# Mathias Chouet - INRA UMR EGFV + UMR MISTEA - 13 janvier 2012
#
# Python SOAP client example for VitPhe webservice
# Requires suds: https://fedorahosted.org/suds/
#

__author__="mathias"
__date__ ="$13 janvier 2012 12:05:10$"

from suds.client import Client
from suds.wsse import *
import unicodedata

# found on: http://stackoverflow.com/questions/517923/what-is-the-best-way-to-remove-accents-in-a-python-unicode-string
def remove_accents(input_str):
    nkfd_form = unicodedata.normalize('NFKD', unicode(input_str))
    return u"".join([c for c in nkfd_form if not unicodedata.combining(c)])

# Transforms a "map" object into a Python dictionary
def mapToDict(map):
    dic = {}
    if (hasattr(map, "item")): # "map" or "mapitem" object
        for mapItem in map.item:
            dic[mapItem.key] = mapToDict(mapItem.value)
        return dic
    else: # text value
        if (map != None):
            map = remove_accents(map)
        return map


# define your parameters here
proxy = False
proxyurl="adress:port"
display_faults=True
mylogin="myuse.rname"
mypassword="mypasswd"
url = "http://bioweb.supagro.inra.fr/vitphe/public/ws/?wsdl"


# Shall we use a proxy?
if (proxy):
    # proxy options
    proxy_info = dict(http=proxyurl, https=proxyurl)
	# creating base client with proxy
    client = Client(url, proxy=proxy_info, cache=None, faults=display_faults)
else:
    # creating base client
    client = Client(url, cache=None, faults=display_faults)

# WSSE authentication
security = Security()
token = UsernameToken(mylogin, mypassword)
#token.setnonce('MyNonceString...')
#token.setcreated(datetime.now())
security.tokens.append(token)
client.set_options(wsse=security)


# List of available methods and types
print client

# Test method
hello = client.service.getHello("Python client")
print hello

# List of experiments
expes = client.service.getExperiments()
print expes
#print mapToDict(expes)

# Measures for experiment 2011_LEPSE_04
#mesures = client.service.getOfflineMeasuresByExperiment("2011-LEPSE-04")
#print mesures
#print mapToDict(mesures)

# Add online measure
success = client.service.addOnlineMeasure('2012-04-27 15:48:25', '45', 'air-temperature', 'PC20-6', 'cm-02', '2011-LEPSE-04', '<data>myAnnotation</data>')
print success

#print "Completed successfully"
