diff --git a/examples/applications/ghnav-flask/config.yml b/examples/applications/ghnav-flask/config.yml new file mode 100644 index 00000000..4409662f --- /dev/null +++ b/examples/applications/ghnav-flask/config.yml @@ -0,0 +1,6 @@ +github: + auth_token: ${GITHUB_TOKEN} + request_timeout: 10 +search: + default_term: "Dependency Injector" + default_limit: 5 diff --git a/examples/applications/ghnav-flask/githubnavigator/__init__.py b/examples/applications/ghnav-flask/githubnavigator/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/examples/applications/ghnav-flask/githubnavigator/application.py b/examples/applications/ghnav-flask/githubnavigator/application.py new file mode 100644 index 00000000..a29072e4 --- /dev/null +++ b/examples/applications/ghnav-flask/githubnavigator/application.py @@ -0,0 +1,32 @@ +"""Application module.""" + +from dependency_injector import containers, providers + +from . import github, views, webapp + + +class Application(containers.DeclarativeContainer): + """Application container.""" + + config = providers.Configuration() + + github_client = providers.Factory( + github.GitHubApiClient, + auth_token=config.github.auth_token, + request_timeout=config.github.request_timeout, + ) + + index_view = providers.Callable( + views.index, + github_client=github_client, + default_search_term=config.search.default_term, + default_search_limit=config.search.default_limit, + ) + + app = providers.Factory( + webapp.create_app, + name=__name__, + routes=[ + webapp.Route('/', 'index', index_view, methods=['GET']), + ], + ) diff --git a/examples/applications/ghnav-flask/githubnavigator/entrypoint.py b/examples/applications/ghnav-flask/githubnavigator/entrypoint.py new file mode 100644 index 00000000..fb545b90 --- /dev/null +++ b/examples/applications/ghnav-flask/githubnavigator/entrypoint.py @@ -0,0 +1,8 @@ +"""Entrypoint module.""" + +from .application import Application + + +application = Application() +application.config.from_yaml('config.yml') +app = application.app() diff --git a/examples/applications/ghnav-flask/githubnavigator/github.py b/examples/applications/ghnav-flask/githubnavigator/github.py new file mode 100644 index 00000000..18593347 --- /dev/null +++ b/examples/applications/ghnav-flask/githubnavigator/github.py @@ -0,0 +1,67 @@ +"""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] diff --git a/examples/applications/ghnav-flask/githubnavigator/templates/index.html b/examples/applications/ghnav-flask/githubnavigator/templates/index.html new file mode 100644 index 00000000..c7e878ba --- /dev/null +++ b/examples/applications/ghnav-flask/githubnavigator/templates/index.html @@ -0,0 +1,40 @@ + + +
++ Search result # {{ loop.index }} from {{ repositories|length }} +
++ Repository: {{ repository['name'] }} +
+
+ Repository owner:
+
+ {{ repository['owner']['login'] }}
+
+ Created at: {{ repository['created_at'] }} +
++ LastCommit: {{ latest_commit['sha'] }} {{ latest_commit['commit']['message'] }} {{ latest_commit['commit']['author']['name'] }} +
+