Created
February 22, 2013 19:46
-
-
Save xuru/5016047 to your computer and use it in GitHub Desktop.
suds starter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import suds | |
| WSDL_URL = "http://mycompany.qmetry.com/myid/WEB-INF/ws/service.php?wsdl" | |
| class QMetry(object): | |
| def __init__(self, timeout=90): | |
| client = suds.client.Client(WSDL_URL) | |
| self.service = client.service | |
| self.client = client | |
| self.login_token = None | |
| def login(self, username, password): | |
| self.login_token = self.service.login(username, password) | |
| def logout(self): | |
| self.service.logout(self.login_token) | |
| self.login_token = None | |
| def version(self): | |
| return self.service.QMetryVersion() | |
| # call all other methods using self.service.* | |
| # proxying | |
| def __getattribute__(self, attr): | |
| service = super(QMetry, self).__getattribute__("service") | |
| try: | |
| # see if we have overwritten the method first (see login) | |
| return super(QMetry, self).__getattribute__(attr) | |
| except (AttributeError, MethodNotFound): | |
| pass | |
| try: | |
| # try the service | |
| _attr = getattr(service, attr) | |
| if isinstance(_attr, suds.client.Method): | |
| logger.info("Calling %s" % _attr.method.name) | |
| if self.login_token: | |
| def func(*args, **kwargs): | |
| return _attr(self.login_token, *args, **kwargs) | |
| return func | |
| else: | |
| if _attr.method.name == 'QMetryVersion': | |
| return _attr | |
| else: | |
| raise QMetryError("You must log in before calling %s" % _attr.method.name) | |
| else: | |
| return _attr | |
| except (AttributeError, MethodNotFound): | |
| # see if it's in the client object | |
| client = super(QMetry, self).__getattribute__("client") | |
| return getattr(client, attr) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment