如何循环浏览soap API的列表?

2024-05-14 16:17:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我不熟悉为客户编写和开发这个新的鸡尾酒应用程序,可能需要一些帮助。我必须对客户端进行soap API调用以获取数据,但一次只能进行一次调用。连续拨打电话的最佳方式是什么?我做了一些搜索以找到构建xml文件和该文件的最佳方法,但我在代码中破坏了一些东西

coctails csv:

cocktail_name,cocktail_base,ingredients,portions in oz,garnish
last word,gin,chartreuse&maraschino&lime,2&3/4th&3/4th&3/4th,cherry
whiskey sour,whiskey,lemon juice&simple syrp,2&3/4&1/2,bitters

这是我的密码:

import xml.etree.ElementTree as ET



#API Params:

target_url = "https://www.cocktailingredsdemo.com/"

api_headers = {301972e3e060102biwkrw5681rf68afd648ce098af0}

#storing the results:
 api_results = []
error_response = []
with open("cocktail_list.csv") as f: 
   reader = csv.DictReader(f)

   #writing out xml for cocktail list

for i, row in enumerate(reader, start=1):
       root = ET.Element('cocktail')
       ET.SubElement(root, "cocktail_name").text = row['cocktail_name']

       ingredientsNode = ET.SubElement(root, "cocktail_ingredients") 
       for code in row['ingredients'].split('&'):
           ET.SubElement(ingredientsNode, "ci").text = code

       ET.SubElement(root, "portions").text = row['portions in oz']
       ET.SubElement(root, "garnish").text = row['garnish']

       cocktail_xml = ET.tostring(root, method='xml', encoding="UTF-8")
       with open(f'cocktail_data.xml', 'wb') as f: 
              f.write(cocktail_xml) 


       response = requests.post(target_url, data=cocktail_xml, headers=api_headers)
       url_response = json.loads(response.text)
       if type(data)==dict and 'error' in data.keys():
           error_response.append(data)
       else:
          results.append(data)

这种方法不起作用:当我循环每一行时,只返回最后一行,最后一个xml被用来进行API调用

这样做的最佳方式是什么,以便它循环通过每一行并进行api调用


Tags: csvtextnameinapidataresponseroot

热门问题