Python 2.7 如何从云端点api调用webapp2方法?
我已经使用Python编写了一个GAE应用程序 该应用程序有一个在Android中构建的移动组件。我正在使用自定义凭据,而不是使用GoogleOAuth进行身份验证 我已经创建了一个Cloudendpoint API,这样应用程序就可以登录了,使用这里描述的思想- 现在,从CloudEndpointAPI中,我想从GAE中的类调用一个方法。你能帮我怎么组织这个吗 我在GAE中的类/方法是Python 2.7 如何从云端点api调用webapp2方法?,python-2.7,google-app-engine,google-cloud-endpoints,webapp2,requesthandler,Python 2.7,Google App Engine,Google Cloud Endpoints,Webapp2,Requesthandler,我已经使用Python编写了一个GAE应用程序 该应用程序有一个在Android中构建的移动组件。我正在使用自定义凭据,而不是使用GoogleOAuth进行身份验证 我已经创建了一个Cloudendpoint API,这样应用程序就可以登录了,使用这里描述的思想- 现在,从CloudEndpointAPI中,我想从GAE中的类调用一个方法。你能帮我怎么组织这个吗 我在GAE中的类/方法是 class GetProgramListHandler(basehandler.BaseHandler):
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 requesthandler
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()
我的CloudEndpointAPI代码如下-
@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问题似乎表明我需要初始化Webapp2RequestHandler。我该怎么做
一旦我进入了CloudAPI(属于我的应用程序),我如何访问属于web应用程序的其他类/方法?我是否需要在CloudAPI中继承Webapp类?听起来您的方法应该与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应用程序都可以访问它。安全吗?我也有同样的情况,希望与webapp2类交互。好的,这里的上下文是单元测试。但是这里的方法似乎是一样的。@Tuelho如果您需要从
webapp2
之外的某个地方从webapp2
处理程序调用某个东西,那么这应该是一个警告信号,告诉您将代码与webapp2
解耦。是的!在我的例子中,我希望在端点内使用webapp2.uri\u for()
实用程序。