【问题标题】:How to call a webapp2 method from Cloud endpoint api?如何从云端点 api 调用 webapp2 方法?
【发布时间】:2016-03-12 04:43:34
【问题描述】:

我使用 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 类吗?

【问题讨论】:

    标签: python-2.7 google-app-engine google-cloud-endpoints webapp2 requesthandler


    【解决方案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)
    

    【讨论】:

    • 谢谢乔希。这正是我最终做的。我将 GetProgramList 方法移到了 Webapp2 之外,因此 Cloudendpoint 和 Webapp2 应用程序都可以访问它。
    • 在另一个thread @moraes 中说: > 切勿在测试之外使用 set_globals()。安全吗?我有同样的情况,想与 webapp2 类进行交互。
    • 好的,上下文有单元测试。但这里的做法似乎是一样的。
    • @Tuelho 如果您需要从webapp2 处理程序从webapp2 之外的某个地方调用某些内容,那么这应该是一个危险信号,告诉您将代码与webapp2 解耦。跨度>
    • 是的!就我而言,我想在我的端点内使用webapp2.uri_for() 实用程序。
    猜你喜欢
    • 1970-01-01
    • 2015-06-20
    • 2017-09-04
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 1970-01-01
    相关资源
    最近更新 更多