Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions mysite/api/urls.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from django.urls import include, path
from django.urls import include, path, re_path
from api import views
from api.views import CarsView
from rest_framework_swagger.views import get_swagger_view
from django.conf.urls import url

schema_view = get_swagger_view(title='Cars API')

urlpatterns = [
url(r'docs/', schema_view),
re_path(r'docs/', schema_view),
path('cars/get', views.cars_json),
path('cars/create', views.create_car),
path('view_cars/', CarsView.as_view()),
Expand Down
19 changes: 15 additions & 4 deletions mysite/elsys/processors/api_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@
COMMENTS_URL = "https://jsonplaceholder.typicode.com/posts/1/comments"
POSTS_URL = "https://jsonplaceholder.typicode.com/posts"


def sort_func(json):
try:
if 'title' in json:
return len(json['title'])
else:
return len(json['body'])
except KeyError:
return 0


class ApiProcessor:
@staticmethod
def longest_comment():
data = requests.get(COMMENTS_URL)
comments = sorted(data.json(), key=lambda d: d['body'], reverse=True)
comments = sorted(data.json(), key=sort_func, reverse=True)
return comments[0]

@staticmethod
def post_with_longest_title():
data = requests.get(POSTS_URL)
posts = sorted(data.json(), key=lambda d: d['title'], reverse=True)
return posts[0]
posts = sorted(data.json(), key=sort_func, reverse=True)
return posts[0]