本文整理匯總了Python中endpoints.api_server方法的典型用法代碼示例。如果您正苦於以下問題:Python endpoints.api_server方法的具體用法?Python endpoints.api_server怎麽用?Python endpoints.api_server使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類endpoints
的用法示例。
在下文中一共展示了endpoints.api_server方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_get_game
# 需要導入模塊: import endpoints [as 別名]
# 或者: from endpoints import api_server [as 別名]
def test_get_game(self):
"""Functional test for api call to get an existing game"""
#create the api
api_call = '/_ah/spi/GameApi.get_game'
app = endpoints.api_server([GameApi], restricted=False)
testapp = webtest.TestApp(app)
#create two players
game, first_user, second_user = self._get_new_game()
#the expected request object as a dictionary, to be serialised to JSON by webtest
request = {"urlsafe_game_key":game.key.urlsafe()}
resp = testapp.post_json(api_call, request)
#check correct default values have been created
self.assertEqual(resp.json['next_move'], first_user.name)
self.assertEqual(resp.json['game_over'], False)
self.assertEqual(resp.json['unmatched_pairs'], "8")
self.assertEqual(resp.json['first_user_score'], "0")
self.assertEqual(resp.json['second_user_score'], "0")
self.assertEqual(resp.json['history'], "[]")
示例2: test_make_move_no_match
# 需要導入模塊: import endpoints [as 別名]
# 或者: from endpoints import api_server [as 別名]
def test_make_move_no_match(self, mock_taskqueue):
"""Functional test for api call to make a move"""
#create the api
api_call = '/_ah/spi/GameApi.make_move'
app = endpoints.api_server([GameApi], restricted=False)
testapp = webtest.TestApp(app)
#create a new game with a mock gridboard
(game, first_user, second_user) = self._get_new_game_with_mock_gridboard()
#make the first move, flip DEATH. Test first guess made.
#the expected request object as a dictionary, to be serialised to JSON by webtest
request = {"urlsafe_game_key":game.key.urlsafe(), "name":first_user.name, "row":0, "column":0}
resp = testapp.post_json(api_call, request)
self.assertEqual(resp.json['message'], "One more guess to make")
#test card is flipped
self.assertTrue(game.board[0][0].flipped)
#make the second move, flip FOOL. Test match wasn't made.
request = {"urlsafe_game_key":game.key.urlsafe(), "name":first_user.name, "row":0, "column":1}
resp = testapp.post_json(api_call, request)
self.assertEqual(resp.json['message'], "Not a match")
self.assertTrue(game.board[0][1].flipped)
self.assertTrue(resp.json['history'], "[good golly:DEATH:0:0:FOOL:0:1:False]")
示例3: test_make_move_game_not_found
# 需要導入模塊: import endpoints [as 別名]
# 或者: from endpoints import api_server [as 別名]
def test_make_move_game_not_found(self, mock_taskqueue):
"""Functional test for api call to make a move"""
#create the api
api_call = '/_ah/spi/GameApi.make_move'
app = endpoints.api_server([GameApi], restricted=False)
testapp = webtest.TestApp(app)
#create a new game with a mock gridboard
(game, first_user, second_user) = self._get_new_game_with_mock_gridboard()
#test not your turn
request = {"urlsafe_game_key":game.key.urlsafe(), "name":second_user.name, "row":0, "column":1}
self.assertRaises(Exception, testapp.post_json, api_call, request)
#test game not found
request = {"urlsafe_game_key":"asadfdsf", "name":first_user.name, "row":0, "column":1}
self.assertRaises(Exception, testapp.post_json, api_call, request)
示例4: test_make_move_invalid_move
# 需要導入模塊: import endpoints [as 別名]
# 或者: from endpoints import api_server [as 別名]
def test_make_move_invalid_move(self, mock_taskqueue):
"""Functional test for api call to make a move"""
#create the api
api_call = '/_ah/spi/GameApi.make_move'
app = endpoints.api_server([GameApi], restricted=False)
testapp = webtest.TestApp(app)
#create a new game with a mock gridboard
(game, first_user, second_user) = self._get_new_game_with_mock_gridboard()
#test exception raised if exceed gridboard boundary
request = {"urlsafe_game_key":game.key.urlsafe(), "name":first_user.name, "row":0, "column":-1}
self.assertRaises(Exception, testapp.post_json, api_call, request)
request = {"urlsafe_game_key":game.key.urlsafe(), "name":first_user.name, "row":4, "column":0}
self.assertRaises(Exception, testapp.post_json, api_call, request)
game.board[0][0].flip()
#test exception raised if card already flipped
request = {"urlsafe_game_key":game.key.urlsafe(), "name":first_user.name, "row":0, "column":0}
self.assertRaises(Exception, testapp.post_json, api_call, request)
示例5: bookmark
# 需要導入模塊: import endpoints [as 別名]
# 或者: from endpoints import api_server [as 別名]
def bookmark(self, request):
return Response()
# [END books]
# [END multiclass]
# [START api_server]
示例6: greet
# 需要導入模塊: import endpoints [as 別名]
# 或者: from endpoints import api_server [as 別名]
def greet(self, request):
user = endpoints.get_current_user()
user_name = user.email() if user else 'Anonymous'
return Greeting(message='Hello, {}'.format(user_name))
# [END authed_greeting_api]
# [START api_server]
示例7: get_user_email
# 需要導入模塊: import endpoints [as 別名]
# 或者: from endpoints import api_server [as 別名]
def get_user_email(self, request):
user = endpoints.get_current_user()
# If there's no user defined, the request was unauthenticated, so we
# raise 401 Unauthorized.
if not user:
raise endpoints.UnauthorizedException
return EchoResponse(content=user.email())
# [END echo_api]
# [START api_server]
示例8: test_new_game
# 需要導入模塊: import endpoints [as 別名]
# 或者: from endpoints import api_server [as 別名]
def test_new_game(self):
"""Functional test for api call to get a new game"""
#create the api
api_call = '/_ah/spi/GameApi.new_game'
app = endpoints.api_server([GameApi], restricted=False)
testapp = webtest.TestApp(app)
#create two players
first_user, second_user = self._get_two_players()
#the expected request object as a dictionary, to be serialised to JSON by webtest
request = {"first_user":first_user.name, "second_user":second_user.name}
resp = testapp.post_json(api_call, request)
#check correct default values have been created
self.assertEqual(resp.json['next_move'], first_user.name)
self.assertEqual(resp.json['game_over'], False)
self.assertEqual(resp.json['unmatched_pairs'], "8")
self.assertEqual(resp.json['first_user_score'], "0")
self.assertEqual(resp.json['second_user_score'], "0")
self.assertEqual(resp.json['history'], "[]")
#test user not found
request = {"first_user":"", "second_user":""}
self.assertRaises(Exception, testapp.post_json, api_call, request)
#test calling new game with the same user twice
request = {"first_user":first_user.name, "second_user":first_user.name}
self.assertRaises(Exception, testapp.post_json, api_call, request)
示例9: test_make_move_made_match
# 需要導入模塊: import endpoints [as 別名]
# 或者: from endpoints import api_server [as 別名]
def test_make_move_made_match(self, mock_taskqueue):
"""Functional test for api call to make a move"""
#create the api
api_call = '/_ah/spi/GameApi.make_move'
app = endpoints.api_server([GameApi], restricted=False)
testapp = webtest.TestApp(app)
#create a new game with a mock gridboard
(game, first_user, second_user) = self._get_new_game_with_mock_gridboard()
#make the first move, flip DEATH. Test first guess made.
#the expected request object as a dictionary, to be serialised to JSON by webtest
request = {"urlsafe_game_key":game.key.urlsafe(), "name":first_user.name, "row":0, "column":0}
resp = testapp.post_json(api_call, request)
self.assertEqual(resp.json['message'], "One more guess to make")
#test card is flipped
self.assertTrue(game.board[0][0].flipped)
#make the second move, flip DEATH. Test match is made.
request = {"urlsafe_game_key":game.key.urlsafe(), "name":first_user.name, "row":0, "column":2}
resp = testapp.post_json(api_call, request)
self.assertEqual(resp.json['message'], "You made a match")
self.assertTrue(game.board[0][2].flipped)
self.assertTrue(resp.json['history'], "[good golly:DEATH:0:0:DEATH:0:2:True]")
#test game history with two moves
request = {"urlsafe_game_key":game.key.urlsafe(), "name":first_user.name, "row":3, "column":3}
resp = testapp.post_json(api_call, request)
request = {"urlsafe_game_key":game.key.urlsafe(), "name":first_user.name, "row":3, "column":2}
resp = testapp.post_json(api_call, request)
self.assertEqual(resp.json['history'], "[good golly:DEATH:0:0:DEATH:0:2:True, good golly:HERMIT:3:3:TEMPERANCE:3:2:False]")
示例10: test_get_user_scores
# 需要導入模塊: import endpoints [as 別名]
# 或者: from endpoints import api_server [as 別名]
def test_get_user_scores(self):
"""Functional test for api call to retrieve user scores"""
#create the api
api_call = '/_ah/spi/GameApi.get_user_scores'
app = endpoints.api_server([GameApi], restricted=False)
testapp = webtest.TestApp(app)
#create some scores
player_one, player_two = self._get_two_players()
scores.new_score(winner=player_one.key, loser=player_two.key, first_user_score=3, second_user_score=2)
#test user not found
request = {"name":"asdfsdf"}
self.assertRaises(Exception, testapp.post_json, api_call, request)
#test scores retrieved
request = {"name":player_one.name}
resp = testapp.post_json(api_call, request)
self.assertEqual(len(resp.json['items']), 1)
self.assertEquals(resp.json['items'][0]['winner'], player_one.name)
self.assertEquals(resp.json['items'][0]['winner_score'], "3")
self.assertEquals(resp.json['items'][0]['loser'], player_two.name)
self.assertEquals(resp.json['items'][0]['loser_score'], "2")
request = {"name":player_two.name}
resp = testapp.post_json(api_call, request)
self.assertEqual(len(resp.json['items']), 1)
self.assertEquals(resp.json['items'][0]['winner'], player_one.name)
self.assertEquals(resp.json['items'][0]['winner_score'], "3")
self.assertEquals(resp.json['items'][0]['loser'], player_two.name)
self.assertEquals(resp.json['items'][0]['loser_score'], "2")
示例11: test_get_user_rankings
# 需要導入模塊: import endpoints [as 別名]
# 或者: from endpoints import api_server [as 別名]
def test_get_user_rankings(self):
"""Test that you are able to retrieve a list of all users ranked by win percentage"""
api_call = '/_ah/spi/UserApi.get_user_rankings'
app = endpoints.api_server([UserApi], restricted=False)
testapp = webtest.TestApp(app)
user = User(name=u'no win', email=u'[email protected]')
user.put()
userone = User(name=u'one win', email=u'[email protected]', total_played=1, wins=1)
userone.put()
usertwo = User(name=u'two wins', email=u'[email protected]', total_played=2, wins=1)
usertwo.put()
#the expected request object as a dictionary, to be serialised to JSON by webtest
resp = testapp.post_json(api_call)
self.assertEqual(len(resp.json['items']), 2)
self.assertEquals(resp.json['items'][1]['name'], usertwo.name)
self.assertEquals(resp.json['items'][1]['email'], "[email protected]")
self.assertEquals(resp.json['items'][1]['wins'], "1")
self.assertEquals(resp.json['items'][1]['total_played'], "2")
self.assertEquals(resp.json['items'][1]['win_percentage'], 0.5)
self.assertEquals(resp.json['items'][0]['name'], userone.name)
self.assertEquals(resp.json['items'][0]['email'], "[email protected]")
self.assertEquals(resp.json['items'][0]['wins'], "1")
self.assertEquals(resp.json['items'][0]['total_played'], "1")
self.assertEquals(resp.json['items'][0]['win_percentage'], 1)
示例12: reset_data
# 需要導入模塊: import endpoints [as 別名]
# 或者: from endpoints import api_server [as 別名]
def reset_data(self, request):
global AIRPORTS
AIRPORTS = copy.deepcopy(SOURCE_AIRPORTS)
return message_types.VoidMessage()
# [END echo_api]
# [START api_server]
示例13: api_server
# 需要導入模塊: import endpoints [as 別名]
# 或者: from endpoints import api_server [as 別名]
def api_server(api_services, **kwargs):
"""Create an api_server.
The primary function of this method is to set up the WSGIApplication
instance for the service handlers described by the services passed in.
Additionally, it registers each API in ApiConfigRegistry for later use
in the BackendService.getApiConfigs() (API config enumeration service).
It also configures service control.
Args:
api_services: List of protorpc.remote.Service classes implementing the API
or a list of _ApiDecorator instances that decorate the service classes
for an API.
**kwargs: Passed through to protorpc.wsgi.service.service_handlers except:
protocols - ProtoRPC protocols are not supported, and are disallowed.
Returns:
A new WSGIApplication that serves the API backend and config registry.
Raises:
TypeError: if protocols are configured (this feature is not supported).
"""
# Disallow protocol configuration for now, Lily is json-only.
if 'protocols' in kwargs:
raise TypeError("__init__() got an unexpected keyword argument 'protocols'")
# Construct the api serving app
apis_app = _ApiServer(api_services, **kwargs)
dispatcher = endpoints_dispatcher.EndpointsDispatcherMiddleware(apis_app)
# Determine the service name
service_name = os.environ.get('ENDPOINTS_SERVICE_NAME')
if not service_name:
_logger.warn('Did not specify the ENDPOINTS_SERVICE_NAME environment'
' variable so service control is disabled. Please specify'
' the name of service in ENDPOINTS_SERVICE_NAME to enable'
' it.')
return dispatcher
# If we're using a local server, just return the dispatcher now to bypass
# control client.
if control_wsgi.running_on_devserver():
_logger.warn('Running on local devserver, so service control is disabled.')
return dispatcher
# The DEFAULT 'config' should be tuned so that it's always OK for python
# App Engine workloads. The config can be adjusted, but that's probably
# unnecessary on App Engine.
controller = control_client.Loaders.DEFAULT.load(service_name)
# Start the GAE background thread that powers the control client's cache.
control_client.use_gae_thread()
controller.start()
return control_wsgi.add_all(
dispatcher,
app_identity.get_application_id(),
controller)