Skip to content

Commit

Permalink
feat: add api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
psincraian committed Apr 17, 2019
1 parent 432d132 commit b3cf4ee
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 2 deletions.
18 changes: 18 additions & 0 deletions pepy/infrastructure/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import json

from flask import Blueprint
from pepy.infrastructure import container
from pepy.infrastructure.api._transformer import transform_project, transform_project_item

api = Blueprint('api', __name__)

@api.route("/projects/<project_name>", methods=["GET"])
def project_action(project_name):
project = container.project_provider.find(project_name)
return json.dumps(transform_project(project))


@api.route("/projects", methods=["GET"])
def project_find():
projects = container.project_provider.for_home()
return json.dumps([transform_project_item(p) for p in projects])
18 changes: 18 additions & 0 deletions pepy/infrastructure/api/_transformer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from typing import Dict

from pepy.domain.read_model import ProjectProjection, ProjectListProjection


def transform_project(project: ProjectProjection) -> Dict:
return {
"id": project.name,
"total_downloads": project.total_downloads,
"downloads": {d.date.isoformat(): d.downloads for d in project.last_downloads}
}


def transform_project_item(project: ProjectListProjection) -> Dict:
return {
"id": project.name,
"total_downloads": project.total_downloads,
}
2 changes: 2 additions & 0 deletions pepy/infrastructure/web/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
from pepy.domain.exception import DomainException
from pepy.domain.model import ProjectName, Password
from pepy.infrastructure import container
from pepy.infrastructure.api import api
from pepy.infrastructure.web._form import SearchForm

app = Flask(__name__)
app.config.from_object(container.config)
app.register_blueprint(api, url_prefix='/api')


@app.route("/", methods=["GET", "POST"])
Expand Down
17 changes: 17 additions & 0 deletions tests/acceptance/api_find_projects.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Feature: show index page with some selected projects

Scenario: show the projects with most downloads from yesterday
Given the following projects exists
| name | downloads |
| pepy | 100 |
When I send the GET request to /api/projects
Then the response status code should be 200
And the api response should be
"""
[
{
"id": "pepy",
"total_downloads": 100
}
]
"""
22 changes: 22 additions & 0 deletions tests/acceptance/api_show_project.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Feature: show index page with some selected projects

Scenario: show the projects with most downloads from yesterday
Given the pepy project with the following downloads
| date | downloads |
| 2018-05-01 | 10 |
| 2018-05-02 | 15 |
| 2018-05-03 | 20 |
When I send the GET request to /api/projects/pepy
Then the response status code should be 200
And the api response should be
"""
{
"id": "pepy",
"total_downloads": 45,
"downloads": {
"2018-05-03": 20,
"2018-05-02": 15,
"2018-05-01": 10
}
}
"""
8 changes: 8 additions & 0 deletions tests/acceptance/steps/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@ def step_impl(context: Context):
result = re.sub(r"\s+", "", raw_result)
expect = re.sub(r"\s+", "", context.text)
assert expect in result, "Result is {}".format(raw_result)


@step("the api response should be")
def step_impl(context):
raw_result = context.response.data.decode()
result = re.sub(r"\s+", "", raw_result)
expect = re.sub(r"\s+", "", context.text)
assert expect == result, "Result is {}".format(raw_result)
6 changes: 4 additions & 2 deletions tests/acceptance/steps/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ def step_impl(context: Context):
@given("the {name} project with the following downloads")
def step_impl(context: Context, name: str):
project_name = ProjectName(name)
project = ProjectStub.create(project_name)
context.container.project_repository.save_projects([project])
downloads = []
total_downloads = 0
for row in context.table:
date = datetime.strptime(row["date"], "%Y-%m-%d").date()
total_downloads += int(row["downloads"])
downloads.append(ProjectDownloads(project_name, Downloads(row["downloads"]), date))
project = ProjectStub.create(project_name, Downloads(total_downloads))
context.container.project_repository.save_projects([project])
context.container.project_repository.save_day_downloads(downloads)


Expand Down

0 comments on commit b3cf4ee

Please sign in to comment.