【问题标题】:How to use QueryXML with SUDS and AutotaskAPI如何将 QueryXML 与 SUDS 和 AutotaskAPI 一起使用
【发布时间】:2015-09-12 05:36:34
【问题描述】:

我一直在尝试使用使用 QueryXML 的 AutotaskAPI 的 query() 方法。为了做到这一点,我知道我必须使用 ATWSResponse 类型才能接收结果。这是我的代码:

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)

尝试这样做会返回以下错误:

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')

我知道我没有正确调用该方法。如何使用 QueryXML 和 ATWSResponse 类型调用 AutotaskAPI?

作为参考,这是 AutotaskAPI 文档: https://support.netserve365.com/help/Content/Userguides/T_WebServicesAPIv1_5.pdf

更新:

使用 Bodsda 的建议,我的完整代码如下所示:

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() 

【问题讨论】:

    标签: python python-2.7 suds


    【解决方案1】:

    您是否粘贴了所有代码?我看不出它是如何工作的,首先你从来没有定义过 Init() 所以当你尝试实例化你的类时它应该会出错。我也看不到您将信息添加到 appconfig.data 字典的位置。

    不管怎样,这是我今天一直在使用的一些示例代码

    #! /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()
    

    【讨论】:

    • 我更新显示我的完整代码,我仍然收到错误,我不知道我的 xml 是否有问题..?
    • 你能告诉我print self.url + "?WSDL"的输出吗
    • 客户端正在连接它,显示了大量的方法和类型。但现在我注意到它不应该是'WSDL,它应该是?WSDL..
    • 你确定?文档 (ww4.autotask.net/help/Content/LinkedDOCUMENTS/WSAPI/…) 建议您应该使用的 URL 是 https://webservices5.autotask.net/atservices/1.5/atws.wsdl
    • 好的,所以我解决了这个问题,现在我得到了:TERM environment variable not set. Query successful... =============================
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    • 2015-05-10
    • 2017-09-14
    • 2013-05-10
    • 2019-11-03
    • 2021-01-09
    相关资源
    最近更新 更多