如何在 SUDS 中使用类型和方法 [英] How to use Types and Methods with SUDS

查看:63
本文介绍了如何在 SUDS 中使用类型和方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码通过 SUDS 连接到 Autotask 服务.我得到一个方法列表,我必须调用其中一个.我是新手,所以我不确定如何正确地做到这一点.这是我的代码,包括 Bodsda 的帮助:

My code connects to a Autotask service through SUDS. I get a list of Methods, one of which I have to call. I'm new to this, so I'm not sure on how to correctly do this. This is my code, including help by 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"]
        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>"""
        new_url = 'https://webservices5.autotask.net/atservices/1.5/atws.wsdl'
        client = Client(new_url, username=self.login_id, password=self.password)
        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:
            print "Query successful..."
            print "============================="
            print response.EntityResults



if __name__ == '__main__':
    handler = ConnectATWS()

这是我的回答:

(ATWSResponse){
   ReturnCode = 1
   EntityResults = ""
   EntityResultType = "ticket"
   Errors = ""
   EntityReturnInfoResults = ""
 }

这是我通过客户端获得的方法和类型列表:

This is the list of Methods and Types I get through the client:

    Suds ( https://fedorahosted.org/suds/ )  version: 0.4 GA  build: R699-20100913

Service ( ATWS ) tns="http://autotask.net/ATWS/v1_5/"
   Prefixes (1)
      ns0 = "http://autotask.net/ATWS/v1_5/"
   Ports (1):
      (ATWSSoap)
         Methods (14):
            CreateAttachment(Attachment attachment, )
            DeleteAttachment(xs:long attachmentId, )
            GetAttachment(xs:long attachmentId, )
            GetFieldInfo(xs:string psObjectType, )
            GetInvoiceMarkup(xs:int InvoiceId, xs:string Format, )
            GetWsdlVersion()
            create(ArrayOfEntity Entities, )
            delete(ArrayOfEntity Entities, )
            getEntityInfo()
            getThresholdAndUsageInfo()
            getUDFInfo(xs:string psTable, )
            getZoneInfo(xs:string UserName, )
            query(xs:string sXML, )
            update(ArrayOfEntity Entities, )
         Types (108):
            ATWSError
            ATWSResponse
            ATWSZoneInfo
            Account
            AccountLocation
            AccountNote
            AccountTeam
            AccountToDo
            ActionType
            AdditionalInvoiceFieldValue
            AllocationCode
            Appointment
            ArrayOfATWSError
            ArrayOfEntity
            ArrayOfEntityInfo
            ArrayOfEntityReturnInfo
            ArrayOfField
            ArrayOfPickListValue
            ArrayOfUserDefinedField
            Attachment
            AttachmentInfo
            AutotaskIntegrations
            BillingItem
            BillingItemApprovalLevel
            ChangeRequestLink
            ClientPortalUser
            Contact
            Contract
            ContractBlock
            ContractCost
            ContractFactor
            ContractMilestone
            ContractNote
            ContractRate
            ContractRetainer
            ContractService
            ContractServiceAdjustment
            ContractServiceBundle
            ContractServiceBundleAdjustment
            ContractServiceBundleUnit
            ContractServiceUnit
            ContractTicketPurchase
            Country
            Department
            Entity
            EntityDuplicateStatus
            EntityInfo
            EntityReturnInfo
            EntityReturnInfoDatabaseAction
            ExpenseItem
            ExpenseReport
            Field
            InstalledProduct
            InstalledProductType
            InstalledProductTypeUdfAssociation
            InternalLocation
            InventoryItem
            InventoryItemSerialNumber
            InventoryLocation
            InventoryTransfer
            Invoice
            InvoiceTemplate
            Opportunity
            PaymentTerm
            Phase
            PickListValue
            Product
            ProductVendor
            Project
            ProjectCost
            ProjectNote
            PurchaseOrder
            PurchaseOrderItem
            PurchaseOrderReceive
            Quote
            QuoteItem
            QuoteLocation
            Resource
            ResourceRole
            ResourceSkill
            Role
            SalesOrder
            Service
            ServiceBundle
            ServiceBundleService
            ServiceCall
            ServiceCallTask
            ServiceCallTaskResource
            ServiceCallTicket
            ServiceCallTicketResource
            ShippingType
            Skill
            Task
            TaskNote
            TaskPredecessor
            TaskSecondaryResource
            Tax
            TaxCategory
            TaxRegion
            Ticket
            TicketChangeRequestApproval
            TicketCost
            TicketNote
            TicketSecondaryResource
            TimeEntry
            UserDefinedField
            UserDefinedFieldDefinition
            UserDefinedFieldListItem

我需要使用 ATWSResponse 类型,但我真的不明白如何做到这一点.此外,我正在尝试模拟这个 vb.net 代码部分,它完成了我在 Python 中尝试做的事情:

I need to use the ATWSResponse type but I don't really understand how to do this. Furthermore, I'm attempting to simulate this vb.net code part which does what I'm trying to do in Python:

Sub ProcessTicket()
    Dim boolQueryFinished = False
    Dim strCurrentID As String = "0"
    Dim strQuery As String
    Dim strCriteria As String = ""
    Dim TicketArray(0) As Ticket

    While (Not (boolQueryFinished))
        If LCase(Trim(varIgnoreTicketStatus)) = "true" Then
            WriteToLog("Updating All Tickets Where EstimatedHours Is Null")
        Else
            WriteToLog("Updating Ticket Where Status <> Complete And EstimatedHours Is Null")
            ' 5 - Complete
            strCriteria = "<condition><field>Status<expression op=""NotEqual"">5</expression></field></condition>"
        End If

        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>"

        Dim r As ATWSResponse
        Dim strLog As String

        r = ATWSService.query(strQuery)
        WriteToLog("Found " & r.EntityResults.Length & " Tickets begining at " & strCurrentID)

        If r.EntityResults.Length > 0 Then
            For Each ent As Entity In r.EntityResults
                CType(ent, Ticket).EstimatedHours = EstHoursDefault

                TicketArray(0) = CType(ent, Ticket)

                Dim sResponse As ATWSResponse
                Dim entityArray() As Entity = CType(TicketArray, Entity())
                sResponse = ATWSService.update(entityArray)

我的问题是如何使用我的 Python 代码成功查询 QueryXML 并返回像 vb.net 代码中的实体?

My question is how can I successfully query with the QueryXML with my Python code and return entities like in the vb.net code?

推荐答案

首先,用于连接到 api 的 url 是不正确的.我需要使用 https://webservices5.autotask.net/atservices/1.5/atws.wsdl.

First of all, the url being used to connect to the api was not the correct one. I needed to use https://webservices5.autotask.net/atservices/1.5/atws.wsdl.

一旦客户端收到响应,我必须匹配 ReturnCode 才能运行我想要的代码.返回码不能为 1(这是查询中的错误),因此我使用了:

Once the client received a response, I had to match the ReturnCode in order to run the code I wanted. The return code could not be 1 (which is an error in the query), therefore I used:

        if response.ReturnCode != 1:
            print "Error code: %s" % response.ReturnCode
            print "Error response: %s" % response.Errors
            sys.exit(1)
        else:
            print "Query successful..."
            print "============================="
            response = response.EntityResults[0]

为了存储我返回的数据(实体),即票证,我必须执行以下循环:

In order to store my returned data (entities), which were Tickets, I had to do the following loops:

            entities = []
            for entity in response:
                entities.append(dict(entity))

            for entity in entities:
                self.query_data.append(entity["Title"] + "  Estimated Hours " + str(entity["EstimatedHours"]))

这样我就可以将元组列表存储在实体列表中,以便访问每个实体本身.完成后,我所要做的就是将结果附加到列表 self.query_data 中.

This way I could store the list of tuples in entities list in order to then access each entity themselves. With this done, all I had to do was append the results to a list self.query_data.

注意:感谢 Bodsda,他帮助我弄清楚我是如何用我的 XML 查询错误地址的,以及循环方面的帮助.

Note: Credit to Bodsda for his help in figuring out how I was querying the wrong address with my XML, plus help with the loops.

这篇关于如何在 SUDS 中使用类型和方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆