def get(self): """Handle get request.""" if not self.request.get('code'): # User denied OAuth2 permissions. logging.info('User denied OAuth2 permissions') return self.redirect('/login/error') credentials = _FLOW.step2_exchange(self.request.get('code')) if credentials: # store user's credentials in database user_ndb = InitUser(credentials.refresh_token) # check if user has any networks api_handler = APIHandler(_CLIENT_ID, _CLIENT_SECRET, user_ndb, _APPLICATION_NAME) networks = api_handler.GetAllNetworks() if not networks: # if user has no networks, redirect to ask if one should be made return self.redirect('/make-test-network') return self.redirect('/') else: # failure: no credentials logging.error('No credentials found from step 2 in flow') return self.redirect('/login/error')
import asyncio import logging from aiohttp import web from api_handler import APIHandler logging.basicConfig(level=logging.INFO) _logger = logging.getLogger('asyncuatoy') if __name__ == '__main__': logging.basicConfig(level=logging.INFO) handler = APIHandler() app = web.Application() app.add_routes([ web.post('/connector', handler.add_connector), web.post('/remove_connector', handler.remove_connector) ]) app.on_shutdown.append(handler.shutdown) web.run_app(app)
def handle_stream(self, stream, address): client = APIHandler(self, stream, address) self.clients.add(client) yield client.on_connect()
def get(self, method): """Delegate GET request calls to the DFP API.""" method = method.lower() user_ndb = InitUser() api_handler = APIHandler(_CLIENT_ID, _CLIENT_SECRET, user_ndb, _APPLICATION_NAME) network_code = self.request.get('network_code') # parse parameters try: limit = int(self.request.get('limit', api_handler.page_limit)) except ValueError: self.response.status = 400 return self.response.write('Limit must be an integer') try: offset = int(self.request.get('offset', 0)) except ValueError: self.response.status = 400 return self.response.write('Offset must be an integer') if method == 'networks': return_obj = api_handler.GetAllNetworks() else: # construct PQL statement where_clause = self.request.get('where', '') statement = ad_manager.FilterStatement(where_clause, limit=limit, offset=offset) try: # throws KeyError if method not found api_handler_func = getattr(api_handler, self.api_handler_method_map[method]) except KeyError: self.response.status = 400 self.response.write('API method not supported (%s).' % method) # retrieve return_obj from api_handler and modify it return_obj = api_handler_func(network_code, statement) # process return_obj if 'columns' in return_obj: # special case: return_obj is from PQL Service cols = return_obj['columns'] return_obj['results'] = [ unpack_row(row, cols) for row in return_obj['results'] ] else: # regular case return_obj['results'] = [ serialize_object(obj) for obj in return_obj['results'] ] if self.request.get('limit'): return_obj['limit'] = limit else: try: return_obj['limit'] = return_obj['totalResultSetSize'] except KeyError: return_obj['limit'] = api_handler.page_limit return_obj['offset'] = offset # construct response headers self.response.headers['Content-Type'] = 'application/json' self.response.write(json.dumps(return_obj))
"""Script starts api server""" from argparse import ArgumentParser from wsgiref.simple_server import make_server from api_handler import APIHandler if __name__ == '__main__': parser = ArgumentParser(description='Starts VM manager API server') parser.add_argument('-host', default='localhost') parser.add_argument('-port', type=int, default=8080) args = parser.parse_args() httpd = make_server(args.host, args.port, APIHandler()) print 'Started on http://%s:%s' % (args.host, args.port) try: httpd.serve_forever() except KeyboardInterrupt: httpd.shutdown() exit()