# Path: services/consuming_services_apis/consuming_services_apis/api/blog_api.py
# @view_config(route_name='blog_post/post', renderer='pretty_json', request_method='GET')
# def blog_post(request):
#     print("Processing {} request from {} for the HTTP service: {}, ua: {}".format(
#         request.method, get_ip(request), request.url, request.user_agent
#     ))
# 
#     data = build_dict(request)
#     post_id = data.get('post_id')
# 
#     post = MemoryDb.get_post(post_id, get_ip(request))
#     if not post:
#         return Response('{"error":"Post with ID not found: ' + post_id + '"}', status=404)
# 
#     return post
# 
# Path: services/consuming_services_apis/consuming_services_apis/api/blog_api.py
# @view_config(route_name='blog_posts/', renderer='pretty_json')
# @view_config(route_name='blog_posts', renderer='pretty_json')
# def blog_posts(request):
#     print("Processing {} request from {} for the HTTP service: {}, ua: {}".format(
#         request.method, get_ip(request), request.url, request.user_agent
#     ))
#     ip = get_ip(request)
#     posts = MemoryDb.get_posts(ip)
# 
#     return posts
# 
# Path: services/consuming_services_apis/consuming_services_apis/api/blog_api.py
# @view_config(route_name='blog_posts', renderer='pretty_json', request_method='POST')
# @view_config(route_name='blog_posts/', renderer='pretty_json', request_method='POST')
# def blog_post_create(request):
#     print("Processing {} request from {} for the HTTP service: {}, ua: {}".format(
#         request.method, get_ip(request), request.url, request.user_agent
#     ))
# 
#     all_posts = MemoryDb.get_posts(get_ip(request))
#     if len(all_posts) > POST_LIMIT_PER_USER:
#         MemoryDb.clear_posts(get_ip(request))
#         return Response('{"error":"Too many posts created. Your blog has reset to the three default posts. '
#                         'You may now add more"}', status=400)
# 
#     try:
#         post_data = request.json_body
#     except Exception as x:
#         return Response('{"error":"Bad request ' + str(x) + '"}', status=400)
# 
#     post = Post(
#         post_data.get('title'),
#         post_data.get('content'),
#         try_int(post_data.get('view_count', 0), 0),
#         try_date(post_data.get('published'), datetime.date.today().isoformat())
#     )
#     trim_post_size(post)
# 
#     MemoryDb.add_post(post, get_ip(request))
#     request.response.status_code = 201
# 
#     return post
# 
# Path: services/consuming_services_apis/consuming_services_apis/api/blog_api.py
# @view_config(route_name='blog_post/post', renderer='pretty_json', request_method='PUT')
# def update_blog_post(request):
#     print("Processing {} request from {} for the HTTP service: {}, ua: {}".format(
#         request.method, get_ip(request), request.url, request.user_agent
#     ))
# 
#     data = build_dict(request)
#     post_id = data.get('post_id')
# 
#     if MemoryDb.is_post_read_only(post_id):
#         return Response('{"error":"The post with id ' + str(post_id) +
#                         ' is read-only. '
#                         'You can only edit posts that you have created yourself via this API."}',
#                         status=403)
# 
#     post = MemoryDb.get_post(post_id, get_ip(request))
#     if not post:
#         return Response('{"error":"Post with ID not found: ' + post_id + '"}', status=404)
# 
#     try:
#         post_data = request.json_body
#     except Exception as x:
#         return Response('{"error":"Bad request {0}"}'.format(x), status=400)
# 
#     post.title = post_data.get('title', post.title)
#     post.content = post_data.get('content', post.content)
#     post.view_count = try_int(post_data.get('view_count', post.view_count), post.view_count)
#     post.published = try_date(post_data.get('published', post.published), datetime.date.today().isoformat())
#     trim_post_size(post)
# 
#     request.response.status_code = 204
# 
#     return post
# 
# Path: services/consuming_services_apis/consuming_services_apis/api/blog_api.py
# @view_config(route_name='blog_post/post', renderer='pretty_json', request_method='DELETE')
# def delete_blog_post(request):
#     print("Processing {} request from {} for the HTTP service: {}, ua: {}".format(
#         request.method, get_ip(request), request.url, request.user_agent
#     ))
# 
#     data = build_dict(request)
#     post_id = data.get('post_id')
# 
#     if MemoryDb.is_post_read_only(post_id):
#         return Response('{"error":"The post with id ' + str(post_id) +
#                         ' is read-only. '
#                         'You can only delete posts that you have created yourself via this API."}',
#                         status=403)
# 
#     post = MemoryDb.get_post(post_id, get_ip(request))
#     if not post:
#         return Response('{"error":"Post with ID not found: ' + post_id + '"}', status=404)
# 
#     MemoryDb.delete_post(post, get_ip(request))
#     request.response.status_code = 202
# 
#     return {'deleted': post_id}


from pyramid.view import view_config
from pyramid.response import Response
from .blog_api import blog_post as raw_blog_post
from .blog_api import blog_posts as raw_blog_posts
from .blog_api import blog_post_create as raw_blog_post_create
from .blog_api import update_blog_post as raw_update_blog_post
from .blog_api import delete_blog_post as raw_delete_blog_post
from consuming_services_apis.infrastructure import auth_service




################################################################################
# GET /api/restricted/blog
#
@view_config(route_name='restricted_blog_posts/', renderer='pretty_json')
@view_config(route_name='restricted_blog_posts', renderer='pretty_json')
def blog_posts(request):
    error_response = validate_auth_and_generate_failed_response(request)
    if error_response:

# Path: services/consuming_services_apis/consuming_services_apis/api/blog_api.py
# @view_config(route_name='blog_post/post', renderer='pretty_json', request_method='GET')
# def blog_post(request):
#     print("Processing {} request from {} for the HTTP service: {}, ua: {}".format(
#         request.method, get_ip(request), request.url, request.user_agent
#     ))
# 
#     data = build_dict(request)
#     post_id = data.get('post_id')
# 
#     post = MemoryDb.get_post(post_id, get_ip(request))
#     if not post:
#         return Response('{"error":"Post with ID not found: ' + post_id + '"}', status=404)
# 
#     return post
# 
# Path: services/consuming_services_apis/consuming_services_apis/api/blog_api.py
# @view_config(route_name='blog_posts/', renderer='pretty_json')
# @view_config(route_name='blog_posts', renderer='pretty_json')
# def blog_posts(request):
#     print("Processing {} request from {} for the HTTP service: {}, ua: {}".format(
#         request.method, get_ip(request), request.url, request.user_agent
#     ))
#     ip = get_ip(request)
#     posts = MemoryDb.get_posts(ip)
# 
#     return posts
# 
# Path: services/consuming_services_apis/consuming_services_apis/api/blog_api.py
# @view_config(route_name='blog_posts', renderer='pretty_json', request_method='POST')
# @view_config(route_name='blog_posts/', renderer='pretty_json', request_method='POST')
# def blog_post_create(request):
#     print("Processing {} request from {} for the HTTP service: {}, ua: {}".format(
#         request.method, get_ip(request), request.url, request.user_agent
#     ))
# 
#     all_posts = MemoryDb.get_posts(get_ip(request))
#     if len(all_posts) > POST_LIMIT_PER_USER:
#         MemoryDb.clear_posts(get_ip(request))
#         return Response('{"error":"Too many posts created. Your blog has reset to the three default posts. '
#                         'You may now add more"}', status=400)
# 
#     try:
#         post_data = request.json_body
#     except Exception as x:
#         return Response('{"error":"Bad request ' + str(x) + '"}', status=400)
# 
#     post = Post(
#         post_data.get('title'),
#         post_data.get('content'),
#         try_int(post_data.get('view_count', 0), 0),
#         try_date(post_data.get('published'), datetime.date.today().isoformat())
#     )
#     trim_post_size(post)
# 
#     MemoryDb.add_post(post, get_ip(request))
#     request.response.status_code = 201
# 
#     return post
# 
# Path: services/consuming_services_apis/consuming_services_apis/api/blog_api.py
# @view_config(route_name='blog_post/post', renderer='pretty_json', request_method='PUT')
# def update_blog_post(request):
#     print("Processing {} request from {} for the HTTP service: {}, ua: {}".format(
#         request.method, get_ip(request), request.url, request.user_agent
#     ))
# 
#     data = build_dict(request)
#     post_id = data.get('post_id')
# 
#     if MemoryDb.is_post_read_only(post_id):
#         return Response('{"error":"The post with id ' + str(post_id) +
#                         ' is read-only. '
#                         'You can only edit posts that you have created yourself via this API."}',
#                         status=403)
# 
#     post = MemoryDb.get_post(post_id, get_ip(request))
#     if not post:
#         return Response('{"error":"Post with ID not found: ' + post_id + '"}', status=404)
# 
#     try:
#         post_data = request.json_body
#     except Exception as x:
#         return Response('{"error":"Bad request {0}"}'.format(x), status=400)
# 
#     post.title = post_data.get('title', post.title)
#     post.content = post_data.get('content', post.content)
#     post.view_count = try_int(post_data.get('view_count', post.view_count), post.view_count)
#     post.published = try_date(post_data.get('published', post.published), datetime.date.today().isoformat())
#     trim_post_size(post)
# 
#     request.response.status_code = 204
# 
#     return post
# 
# Path: services/consuming_services_apis/consuming_services_apis/api/blog_api.py
# @view_config(route_name='blog_post/post', renderer='pretty_json', request_method='DELETE')
# def delete_blog_post(request):
#     print("Processing {} request from {} for the HTTP service: {}, ua: {}".format(
#         request.method, get_ip(request), request.url, request.user_agent
#     ))
# 
#     data = build_dict(request)
#     post_id = data.get('post_id')
# 
#     if MemoryDb.is_post_read_only(post_id):
#         return Response('{"error":"The post with id ' + str(post_id) +
#                         ' is read-only. '
#                         'You can only delete posts that you have created yourself via this API."}',
#                         status=403)
# 
#     post = MemoryDb.get_post(post_id, get_ip(request))
#     if not post:
#         return Response('{"error":"Post with ID not found: ' + post_id + '"}', status=404)
# 
#     MemoryDb.delete_post(post, get_ip(request))
#     request.response.status_code = 202
# 
#     return {'deleted': post_id}


# Path: services/consuming_services_apis/consuming_services_apis/api/restricted_blog_api.py
from pyramid.view import view_config
from pyramid.response import Response
from .blog_api import blog_post as raw_blog_post
from .blog_api import blog_posts as raw_blog_posts
from .blog_api import blog_post_create as raw_blog_post_create
from .blog_api import update_blog_post as raw_update_blog_post
from .blog_api import delete_blog_post as raw_delete_blog_post
from consuming_services_apis.infrastructure import auth_service





################################################################################
# GET /api/restricted/blog
#
@view_config(route_name='restricted_blog_posts/', renderer='pretty_json')
@view_config(route_name='restricted_blog_posts', renderer='pretty_json')
def blog_posts(request):
    error_response = validate_auth_and_generate_failed_response(request)
    if error_response:

        return error_response