首页 文章

如何从Cloud endpoints api调用webapp2方法?

提问于
浏览
1

我用Python编写了一个GAE应用程序 .

该应用程序具有一个在Android中构建的移动组件 . 我使用的是自定义凭据,而不是使用Google OAuth进行身份验证 .

我创建了一个Cloudendpoint API,因此应用程序已登录,使用此处描述的想法 - Is there a way to secure Google cloud endpoints proto datastore?

现在从Cloudendpoint API我想从GAE中的类调用一个方法 . 你能帮我解决一下这个问题吗?

我在GAE中的类/方法是这样的

class GetProgramListHandler(basehandler.BaseHandler):   

    field1 = "none"
    field2 = "none"
    field3 = "none"

def date_handler(obj):
        return obj.isoformat() if hasattr(obj, 'isoformat') else obj
def post(self):
    logging.info(self.request.body)
    data_received = json.loads(self.request.body)

    field1 = data_received['field1']
    field12 = data_received['field2']
    field3 = data_received['field3']

    data_sent_obj, program_data_obj = self.get_program_list(current_center_admin_email_id, current_center_name_sent, current_user_email_id)

    return_data = []
    return_data = json.dumps({'data_sent': dict(data_sent_obj),
        'program_data':  [dict(p.to_dict()) for p in program_data_obj]},default = date_handler)

    self.response.headers['content-type']=("application/json;charset=UTF-8")
    self.response.out.write(return_data)


@classmethod
def get_program_list(request,field1,field2,field3) :
    field1 = field1
    field2 = field2
    field3 = field3

我的GAE应用程序是一个Web应用程序 . 我的main.py有这个

app = webapp2.WSGIApplication([

    webapp2.Route('/getprogramlist', getprogramlist.GetProgramListHandler, name='getprogramlist'),
], debug=True, config=config.config)

这很好用 .

Basehandler是webapp2 RequestHanlder

import time
import webapp2_extras.appengine.auth.models
from webapp2_extras import security

class BaseHandler(webapp2.RequestHandler):
    @webapp2.cached_property
    def auth(self):
        """Shortcut to access the auth instance as a property."""
        return auth.get_auth()

我的Cloudendpoint API代码是这样的 -

@endpoints.api(
    name='cloudendpoint', 
    version='v1')   
class LoginApi(remote.Service):

    MULTIPLY_METHOD_RESOURCE = endpoints.ResourceContainer(API_L_value_request)

    @endpoints.method(MULTIPLY_METHOD_RESOURCE, 
        API_L_value_response,
        path='hellogreeting', 
        http_method='POST',
        name='loginvalue.getloginvalue')
    def login_single(self, request):


        try:
            l_children_user_admin_pair_array = []
            program_list = []

            user_type_data = { 
                'user_type': "error",
                'user_email_id': "error",
                'user_check_flag': "errors"}

            if (user_type = "maskvalue"):       


                pass_credential_flag = "y"

                if pass_credential_flag == 'y':

                    # If the user_type is "super_admin" do this
                    if (user_type_data["user_type"] == "super-admin"):
                        field1 = l_children_user_admin_pair_array[0]["field1"]
                        field2 = l_children_user_admin_pair_array[0]["field2"]
                        field3 = user_type_data["field3"]

                        program_data = []

                        # program_data_obj = HOW DO I CALL get_program_list on GetProgramListHandler?

我想在CloudApi内部的GetProgramListHandler上调用get_program_list(在发布代码的末尾) . 这里的Stackoverflow问题 - AssertionError: Request global variable is not set似乎表明我需要初始化Webapp2RequestHandler . 我该怎么做呢?

一旦我进入CloudAPI(属于我的应用程序),我如何访问属于Web应用程序的其他类/方法?我是否需要在CloudAPI中继承Webapp类?

1 回答

  • 1

    听起来你的方法应该与Web处理程序分离,以便它可以从两个上下文中执行 . 如果由于某种原因无法执行此操作,则可以初始化空的 webapp2 请求,以避免出现其中一些错误 .

    # app is an instance of your webapp2.WSGIApplication
    req = webapp2.Request.blank('/')
    req.app = app
    app.set_globals(app=app, request=req)
    

相关问题