如何在承诺模式下使用JavaScript库调用App Engine端点 [英] How to call App Engine Endpoints with the JavaScript library in promises mode

查看:97
本文介绍了如何在承诺模式下使用JavaScript库调用App Engine端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我目前正在将此应用程序从回调模式更改为承诺模式,如Google推荐的那样( https://developers.google。 com / api-client-library / javascript / features / promises#using-promises ),我遇到了一个问题。请注意,该应用程序在回调模式下效果良好。



我在promises模式下遇到的问题是找到正确的路径参数当调用请求方法时:

JavaScrit代码:

  var params = {'webSafeKeyParent':'neN4fm15xW52b2ljZXMtb19saW5lmlYLEglBY1NFwpRpdHkYgICAgQj97AoM'}; 
gapi.client.request({
'path':'https://myappenginename.appspot.com/_ah/api/customerApi/v1/ ?????????',
'params':params
})。then(function(response){
// Handle response
},function(reason){
//处理错误
});

customerApi中的端点定义:

  @ApiMethod(
name =listByParent,
path =customerByParent / {webSafeKeyParent},
httpMethod = ApiMethod.HttpMethod.GET,
scopes = {Constants.EMAIL_SCOPE},
clientIds = {Constants.WEB_CLIENT_ID,com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID},
audiences = {Constants.ANDROID_AUDIENCE})
公开列表< Customer> listByParent(最终用户用户@Named(webSafeKeyParent)最终字符串webSafeKeyParent,@Nullable @Named(游标)字符串游标,@Nullable @Named(限制)整数限制)抛出UnauthorizedException {

对于我的一些终结点,它的工作方式是在JavaScript请求方法的路径参数中包含path和名称在@ApiMethod注释中声明。



即对于上述端点,以下路径有效:
https:/ /myappenginename.appspot.com/_ah/api/customerApi/v1/customerByParent/listByParent



奇怪的是,这并不适用于其他一些相同的端点类。我收到了404 HTTP错误或503错误。



当您使用API​​资源管理器查询端点时,我也尝试了在请求下显示的路径,但没有成功......



有没有关于如何使用Google API JavaScript客户端库以承诺调用App Engine端点的详细文档?我还没有找到。你有什么建议可以分享吗?



预先致谢

解决方案

实际上,请求方法可以在路径参数中工作,所有这些参数由@ApiMethod注释中声明的路径和名称的值组成......



如果它对某些端点不起作用,那我就是一个错误。不过,不知道是哪个错误。注意,我注意到向JavaScript请求方法传递App Engine端点的正确httpMethod非常重要。默认情况下,请求方法假定它是一个GET。如果您的端点在@ApiMethod注释中有httpMethod = ApiMethod.HttpMethod.POST,则应传递参数'method':'POST',详见文档: https://developers.google.com/api-client-library/javascript/reference/referencedocs#gapiclientrequestargs


I have a web application which calls several App Engine Endpoints with the Google API JavaScript client library.

I am currently changing this application from callback mode to promises mode, as recommended by Google (https://developers.google.com/api-client-library/javascript/features/promises#using-promises) and I am encountering a problem. Note that the app works well with the callback mode.

My problem with the promises mode is to find what is the correct path argument to use when calling the request method:

JavaScrit code:

var params = {'webSafeKeyParent’: ‘neN4fm15xW52b2ljZXMtb19saW5lmlYLEglBY1NFwpRpdHkYgICAgQj97AoM’};
gapi.client.request({
      'path': 'https://myappenginename.appspot.com/_ah/api/customerApi/v1/?????????',
      'params': params
}).then(function(response) {
        // Handle response       
}, function(reason) {
        // Handle error
});

Endpoint definition in "customerApi":

@ApiMethod(
        name = "listByParent",
        path = "customerByParent/{webSafeKeyParent}",
        httpMethod = ApiMethod.HttpMethod.GET,
        scopes = {Constants.EMAIL_SCOPE},
        clientIds = {Constants.WEB_CLIENT_ID, com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID},
        audiences = {Constants.ANDROID_AUDIENCE})
public List<Customer> listByParent(final User user, @Named("webSafeKeyParent") final String webSafeKeyParent, @Nullable @Named("cursor") String cursor, @Nullable @Named("limit") Integer limit) throws UnauthorizedException {

For few of my endpoints it works by including in the path argument of the JavaScript request method, the values of "path" and "name" as declared in the @ApiMethod annotation.

i.e. for the above endpoint, the following path works: https://myappenginename.appspot.com/_ah/api/customerApi/v1/customerByParent/listByParent

Strangely enough this does NOT work for some other endpoints of the same kind. I receive either a 404 HTTP error or a 503 one.

I've also tried with the paths displayed under "Request" when you query the endpoints with the APIs Explorer but without success....

Is there any detailed documentation on how to call App Engine Endpoints with promises, with the Google API JavaScript client library? I have not found any. Do you have some advice to share please?

Thanks in advance

解决方案

Actually the request method DOES work ALL THE TIME with the "path" argument composed of the values of "path" and "name" as declared in the @ApiMethod annotation...

It was a mistake on my side if it didn't work for some endpoints. Don't know which mistake, however.

Note that I have noticed that it is very important to pass to the JavaScript request method the correct httpMethod of the App Engine Endpoints. By default the request methid assumes that it is a GET. In case your Endpoint has httpMethod= ApiMethod.HttpMethod.POST in the @ApiMethod annotation, you shall pass the argument 'method': 'POST', as detailed in the doc: https://developers.google.com/api-client-library/javascript/reference/referencedocs#gapiclientrequestargs

这篇关于如何在承诺模式下使用JavaScript库调用App Engine端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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