0

I've been trying to use the query() method of the AutotaskAPI which uses QueryXML. In order to do this, I understand I have to use the ATWSResponse type in order to receive results. This is my code:

class ConnectATWS():
    def __init__(self):
        #Connect to server with the credentials
        app_config = Init()
        self.username = app_config.data["Username"]
        self.password = app_config.data["Password"]
        self.login_id = app_config.data["LoginID"]
        self.url = app_config.data["AutotaskUpdateTicketEstimatedHours_net_autotask_webservices5_ATWS"]
        strCurrentID = "0"
        strCriteria = "<condition><field>Status<expression op=""NotEqual"">5</expression></field></condition>"
        strQuery = "<queryxml><entity>Ticket</entity><query>" + \
                        "<condition><field>id<expression op=""greaterthan"">" + strCurrentID + "</expression></field></condition>" + strCriteria + \
                        "<condition><field>EstimatedHours<expression op=""isnull""></expression></field></condition>" + \
                        "</query></queryxml>"

        client = Client(self.url + "?WSDL", username=self.login_id, password=self.password)
        response = client.service.query(strQuery)

Trying this returns the following error:

No handlers could be found for logger "suds.client"
Traceback (most recent call last):
  File "/Users/AAAA/Documents/Aptana/AutotaskUpdateTicketEstimatedHours/Main.py", line 46, in <module>
    handler = ConnectATWS()
  File "/Users/AAAA/Documents/Aptana/AutotaskUpdateTicketEstimatedHours/Main.py", line 40, in __init__
    response = client.service.query(strQuery)
  File "/Library/Python/2.7/site-packages/suds/client.py", line 542, in __call__
    return client.invoke(args, kwargs)
  File "/Library/Python/2.7/site-packages/suds/client.py", line 602, in invoke
    result = self.send(soapenv)
  File "/Library/Python/2.7/site-packages/suds/client.py", line 649, in send
    result = self.failed(binding, e)
  File "/Library/Python/2.7/site-packages/suds/client.py", line 708, in failed
    raise Exception((status, reason))
Exception: (307, u'Temporary Redirect')

I know I'm not properly calling the method. How can i call the AutotaskAPI with the QueryXML and ATWSResponse type?

For reference, this is the AutotaskAPI documentation: https://support.netserve365.com/help/Content/Userguides/T_WebServicesAPIv1_5.pdf

UPDATE:

Using Bodsda's suggestion, my complete code looks like this:

import os, sys
import xml.etree.ElementTree as ET
from suds.client import Client
from suds.sax.element import Element


class Init():
    def __init__(self):
        #Search the app.config file for all data to be used
        script_dir = os.path.dirname(__file__)
        file_path = "app.config"
        abs_file_path = os.path.join(script_dir, file_path) 

        tree = ET.parse(abs_file_path)
        root = tree.getroot()
        sites = root.iter('AutotaskUpdateTicketEstimatedHours.My.MySettings')
        self.data = {}
        for site in sites: 
            apps = site.findall('setting')
            for app in apps:
                self.data[app.get('name')] = app.find('value').text


class ConnectATWS():
    def __init__(self):
        #Connect to server with the credentials
        app_config = Init()
        self.username = app_config.data["Username"]
        self.password = app_config.data["Password"]
        self.login_id = app_config.data["LoginID"]
        self.url = app_config.data["AutotaskUpdateTicketEstimatedHours_net_autotask_webservices5_ATWS"]
        strQuery = """
    <queryxml>
        <entity>Ticket</entity>
        <query>
            <condition>
                <field>Id
                    <expression op="GreaterThan">0</expression>
                </field>
            </condition>
            <condition>
                <field>Status
                    <expression op="NotEqual">5</expression>
                </field>
            </condition>
            <condition>
                <field>EstimatedHours
                    <expression op="IsNull"></expression>
                </field>
            </condition>
        </query>
    </queryxml>"""

        client = Client(self.url + "?WSDL", username=self.login_id, password=self.password)
        #obj = client.factory.create('ATWSResponse')
        response = client.service.query(strQuery)

        if response.ReturnCode != 1:
            print "Error code: %s" % response.ReturnCode
            print "Error response: %s" % response.Errors
            sys.exit(1)
        else:
            os.system("clear")
            print "Query successful..."
            print "============================="
            print response.EntityResults



if __name__ == '__main__':
    handler = ConnectATWS() 
feners
  • 645
  • 5
  • 19
  • 48

1 Answers1

1

Did you paste all of your code? I don't see how it could work, for a start you never define Init() so it should error when you try to instantiate your class. I also don't see where you add information to appconfig.data dictionary.

Anyway, here's some sample code that I've been using today

#! /usr/bin/env python

import sys
import os
from suds.client import Client

at_username = "foo@bar.com"
at_password = "foobar"

at_url = "https://webservices4.autotask.net/atservices/1.5/atws.wsdl"

def main():
    client = Client(at_url, username=at_username, password=at_password)           # Instatiate a suds.client.Client instance and connect to webservices URL

    q = """
    <queryxml>
        <entity>Ticket</entity>
        <query>
            <condition>
                <field>Id
                    <expression op="GreaterThan">0</expression>
                </field>
            </condition>
            <condition>
                <field>Status
                    <expression op="NotEqual">5</expression>
                </field>
            </condition>
            <condition>
                <field>EstimatedHours
                    <expression op="IsNull"></expression>
                </field>
            </condition>
        </query>
    </queryxml>"""

    # Status value '5' == Complete

    response = client.service.query(q)

    if response.ReturnCode != 1:
        print "Error code: %s" % response.ReturnCode
        print "Error response: %s" % response.Errors
        sys.exit(1)
    else:
        os.system("clear")
        print "Query successful..."
        print "============================="
        print response.EntityResults


main()
Bodsda
  • 26
  • 1
  • I updated to show my complete code, I'm still getting the error, I don't know if something is wrong with my xml..? – feners Sep 16 '15 at 17:01
  • Can you show me the output of `print self.url + "?WSDL"` – Bodsda Sep 16 '15 at 17:28
  • Well the client was connecting to it, displaying a large list of Methods and Types.. But now I notice that it should not be `'WSDL`, it should be `?WSDL`.. – feners Sep 16 '15 at 17:45
  • You sure? The documentation (https://ww4.autotask.net/help/Content/LinkedDOCUMENTS/WSAPI/T_WebServicesAPIv1_5.pdf) suggests the URL you should be using is `https://webservices5.autotask.net/atservices/1.5/atws.wsdl` – Bodsda Sep 16 '15 at 17:46
  • ok, so I fixed that, and now I get : `TERM environment variable not set. Query successful... =============================` – feners Sep 16 '15 at 17:50
  • Cool, so you'll only get the Query successful message if the response.ReturnCode is not 1, so that proves the connection and query succeed. The "TERM environment variable not set" may be an OS thing. Try removing the `os.system("clear") line` – Bodsda Sep 16 '15 at 17:52
  • Now I just get the query successful thing, but no entity results..I do seem to get two blank spaces for some reason.. – feners Sep 16 '15 at 17:54
  • I had open a more complete question where I better describe my purpose for this here: http://stackoverflow.com/questions/32488804/how-to-use-types-and-methods-with-suds I would greatly appreciate it if you could help me out with this.. – feners Sep 16 '15 at 18:00
  • The fact that you get the "Query successful" message proves that the connection works. It also proves that your queryxml section is valid and does not produce an error. If it isn't returning you any data I would suggest that there is no data based on your conditions. Try and prove this by having just 1 condition to begin with, have a NotEqual 5 – Bodsda Sep 16 '15 at 18:50
  • Would you mind taking this to chat so I can ask you about some doubts on how to use the XMLQuery..? – feners Sep 16 '15 at 20:56
  • yeah sure, I'm around for the next 7 hours or so – Bodsda Sep 17 '15 at 09:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89930/discussion-between-feners-and-bodsda). – feners Sep 17 '15 at 15:23