Skip to content

Commit 85fe498

Browse files
authored
Merge pull request #17 from HTTPArchive/development
New version
2 parents ce134ea + 8ea3b44 commit 85fe498

File tree

7 files changed

+84
-31
lines changed

7 files changed

+84
-31
lines changed

functions/adoption/libs/network.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
"""
3+
Network
4+
5+
Handles formatting responses to match the tuple pattern required by
6+
the flask/GCP wrapper for Cloud Functions.
7+
"""
8+
9+
PREFLIGHT_HEADERS = {
10+
"Access-Control-Allow-Origin": "*",
11+
"Access-Control-Allow-Methods": "GET",
12+
"Access-Control-Allow-Headers": "Content-Type",
13+
"Access-Control-Max-Age": "3600",
14+
}
15+
16+
HEADERS = {"Access-Control-Allow-Origin": "*", "Content-Type": "application/json"}
17+
18+
def respond_cors():
19+
"""
20+
To be used to return OPTIONS responses to satisfy CORS preflight requests.
21+
"""
22+
return ("", 204, PREFLIGHT_HEADERS)
23+
24+
def respond(data, status=200):
25+
"""
26+
To be used to return responses to satisfy CORS requests.
27+
"""
28+
return (data, status, HEADERS)

functions/adoption/main.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,13 @@
22
from .libs.validator import Validator
33
from .libs.utils import output
44
from .libs.queries import list_data
5+
from .libs.network import respond_cors, respond
56

67
@functions_framework.http
78
def dispatcher(request):
8-
# For more information about CORS and CORS preflight requests, see:
9-
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
109

11-
# Set CORS headers for the preflight request
1210
if request.method == "OPTIONS":
13-
# Allows GET requests from any origin with the Content-Type
14-
# header and caches preflight response for an 3600s
15-
headers = {
16-
"Access-Control-Allow-Origin": "*",
17-
"Access-Control-Allow-Methods": "GET",
18-
"Access-Control-Allow-Headers": "Content-Type",
19-
"Access-Control-Max-Age": "3600",
20-
}
21-
22-
return ("", 204, headers)
11+
return respond_cors()
2312

2413
# Set CORS headers for the main request
2514
headers = {"Access-Control-Allow-Origin": "*"}

functions/categories/libs/queries.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,14 @@ def list_data(params):
2727
category_array = convert_to_array(params['category'])
2828

2929
for category in category_array:
30-
results = DB.collection(u'categories').where("category", "==", category).stream()
30+
results = query.where("category", "==", category).stream()
3131
for doc in results:
3232
data.append(doc.to_dict())
3333

34+
else:
35+
documents = query.stream()
36+
37+
for doc in documents:
38+
data.append(doc.to_dict())
39+
3440
return Result(result=data)

functions/categories/libs/validator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ def __init__(self, params):
99
def validate(self):
1010
result = Result(status="ok", result="()")
1111

12-
if 'onlyname' not in self.params:
13-
if 'category' not in self.params:
14-
self.add_error("category", "missing category parameter")
12+
# if 'onlyname' not in self.params:
13+
# if 'category' not in self.params:
14+
# self.add_error("category", "missing category parameter")
1515

1616
return Result(errors=self.errors, result=self.params)
1717

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
class Presenters:
3+
@staticmethod
4+
def technology(item):
5+
return {
6+
'client': item['client'],
7+
'similar_technologies': item['similar_technologies'],
8+
'description': item['description'],
9+
'origins': item['origins'],
10+
'technology': item['technology'],
11+
'category': item['category']
12+
}
Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,53 @@
11
import os
22
import json
33
from google.cloud import firestore
4+
from google.cloud.firestore_v1.base_query import FieldFilter, Or
5+
46
from .result import Result
57
from .utils import convert_to_array
8+
from .presenters import Presenters
9+
610

711
DB = firestore.Client(project=os.environ.get('PROJECT'), database=os.environ.get('DATABASE'))
812

913
def list_data(params):
10-
ref = DB.collection(u'technologies')
14+
onlyname = False
15+
ref = DB.collection('technologies')
1116

1217
query = ref
1318

14-
if 'start' in params:
15-
query = query.where('date', '>=', params['start'])
16-
if 'end' in params:
17-
query = query.where('date', '<=', params['end'])
18-
if 'geo' in params:
19-
query = query.where('geo', '==', params['geo'])
2019
if 'technology' in params:
20+
arfilters = []
2121
params_array = convert_to_array(params['technology'])
22-
query = query.where('technology', 'in', params_array)
23-
if 'rank' in params:
24-
query = query.where('rank', '==', params['rank'])
22+
for tech in params_array:
23+
arfilters.append(FieldFilter('technology', '==', tech))
24+
25+
or_filter = Or(filters=arfilters)
26+
27+
query = query.where(filter=or_filter)
28+
2529
if 'category' in params:
2630
params_array = convert_to_array(params['category'])
27-
query = query.where('category', 'in', params_array)
31+
query = query.where(filter=FieldFilter('category_obj', 'array_contains_any', params_array))
32+
33+
if 'onlyname' in params:
34+
onlyname = True
35+
36+
if 'sort' not in params:
37+
query = query.order_by('technology', direction=firestore.Query.ASCENDING)
38+
else:
39+
if params['sort'] == 'origins':
40+
query = query.order_by('origins', direction=firestore.Query.DESCENDING)
41+
2842

2943
documents = query.stream()
3044

3145
data = []
3246
for doc in documents:
33-
data.append(doc.to_dict())
47+
item = doc.to_dict()
48+
if onlyname:
49+
data.append(item['technology'])
50+
else:
51+
data.append(Presenters.technology(doc.to_dict()))
3452

3553
return Result(result=data)

functions/technologies/libs/validator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ def __init__(self, params):
99
def validate(self):
1010
result = Result(status="ok", result="()")
1111

12-
if 'technology' not in self.params:
13-
self.add_error("technology", "missing technology parameter")
12+
# if 'technology' not in self.params:
13+
# self.add_error("technology", "missing technology parameter")
1414

1515
return Result(errors=self.errors, result=self.params)
1616

0 commit comments

Comments
 (0)