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')
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))