在这里休息python的API回调函数

发布于 2025-01-30 06:06:33 字数 3613 浏览 1 评论 0原文

有没有一种方法可以在Python中使用请求使用回调功能?这是来自此处的API示例,我试图使用Python复制。这是参数中末尾使用的函数,定义了jsoncallback = parseresponse。

如果我可以以另一种方式替换回调的功能,那是否可以接受响应并完成分娩术中完成的所有操作?

"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());
    }

Is there a way to use callback functions in Python with requests? This is from a here API example im trying to copy using python. This is the request and the function used at the end in the parameters is defined jsoncallback=parseResponse.

If i can replace the functionality of the callback in a other way would be fine too, is it possible to just take the response and do everything thats done in 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());
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弃爱 2025-02-06 06:06:33

您不需要使用任何回调(因为没有什么可以执行的); elide参数并调用response.json()

下面的代码确实提高了

未经授权。该请求不是来自授权来源。

因此,您可能需要一些额外的标题或此类标题(如果将凭据匹配到站点地址,则可能是原始标题)。

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

You don't need to use any callback (since there's nothing that would execute it anyway); elide the parameter and call response.json().

The below code does raise

Unauthorized. The request is not from an authorized source.

so you may need some additional headers or such (possibly an Origin header if the credentials are matched to a site address).

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())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文