def execute(self, request): response = Response() try: def backup_endpoint_posts(): print('backing up') # Obtain post data from endpoint get_data_request = GetEndpointDataRequest(request.endpoint) get_data_uc = GetEndpointDataUseCase() endpoint_data_response = get_data_uc.execute(get_data_request) # Backup obtained endpoint backup_data_request = BackupEndpointDataRequest(endpoint_data_response.data, timezone = request.timezone) backup_data_uc = BackupEndpointDataUseCase() backup_data_uc.execute(backup_data_request) # Start scheduler scheduler = BackgroundScheduler() scheduler.add_job(lambda: backup_endpoint_posts(), 'cron', hour = request.hour, minute = request.minute, second = request.second, timezone = pytz.timezone(request.timezone)) scheduler.start() return response except Exception as e: response.add_exception_error(e) return response
def execute(self, request_object): response = Response() try: users = self.user_repo.list_users(request_object.order) response.data = users return response except Exception as e: response.add_exception_error(e) return response
def make_response(self, request, context): # brute force approach where better approaches exist all_pages = { page.uri: page for page in DepthFirstTraverser(context.root_page).traverse() } page = all_pages.get(request.uri) if page: return Response(http_code="200", page=page) else: return Response(http_code="404", page=WikiPage("404"))
def execute(self, request): response = Response() try: for post in [p['data'] for p in request.endpoint_data['data']['children']]: user_id = self.user_repo.add_user(User(name = post['author'], ups = post['ups'], comments = post['num_comments'])) created_time = datetime.fromtimestamp(post['created_utc'], tz = pytz.timezone(request.timezone)) self.post_repo.add_post(Post(title = post['title'], ups = post['ups'], comments = post['num_comments'], created = created_time, author = user_id)) return response except Exception as e: response.add_exception_error(e) return response
def execute(self, request): response = Response() try: data_json = requests.get(request.endpoint, headers={ 'User-agent': 'winnin backend' }).json() response.data = data_json return response except Exception as e: response.add_exception_error(e) return response
def execute(self, request_object): response = Response() try: posts = self.post_repo.list_posts(request_object.order, request_object.start, request_object.end) response.data = posts return response except Exception as e: response.add_exception_error(e) return response
def __call__(self, environ, start_response): try: request = Request(environ) callback, args = routers.match(request.path) response = callback(request, *args) except NotFound: response = Response(f'<h1>Not found</h1>', status=404) start_response(response.status, response.headers.items()) return iter(response)
def make_response(self, request, context): self.request = request self.context = context results_page = WikiPage(self.title()) matching_pages = ( page for page in DepthFirstTraverser(context.root_page).traverse() if self.traverse(page)) results_page.text = "found term in pages:<ul>" for result_page in matching_pages: results_page.text += '<li>' + result_page.title + '</li>' results_page.text += "</ul>" return Response(page=results_page)
def hello(request, name): return Response(f'hello {name}')