向供应商 API 发送更新的 Python 程序可以接受处理返回响应

问题描述 投票:0回答:1

这是我第一次尝试使用 API。我使用了 Python,一切似乎都正常——我能够向供应商发送 17,500 多条记录,并且它们在他们的网站上完美显示。问题是供应商使用的 ID 与我们不同。我在公立学校系统工作,这是为了我们的巴士交通。我经过供应商巴士站,在他们的回应中,他们向我传递了创建的 ID。然后,我必须获取该 ID 并将其传递到 SQL Server 过程中以更新交叉引用表。我通过仅发送一条记录进行了测试,效果非常好,因此我打开了该流程来推送所有 17,500 多个记录。当我检查交叉引用表时,我看到了意想不到的结果。在 17,500 条记录中,只有 13 条记录更新了供应商 ID。最重要的是,有一个状态字段应该从“New”更新为“Processed”,但即使在更新了供应商 ID 的 13 条记录中,它也没有更新。

我不知道我做错了什么,但我希望有人能发现问题。这是我的Python代码:

import pyodbc as db 
import requests

url = "<vendor URL>"

def GetData():
    connVendorData = db.connect(<Driver Info>)
    connVendorData.setdecoding(db.SQL_CHAR, encoding='latin1')
    connVendorData.setencoding('latin1')
    
    cursor_Vendor = connVendorData.cursor()
    cursor_Vendor.execute('''exec get_addressBookUpdates''')

    for row in cursor_Vendor: 
        payload = {
            "geofence": { "circle": {
                    "latitude": str(row.latitude),
                    "longitude": str(row.longitude),
                    "radiusMeters": str(row.radius)
                } },
            "formattedAddress": row.formattedAddress,
            "longitude": str(row.longitude),
            "latitude": str(row.latitude),
            "name": row.addressBookTitle
        }
        headers = {
            "accept": "application/json",
            "content-type": "application/json",
            "authorization": "<vendor token>"
        }

        response = requests.post(url, json=payload, headers=headers)

        connMyCompanyData = db.connect(<Driver Info>)
        connMyCompanyData.setdecoding(db.SQL_CHAR, encoding='latin1')
        connMyCompanyData.setencoding('latin1')

        cursor_MyCompany = connMyCompanyData.cursor()
        cursor_MyCompany.execute('''exec update_stopIDs_xref_AfterProcessing ?, ?''', row.status, response.text)
        
GetData()

这是更新交叉引用表的存储过程:

alter PROC update_stopIDs_xref_AfterProcessing
@status VARCHAR(25),
@json NVARCHAR(MAX)

AS

SET NOCOUNT ON 

--Test Parms
--DECLARE @json nvarchar(MAX)
--DECLARE @status VARCHAR(25) = 'New'

--SET @json = '{
--  "data": {
--    "id": "137279769",
--    "name": "MILLGATE RD @ LA GRANGE RD",
--    "createdAtTime": "2024-05-12T15:12:47.880383692Z",
--    "formattedAddress": "MILLGATE RD @ LA GRANGE RD",
--    "geofence": {
--      "circle": {
--        "latitude": 38.27088968,
--        "longitude": -85.57484079,
--        "radiusMeters": 25
--      }
--    },
--    "latitude": 38.27088968,
--    "longitude": -85.57484079
--  }
--}'

--Load the parms into a #temp table 
DROP TABLE IF EXISTS #temp 
SELECT  JSON_VALUE(@json, '$.data.id')                  vendorID,
        JSON_VALUE(@json, '$.data.formattedAddress')    formattedAddress,
        JSON_VALUE(@json, '$.data.createdAtTime')       createdTime
INTO    #temp 

--Update stopIDs_xref depending on the processing that happened 
IF @status = 'New'
    UPDATE  stopIDs_xref
    SET     vendorId = t.vendorID,
            status = 'Processed'
    FROM    stopIDs_xref sx
            JOIN #temp t ON t.formattedAddress = sx.formattedAddress 
ELSE IF @status = 'Update'
    UPDATE  stopIDs_xref 
    SET     status = 'Processed'
    FROM    stopIDs_xref sx
            JOIN #temp t ON t.formattedAddress = sx.formattedAddress
ELSE IF @status = 'Delete'
    DELETE 
    FROM    stopIDs_xref 
    FROM    stopIDs_xref sx
            JOIN #temp t ON t.formattedAddress = sx.formattedAddress 
python t-sql
1个回答
0
投票

经过更多研究,我发现问题是我的代码中的一些基本逻辑缺陷。 1)我的连接字符串指向我的SQL Server,它在for循环中,所以我本质上是在每次迭代中创建一个新的连接,我猜这会让数据库不堪重负。我将连接字符串移到了 for 循环之外。 2)这样做,我可以跟随更新的发生,一切看起来都很好,但由于某种原因,当处理完成时,更新将“消失”,并且数据被恢复为原始值。发现我需要在逻辑中添加一个提交。 3) 将连接关闭字符串移至逻辑末尾。之后一切都按预期进行。希望这可以帮助其他可能遇到同样问题的人。

import pyodbc as db 
import requests

url_insert = "https://api.samsara.com/addresses"
connSamsaraData = db.connect('Driver={SQL Server};'
            'Server=appdb.jefferson.kyschools.us,7771;'
            'Database=Transportation_Samsara;'
            'UID:jcps_reports;'
            'PWD=Gl4sRsr3p0rt5'
            'Trusted_Connection=yes;')
cursor_Samsara = connSamsaraData.cursor()

connJCPSData = db.connect('Driver={SQL Server};'
            'Server=appdb.jefferson.kyschools.us,7771;'
            'Database=Transportation_Samsara;'
            'UID:jcps_reports;'
            'PWD=Gl4sRsr3p0rt5'
            'Trusted_Connection=yes;')
cursor_JCPS = connJCPSData.cursor()

def InsertNewAddresses():
    cursor_Samsara.execute('''exec get_addressBookInserts''')

    for row in cursor_Samsara: 
    payload = {
        "geofence": { "circle": {
            "latitude": str(row.latitude),
            "longitude": str(row.longitude),
            "radiusMeters": str(row.radius)
        } },
        "formattedAddress": row.formattedAddress,
        "longitude": str(row.longitude),
        "latitude": str(row.latitude),
        "name": row.addressBookTitle
    }
    headers = {
        "accept": "application/json",
        "content-type": "application/json",
        "authorization": "Bearer samsara_api_cDRDqPgdOJsnnOBPkAKXAicynXPA8L"
    }

    response = requests.post(url_insert, json=payload, headers=headers)

    cursor_JCPS.execute('''exec update_stopIDs_xref_AfterProcessing ?, ?''', row.status, response.text)

    cursor_JCPS.commit()

def UpdateExistingAddresses():
    cursor_Samsara.execute('''exec get_addressBookUpdates''')

    for row in cursor_Samsara: 
    url_update = "https://api.samsara.com/addresses/" + str(row.samsaraId)

    payload = {
        "geofence": { "circle": {
            "latitude": str(row.latitude),
            "longitude": str(row.longitude),
            "radiusMeters": str(row.radius)
        } },
        "formattedAddress": row.formattedAddress,
        "latitude": str(row.latitude),
        "longitude": str(row.longitude),
        "name": row.addressBookTitle
    }
    headers = {
        "accept": "application/json",
        "content-type": "application/json",
        "authorization": "Bearer samsara_api_cDRDqPgdOJsnnOBPkAKXAicynXPA8L"
    }

    response = requests.patch(url_update, json=payload, headers=headers)

    print(response.text)

    cursor_JCPS.execute('''exec update_stopIDs_xref_AfterProcessing ?, ?''', row.status, response.text)

    cursor_JCPS.commit()

def DeleteOldAddresses():
    cursor_Samsara.execute('''exec get_addressBookDeletes''')

    for row in cursor_Samsara: 
    url_delete = "https://api.samsara.com/addresses/" + str(row.samsaraId)

    headers = {
        "accept": "application/json",
        "authorization": "Bearer samsara_api_cDRDqPgdOJsnnOBPkAKXAicynXPA8L"
    }

    response = requests.delete(url_delete, headers=headers)

    cursor_JCPS.execute('''exec update_stopIDs_xref_AfterProcessing ?, ?''', row.status, response.text)

    cursor_JCPS.commit()

InsertNewAddresses()
UpdateExistingAddresses()
DeleteOldAddresses()

connSamsaraData.close()
connJCPSData.close()
© www.soinside.com 2019 - 2024. All rights reserved.