-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharticles.py
68 lines (62 loc) · 1.54 KB
/
articles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from content_api.util import named_args
ARTICLES = [
{
'title': 'Why are so many corona patients struggling with lingering symptoms?'
},
{
'title': 'Lionel Messi labels Barcelona a "weak" team following shock defeat'
}
]
PREMIUM_ARTICLES = [
{
'title': 'Many people don\'t know their blood type. Here\'s how to find out yours'
}
]
json_schema = {
'type': 'object',
'properties': {
'title': {'type': 'string'}
},
'additionalProperties': False,
'required': ['title']
}
@named_args
def list_articles(query, headers, **request):
def is_match(article):
return 'q' not in query or query['q'].lower() in article['title'].lower()
articles = ARTICLES
if headers.get('Authorization') == 'secret':
articles = articles + PREMIUM_ARTICLES
data = [a for a in articles if is_match(a)]
return {'body': {'data': data}}
@named_args
def create_articles(body, **request):
ARTICLES.append(body)
return {'body': body}
routes = [
{
'method': 'GET',
'path': '/v1/articles',
'handler': list_articles,
'parameters': [
{
'in': 'query',
'name': 'q',
'description': 'Search query to filter by',
'schema': {'type': 'string', 'minLength': 2}
},
{
'in': 'header',
'name': 'Authorization',
'description': 'Auth key for premium access',
'schema': {'type': 'string', 'minLength': 6, 'maxLength': 6}
}
]
},
{
'method': 'POST',
'path': '/v1/articles',
'handler': create_articles,
'request_schema': json_schema
}
]