Skip to content

Commit

Permalink
Replace DRF with connexion
Browse files Browse the repository at this point in the history
`connexion` is a framework that automagically handles HTTP requests
based on OpenAPI Specification. The routes and the documentation are
autogenerated. Models are being validated against the specification.

* Remove all django files
* Update requirements
* Create function to generate and configure a connexion app
* Implement a way to manage the settings similar to what was done for
  DRF
* Fix the unit tests
* Fix integration tests
* Update the docker image
* Fix the wheel package
* Update and clean up Makefile
* Update the GitHub PR template

Fixes request-yo-racks#60
  • Loading branch information
rgreinho committed Nov 11, 2018
1 parent e9a3f18 commit ea389e3
Show file tree
Hide file tree
Showing 52 changed files with 263 additions and 507 deletions.
11 changes: 2 additions & 9 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
[run]
omit =
*__init__.py
api/apps/api/models.py
api/apps/api/serializers.py
api/apps/api/tests/*
api/apps/api/urls.py
api/apps/api/views.py
api/celery/celery.py
api/celery/celery_settings.py
api/celery/tests/*
api/connexion_*
api/controller/*
api/settings/*
api/static.py
api/urls.py
api/wsgi.py


[report]
fail_under = 90.0
7 changes: 3 additions & 4 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ Add a screenshot if applicable.
-->


<!--
Motivation and Context
----------------------
<!-- Why is this change required? What problem does it solve? -->
Why is this change required? What problem does it solve? -->


How Has This Been Tested?
-------------------------
<!--
How Has This Been Tested?
Add any information that could help the reviewer to validate the PR.
Please describe in detail how you tested your changes, include details
of your testing environment, and the tests you ran to see how your
Expand Down
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ MAINTAINER Rémy Greinhofer <remy.greinhofer@requestyoracks.org>
# Update the package list.
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
git \
git \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

Expand All @@ -15,7 +15,8 @@ WORKDIR /usr/src/app
COPY . .

# Build the packages.
RUN python setup.py bdist_wheel
RUN rm -fr dist \
&& python setup.py bdist_wheel

###
# Create the release image.
Expand Down
30 changes: 4 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ else
RUN_CMD = $(LOCAL_RUN_CMD)
endif

# Docker run Django parameters.
DJANGO_MANAGE_CMD = python manage.py
RUN_DJANGO_MANAGE_CMD = $(RUN_CMD) $(DJANGO_MANAGE_CMD)

default: setup

help: # Display help
Expand Down Expand Up @@ -81,24 +77,6 @@ clean-minikube: ## Remove all the Kubernetes objects associated to this project
clean-repo: ## Remove unwanted files in project (!DESTRUCTIVE!)
@cd $(TOPDIR) && git clean -ffdx && git reset --hard

.PHONY: django-migrate
django-migrate: ## Run the Django migrations
@bash tools/kubernetes-django-manage.sh migrate

.PHONY: django-make-migrations
django-make-migrations: ## Prepare the Django migrations
@bash tools/kubernetes-django-manage.sh makemigrations

.PHONY: django-shell
django-shell: ## Run the Django Shell
@bash tools/kubernetes-django-manage.sh shell

.PHONY: django-superuser
django-superuser: ## Create the Django super user
@bash tools/kubernetes-django-manage.sh createsuperuser \
--username admin\
--email admin@requestyoracks.com

.PHONY: deploy-minikube-api
deploy-minikube-api: ## Deploy the API on Minikube
cd charts \
Expand Down Expand Up @@ -159,10 +137,10 @@ local-celery-worker: ## Start a local celery worker
&& eval $$(tools/kubernetes-local-env-vars.sh) \
&& $(LOCAL_RUN_CMD) docker/docker-entrypoint.sh celery worker

.PHONY: local-django-api
local-django-api: ## Run Django locally
.PHONY: local-api
local-api: ## Run connexion locally
source $(HOME)/.config/ryr/ryr-env.sh \
&& export DJANGO_SETTINGS_MODULE=api.settings.local \
&& export CONNEXION_SETTINGS_MODULE=api.settings.local \
&& export RYR_API_API_OPTS="--reload --timeout 1800" \
&& export RYR_LOG_LEVEL=info \
&& eval $$(tools/kubernetes-local-env-vars.sh) \
Expand All @@ -176,7 +154,7 @@ venv: venv/bin/activate ## Setup local venv
venv/bin/activate: requirements.txt
test -d venv || python3 -m venv venv || virtualenv --no-setuptools --no-wheel -p python3 venv
. venv/bin/activate \
&& pip install --upgrade pip \
&& pip install --upgrade pip setuptools \
&& pip install -U -r requirements.txt \
&& pip install -r requirements-dev.txt \
&& pip install -e .
Expand Down
1 change: 0 additions & 1 deletion api/apps/api/models.py

This file was deleted.

1 change: 0 additions & 1 deletion api/apps/api/serializers.py

This file was deleted.

14 changes: 0 additions & 14 deletions api/apps/api/urls.py

This file was deleted.

63 changes: 0 additions & 63 deletions api/apps/api/views.py

This file was deleted.

1 change: 0 additions & 1 deletion api/assets/css/styles.css

This file was deleted.

1 change: 0 additions & 1 deletion api/assets/js/scripts.js

This file was deleted.

9 changes: 6 additions & 3 deletions api/celery/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from celery import chord
from celery.utils.log import get_task_logger

from api.apps.api.collectors.base import BusinessInfo
from api.apps.api.collectors.generic import CollectorClient
from api.collectors.base import BusinessInfo
from api.collectors.generic import CollectorClient
from api.celery.worker import app

logger = get_task_logger(__name__)
Expand Down Expand Up @@ -41,8 +41,11 @@ def collect_place_details_from_yelp(name, address):
client.search_places(address, terms=name, limit=1)
place_summary = client.retrieve_search_summary(0)

if not place_summary:
raise ValueError('Yelp did not return any result.')

# Extract the place_id.
details = client.get_place_details(place_summary.id)
details = client.get_place_details(place_summary.place_id)
return details


Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion api/apps/api/collectors/base.py → api/collectors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def from_json(cls, json_obj):
class PlaceSearchSummary:
"""Define Place Search Summary information."""

id: str = ''
place_id: str = ''
name: str = ''
address: str = ''

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Defines a generic client for the collectors."""

from api.apps.api.collectors.google import GoogleCollector
from api.apps.api.collectors.yelp import YelpCollector
from api.collectors.google import GoogleCollector
from api.collectors.yelp import YelpCollector


class CollectorClient:
Expand Down Expand Up @@ -106,7 +106,7 @@ def lookup_place(self, place_id=None, name=None, address=None):
limit=1,
)
search_summary = self.retrieve_search_summary(0)
lookup_id = search_summary.id
lookup_id = search_summary.place_id
else:
lookup_id = place_id
place_details = self.get_place_details(lookup_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import googlemaps

from api.apps.api.collectors.base import AbstractClientCollector
from api.apps.api.collectors.base import BusinessInfo
from api.apps.api.collectors.base import PlaceSearchSummary
from api.collectors.base import AbstractClientCollector
from api.collectors.base import BusinessInfo
from api.collectors.base import PlaceSearchSummary


class GoogleCollector(AbstractClientCollector):
Expand Down Expand Up @@ -94,7 +94,7 @@ def retrieve_search_summary(self, index=0):
search_summary = PlaceSearchSummary()
business = self.search_results.get('results')[index]

search_summary.id = business.get('place_id', '')
search_summary.place_id = business.get('place_id', '')
search_summary.name = business.get('name', '')
search_summary.address = business.get('vicinity', '')

Expand Down
8 changes: 4 additions & 4 deletions api/apps/api/collectors/yelp.py → api/collectors/yelp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

import requests

from api.apps.api.collectors.base import AbstractRestCollector
from api.apps.api.collectors.base import BusinessInfo
from api.apps.api.collectors.base import PlaceSearchSummary
from api.collectors.base import AbstractRestCollector
from api.collectors.base import BusinessInfo
from api.collectors.base import PlaceSearchSummary


class YelpCollector(AbstractRestCollector):
Expand Down Expand Up @@ -114,7 +114,7 @@ def retrieve_search_summary(self, index=0):
search_summary = PlaceSearchSummary()
business = self.search_results.get('businesses')[index]

search_summary.id = business.get('id', '')
search_summary.place_id = business.get('id', '')
search_summary.name = business.get('name', '')
search_summary.address = ' '.join(business.get('location', {}).get('display_address', ''))

Expand Down
12 changes: 12 additions & 0 deletions api/connexion_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
Define the REST API server.
This was done to help transitionning to `connexion` and may be deleted at some point in the future.
To launch the server locally, use the `make local-api` command instead.
"""

from api.connexion_utils import create_connexion_app

if __name__ == '__main__':
app = create_connexion_app()
app.run()
Loading

0 comments on commit ea389e3

Please sign in to comment.