# Path: api/github/client.py
# class GitHubClientError(Exception):
#     def __init__(self, body, response):
#         self.body = body
#         self.response = response
# 
# Path: api/github/process.py
# class RepoProcessor:
# 
#     def __init__(self, client: GitHubClient):
#         self.client = client
# 
#     async def get_comparison_since_last_release(self, username: str, repo: str):
#         tags_json = await self.client.repo_tags(username, repo)
#         if not tags_json:
#             return None
#         latest_tag_sha = tags_json[0]['commit']['sha']
# 
#         comparison_json = await self.client.compare(
#             username,
#             repo,
#             base=latest_tag_sha,
#             head='HEAD'
#         )
#         comparison_json['latest_tag'] = tags_json[0]['name']
#         return comparison_json


from aiohttp_utils import Response
from .github.client import GitHubClientError
from .github.process import RepoProcessor

async def index(request):
    return Response({
        'message': 'Welcome to the sir API',
        'links': {
            'should_i_release': request.app.router['should_i_release'].url(
                parts=dict(username=':username', repo=':repo')
            ),
        }
    })


async def should_i_release(request):
    client = request.app['github_client']
    processor = RepoProcessor(client)
    username = request.match_info['username']
    repo = request.match_info['repo']
    try:
        comparison_json = await processor.get_comparison_since_last_release(username, repo)
    except GitHubClientError as err:
        return Response(err.body, status=err.response.status)

    ahead_by = comparison_json['ahead_by']
    return Response({
        'username': username,

# Path: api/github/client.py
# class GitHubClientError(Exception):
#     def __init__(self, body, response):
#         self.body = body
#         self.response = response
# 
# Path: api/github/process.py
# class RepoProcessor:
# 
#     def __init__(self, client: GitHubClient):
#         self.client = client
# 
#     async def get_comparison_since_last_release(self, username: str, repo: str):
#         tags_json = await self.client.repo_tags(username, repo)
#         if not tags_json:
#             return None
#         latest_tag_sha = tags_json[0]['commit']['sha']
# 
#         comparison_json = await self.client.compare(
#             username,
#             repo,
#             base=latest_tag_sha,
#             head='HEAD'
#         )
#         comparison_json['latest_tag'] = tags_json[0]['name']
#         return comparison_json


# Path: api/handlers.py
from aiohttp_utils import Response
from .github.client import GitHubClientError
from .github.process import RepoProcessor


async def index(request):
    return Response({
        'message': 'Welcome to the sir API',
        'links': {
            'should_i_release': request.app.router['should_i_release'].url(
                parts=dict(username=':username', repo=':repo')
            ),
        }
    })


async def should_i_release(request):
    client = request.app['github_client']
    processor = RepoProcessor(client)
    username = request.match_info['username']
    repo = request.match_info['repo']
    try:
        comparison_json = await processor.get_comparison_since_last_release(username, repo)
    except GitHubClientError as err:
        return Response(err.body, status=err.response.status)

    ahead_by = comparison_json['ahead_by']
    return Response({
        'username': username,

        'repo': repo,