python - Rest Here Api 回调函数在 python 而不是 JS

有没有办法在请求中使用 Python 中的回调函数?这来自一个 here API 示例,我试图使用 python 进行复制。这是请求和参数中最后使用的函数定义为jsoncallback=parseResponse。

如果我可以用其他方式替换回调的功能也可以,是否可以只接受响应并执行在 parseRespone 中完成的所有操作?

"https://route.api.here.com/routing/7.2/calculateroute.json?jsonAttributes=1&waypoint0=50.7799,6.08425&waypoint1=50.77988,6.08288&waypoint2=50.78144,6.07794&representation=overview&routeattributes=sc,sm,sh,bb,lg,no,shape&legattributes=li&linkattributes=sh,nl,fc&mode=fastest;car;traffic:enabled&app_id=inCUge3uprAQEtRaruyaZ8&app_code=9Vyk_MElhgPCytA7z3iuPA&jsoncallback=parseResponse"
var parseResponse = function (resp)
    {
        if (resp.error != undefined)
        {
            alert (resp.error);
            feedbackTxt.innerHTML = resp.error;
            return;
        }
        if (resp.response == undefined)
        {
            alert (resp.subtype + " " + resp.details);
            feedbackTxt.innerHTML = resp.error;
            return;
        }
        //add Routing Release number if not already done
        if (releaseRoutingShown == false){
            releaseInfoTxt.innerHTML+="<br />HLP Routing: "+resp.response.metaInfo.moduleVersion;
            routerMapRelease = resp.response.metaInfo.mapVersion;
            mapReleaseTxt.innerHTML = "HLP Routing Service based on "+routerMapRelease+ " map release";
            releaseRoutingShown = true;
        }

        var strip = new H.geo.Strip(),
        shape = resp.response.route[0].shape,
        i,
        l = shape.length;

        for(i = 0; i < l; i++)
        {
            strip.pushLatLngAlt.apply(strip, shape[i].split(',').map(function(item) { return parseFloat(item); }));
        }

        polyline = new H.map.Polyline(strip,
            {
                style:
                {
                    lineWidth: 5,
                    strokeColor: "rgba(18, 65, 145, 0.7)",
                    lineJoin: "round"
                }
            });

            group.addObject(polyline);

            var links = [];
            for(var i = 0; i < resp.response.route[0].leg.length; i++)
                links = links.concat(resp.response.route[0].leg[i].link);
            
            pdeManager.setLinks(links);
            pdeManager.setBoundingBoxContainer(group);
            pdeManager.setOnTileLoadingFinished(pdeManagerFinished);
            pdeManager.start();
    }

    function pdeManagerFinished(finishedRequests)
    {
        feedbackTxt.innerHTML = "Done. Requested " + finishedRequests + " PDE tiles for " + numLinksMatched + " route links. ";
        
        var resultHTML = '<table class="pde_table" cellpadding="2" cellspacing="0" border="1" width="90%">' +
                         '<thead>' + 
                            '<tr>' +
                            '<th width="80%">Sign</th>' +
                            '<th width="20%">#</th>' +
                            '</tr>' +
                         '</thead>' +
                         '<tbody id="maps_table_body">';

        for(var sign in signs)
        {
            resultHTML += "<tr>" + "<td>" + sign + "</td>" + "<td>" + signs[sign] + "</td>" + "</tr>";
        }
        
        resultHTML += "</tbody>" + "</table>";

        document.getElementById("resultArea").innerHTML = resultHTML;
        document.getElementById("resultArea").style.display = "block";
        
        map.addObject(group);
        map.setViewBounds(group.getBounds());
    }

回答1

您不需要使用任何回调(因为无论如何都不会执行它);省略参数并调用 response.json()

下面的代码确实提出了

未经授权。该请求并非来自授权来源。

因此您可能需要一些额外的标头等(如果凭据与站点地址匹配,则可能是 Origin 标头)。

import requests

resp = requests.get(
    url="https://route.api.here.com/routing/7.2/calculateroute.json",
    params={
        "jsonAttributes": "1",
        "waypoint0": "50.7799,6.08425",
        "waypoint1": "50.77988,6.08288",
        "waypoint2": "50.78144,6.07794",
        "representation": "overview",
        "routeattributes": "sc,sm,sh,bb,lg,no,shape",
        "legattributes": "li",
        "linkattributes": "sh,nl,fc",
        "mode": "fastest;car;traffic:enabled",
        "app_id": "inCUge3uprAQEtRaruyaZ8",
        "app_code": "9Vyk_MElhgPCytA7z3iuPA",
    },
)
# Uncomment this to see the actual error.
# print(resp.content)
resp.raise_for_status()
print(resp.json())

相似文章

最新文章