Skip to content

Commit

Permalink
Merge pull request #17 from HTTPArchive/development
Browse files Browse the repository at this point in the history
New version
  • Loading branch information
maceto authored Feb 10, 2024
2 parents ce134ea + 8ea3b44 commit 85fe498
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 31 deletions.
28 changes: 28 additions & 0 deletions functions/adoption/libs/network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

"""
Network
Handles formatting responses to match the tuple pattern required by
the flask/GCP wrapper for Cloud Functions.
"""

PREFLIGHT_HEADERS = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Max-Age": "3600",
}

HEADERS = {"Access-Control-Allow-Origin": "*", "Content-Type": "application/json"}

def respond_cors():
"""
To be used to return OPTIONS responses to satisfy CORS preflight requests.
"""
return ("", 204, PREFLIGHT_HEADERS)

def respond(data, status=200):
"""
To be used to return responses to satisfy CORS requests.
"""
return (data, status, HEADERS)
15 changes: 2 additions & 13 deletions functions/adoption/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,13 @@
from .libs.validator import Validator
from .libs.utils import output
from .libs.queries import list_data
from .libs.network import respond_cors, respond

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

# Set CORS headers for the preflight request
if request.method == "OPTIONS":
# Allows GET requests from any origin with the Content-Type
# header and caches preflight response for an 3600s
headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Max-Age": "3600",
}

return ("", 204, headers)
return respond_cors()

# Set CORS headers for the main request
headers = {"Access-Control-Allow-Origin": "*"}
Expand Down
8 changes: 7 additions & 1 deletion functions/categories/libs/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ def list_data(params):
category_array = convert_to_array(params['category'])

for category in category_array:
results = DB.collection(u'categories').where("category", "==", category).stream()
results = query.where("category", "==", category).stream()
for doc in results:
data.append(doc.to_dict())

else:
documents = query.stream()

for doc in documents:
data.append(doc.to_dict())

return Result(result=data)
6 changes: 3 additions & 3 deletions functions/categories/libs/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ def __init__(self, params):
def validate(self):
result = Result(status="ok", result="()")

if 'onlyname' not in self.params:
if 'category' not in self.params:
self.add_error("category", "missing category parameter")
# if 'onlyname' not in self.params:
# if 'category' not in self.params:
# self.add_error("category", "missing category parameter")

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

Expand Down
12 changes: 12 additions & 0 deletions functions/technologies/libs/presenters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

class Presenters:
@staticmethod
def technology(item):
return {
'client': item['client'],
'similar_technologies': item['similar_technologies'],
'description': item['description'],
'origins': item['origins'],
'technology': item['technology'],
'category': item['category']
}
42 changes: 30 additions & 12 deletions functions/technologies/libs/queries.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,53 @@
import os
import json
from google.cloud import firestore
from google.cloud.firestore_v1.base_query import FieldFilter, Or

from .result import Result
from .utils import convert_to_array
from .presenters import Presenters


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

def list_data(params):
ref = DB.collection(u'technologies')
onlyname = False
ref = DB.collection('technologies')

query = ref

if 'start' in params:
query = query.where('date', '>=', params['start'])
if 'end' in params:
query = query.where('date', '<=', params['end'])
if 'geo' in params:
query = query.where('geo', '==', params['geo'])
if 'technology' in params:
arfilters = []
params_array = convert_to_array(params['technology'])
query = query.where('technology', 'in', params_array)
if 'rank' in params:
query = query.where('rank', '==', params['rank'])
for tech in params_array:
arfilters.append(FieldFilter('technology', '==', tech))

or_filter = Or(filters=arfilters)

query = query.where(filter=or_filter)

if 'category' in params:
params_array = convert_to_array(params['category'])
query = query.where('category', 'in', params_array)
query = query.where(filter=FieldFilter('category_obj', 'array_contains_any', params_array))

if 'onlyname' in params:
onlyname = True

if 'sort' not in params:
query = query.order_by('technology', direction=firestore.Query.ASCENDING)
else:
if params['sort'] == 'origins':
query = query.order_by('origins', direction=firestore.Query.DESCENDING)


documents = query.stream()

data = []
for doc in documents:
data.append(doc.to_dict())
item = doc.to_dict()
if onlyname:
data.append(item['technology'])
else:
data.append(Presenters.technology(doc.to_dict()))

return Result(result=data)
4 changes: 2 additions & 2 deletions functions/technologies/libs/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def __init__(self, params):
def validate(self):
result = Result(status="ok", result="()")

if 'technology' not in self.params:
self.add_error("technology", "missing technology parameter")
# if 'technology' not in self.params:
# self.add_error("technology", "missing technology parameter")

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

Expand Down

0 comments on commit 85fe498

Please sign in to comment.