mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-02-14 10:30:51 +03:00
Do more refactoring for ghnav-flask
This commit is contained in:
parent
e8e878e623
commit
00fb04f905
|
@ -1,8 +1,9 @@
|
||||||
"""Application module."""
|
"""Application module."""
|
||||||
|
|
||||||
from dependency_injector import containers, providers
|
from dependency_injector import containers, providers
|
||||||
|
import github
|
||||||
|
|
||||||
from . import github, views, webapp
|
from . import services, views, webapp
|
||||||
|
|
||||||
|
|
||||||
class Application(containers.DeclarativeContainer):
|
class Application(containers.DeclarativeContainer):
|
||||||
|
@ -11,14 +12,19 @@ class Application(containers.DeclarativeContainer):
|
||||||
config = providers.Configuration()
|
config = providers.Configuration()
|
||||||
|
|
||||||
github_client = providers.Factory(
|
github_client = providers.Factory(
|
||||||
github.GitHubApiClient,
|
github.Github,
|
||||||
auth_token=config.github.auth_token,
|
login_or_token=config.github.auth_token,
|
||||||
request_timeout=config.github.request_timeout,
|
timeout=config.github.request_timeout,
|
||||||
|
)
|
||||||
|
|
||||||
|
search_service = providers.Factory(
|
||||||
|
services.SearchService,
|
||||||
|
github_client=github_client,
|
||||||
)
|
)
|
||||||
|
|
||||||
index_view = providers.Callable(
|
index_view = providers.Callable(
|
||||||
views.index,
|
views.index,
|
||||||
github_client=github_client,
|
search_service=search_service,
|
||||||
default_search_term=config.search.default_term,
|
default_search_term=config.search.default_term,
|
||||||
default_search_limit=config.search.default_limit,
|
default_search_limit=config.search.default_limit,
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,67 +0,0 @@
|
||||||
"""Github API module."""
|
|
||||||
|
|
||||||
import requests
|
|
||||||
|
|
||||||
|
|
||||||
class GitHubApiClient:
|
|
||||||
"""GitHub API client performs operations with Github API."""
|
|
||||||
|
|
||||||
API_URL = 'https://api.github.com/'
|
|
||||||
DEFAULT_LIMIT = 5
|
|
||||||
|
|
||||||
def __init__(self, auth_token, request_timeout):
|
|
||||||
"""Initialize search."""
|
|
||||||
self._auth_token = auth_token
|
|
||||||
self._request_timeout = request_timeout
|
|
||||||
|
|
||||||
def search_repositories(self, search_term, limit):
|
|
||||||
"""Search repositories."""
|
|
||||||
if not search_term:
|
|
||||||
return []
|
|
||||||
|
|
||||||
repositories = self._make_search('repositories', search_term, limit)
|
|
||||||
latest_commits = [
|
|
||||||
self._get_latest_commit(repository, search_term)
|
|
||||||
for repository in repositories
|
|
||||||
]
|
|
||||||
return list(zip(repositories, latest_commits))
|
|
||||||
|
|
||||||
def _make_search(self, entity, search_term, limit):
|
|
||||||
headers = {}
|
|
||||||
if self._auth_token:
|
|
||||||
headers['authorization'] = f'token {self._auth_token}'
|
|
||||||
|
|
||||||
response = requests.get(
|
|
||||||
url=f'{self.API_URL}search/{entity}',
|
|
||||||
params={
|
|
||||||
'q': f'{search_term} in:name',
|
|
||||||
'sort': 'updated',
|
|
||||||
'order': 'desc',
|
|
||||||
'page': 1,
|
|
||||||
'per_page': limit,
|
|
||||||
},
|
|
||||||
headers=headers,
|
|
||||||
timeout=self._request_timeout,
|
|
||||||
)
|
|
||||||
data = response.json()
|
|
||||||
return data['items']
|
|
||||||
|
|
||||||
def _get_latest_commit(self, repository, search_term):
|
|
||||||
headers = {}
|
|
||||||
if self._auth_token:
|
|
||||||
headers['authorization'] = f'token {self._auth_token}'
|
|
||||||
|
|
||||||
response = requests.get(
|
|
||||||
url=repository['commits_url'].replace('{/sha}', ''),
|
|
||||||
params={
|
|
||||||
'q': f'{search_term} in:name',
|
|
||||||
'sort': 'updated',
|
|
||||||
'order': 'desc',
|
|
||||||
'page': 1,
|
|
||||||
'per_page': 1,
|
|
||||||
},
|
|
||||||
headers=headers,
|
|
||||||
timeout=self._request_timeout,
|
|
||||||
)
|
|
||||||
data = response.json()
|
|
||||||
return data[0]
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
"""Services module."""
|
||||||
|
|
||||||
|
from github import Github
|
||||||
|
from github.Repository import Repository
|
||||||
|
from github.Commit import Commit
|
||||||
|
|
||||||
|
|
||||||
|
class SearchService:
|
||||||
|
"""Search service performs search on Github."""
|
||||||
|
|
||||||
|
def __init__(self, github_client: Github):
|
||||||
|
self._github_client = github_client
|
||||||
|
|
||||||
|
def search_repositories(self, term, limit):
|
||||||
|
"""Search for repositories and return formatted data."""
|
||||||
|
repositories = self._github_client.search_repositories(term, **{'in': 'name'})
|
||||||
|
return [
|
||||||
|
self._format_repo(repository)
|
||||||
|
for repository in repositories[:limit]
|
||||||
|
]
|
||||||
|
|
||||||
|
def _format_repo(self, repository: Repository):
|
||||||
|
return {
|
||||||
|
'url': repository.html_url,
|
||||||
|
'name': repository.name,
|
||||||
|
'owner': {
|
||||||
|
'login': repository.owner.login,
|
||||||
|
'url': repository.owner.html_url,
|
||||||
|
'avatar_url': repository.owner.avatar_url,
|
||||||
|
},
|
||||||
|
'created_at': repository.created_at,
|
||||||
|
'latest_commit': self._format_commit(repository.get_commits()[0]),
|
||||||
|
}
|
||||||
|
|
||||||
|
def _format_commit(self, commit: Commit):
|
||||||
|
return {
|
||||||
|
'sha': commit.sha,
|
||||||
|
'url': commit.html_url,
|
||||||
|
'message': commit.commit.message,
|
||||||
|
'author_name': commit.commit.author.name,
|
||||||
|
}
|
|
@ -16,23 +16,23 @@
|
||||||
{% if repositories|length == 0 %}
|
{% if repositories|length == 0 %}
|
||||||
<small>No search results</small>
|
<small>No search results</small>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% for repository, latest_commit in repositories %} {{n}}
|
{% for repository in repositories %} {{n}}
|
||||||
<p>
|
<p>
|
||||||
<small>Search result # {{ loop.index }} from {{ repositories|length }}</small>
|
<small>Search result # {{ loop.index }} from {{ repositories|length }}</small>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Repository: <a href="{{ repository['html_url'] }}">{{ repository['name'] }}</a>
|
Repository: <a href="{{ repository.url }}">{{ repository.name }}</a>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Repository owner:
|
Repository owner:
|
||||||
<a href="{{ repository['owner']['html_url'] }}"><img src="{{ repository['owner']['avatar_url'] }}" alt="avatar" height="24" width="24"/></a>
|
<a href="{{ repository.owner.url }}"><img src="{{ repository.owner.avatar_url }}" alt="avatar" height="24" width="24"/></a>
|
||||||
<a href="{{ repository['owner']['html_url'] }}">{{ repository['owner']['login'] }}</a>
|
<a href="{{ repository.owner.url }}">{{ repository.owner.login }}</a>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Created at: {{ repository['created_at'] }}
|
Created at: {{ repository.created_at }}
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
LastCommit: <a href="{{ latest_commit['html_url'] }}">{{ latest_commit['sha'] }}</a> {{ latest_commit['commit']['message'] }} {{ latest_commit['commit']['author']['name'] }}
|
LastCommit: <a href="{{ repository.latest_commit.url }}">{{ repository.latest_commit.sha }}</a> {{ repository.latest_commit['message'] }} {{ repository.latest_commit.author_name }}
|
||||||
</p>
|
</p>
|
||||||
<hr/>
|
<hr/>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
|
@ -2,14 +2,14 @@
|
||||||
|
|
||||||
from flask import request, render_template
|
from flask import request, render_template
|
||||||
|
|
||||||
from .github import GitHubApiClient
|
from .services import SearchService
|
||||||
|
|
||||||
|
|
||||||
def index(github_client: GitHubApiClient, default_search_term, default_search_limit):
|
def index(search_service: SearchService, default_search_term, default_search_limit):
|
||||||
search_term = request.args.get('search_term', default_search_term)
|
search_term = request.args.get('search_term', default_search_term)
|
||||||
limit = request.args.get('limit', default_search_limit)
|
limit = request.args.get('limit', default_search_limit, int)
|
||||||
|
|
||||||
repositories = github_client.search_repositories(search_term, limit)
|
repositories = search_service.search_repositories(search_term, limit)
|
||||||
|
|
||||||
return render_template(
|
return render_template(
|
||||||
'index.html',
|
'index.html',
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
dependency-injector
|
dependency-injector
|
||||||
flask
|
flask
|
||||||
requests
|
pygithub
|
||||||
|
|
Loading…
Reference in New Issue
Block a user