-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import os | ||
import json | ||
from google.cloud import firestore | ||
from .result import Result | ||
from .utils import convert_to_array | ||
|
||
DB = firestore.Client(project=os.environ.get('PROJECT')) | ||
|
||
def list_data(params): | ||
ref = DB.collection(u'page_weight') | ||
|
||
query = ref | ||
print("params", params) | ||
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: | ||
params_array = convert_to_array(params['technology']) | ||
query = query.where('technology', 'in', params_array) | ||
if 'rank' in params: | ||
query = query.where('rank', '==', params['rank']) | ||
|
||
documents = query.stream() | ||
|
||
data = [] | ||
for doc in documents: | ||
data.append(doc.to_dict()) | ||
|
||
return Result(result=data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
class Result(): | ||
def __init__(self, status=None, result=None, errors=[]): | ||
self._status = status | ||
self.result = result | ||
self.errors = errors | ||
|
||
def success(self) -> bool: | ||
return not self.failure() | ||
|
||
def failure(self) -> bool: | ||
return len(self.errors) > 0 | ||
|
||
@property | ||
def status(self): | ||
if self._status != None: | ||
return self._status | ||
|
||
return "ok" if self.success else "error" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import json | ||
|
||
def output(result, headers={}): | ||
status = 200 if result.success() else 400 | ||
payload = result.result if result.success() else convert_to_hashes(result.errors) | ||
return (json.dumps(payload), status, headers) | ||
|
||
def convert_to_hashes(arr): | ||
hashes_arr = [] | ||
for inner_arr in arr: | ||
hash_dict = {inner_arr[0]: inner_arr[1]} | ||
hashes_arr.append(hash_dict) | ||
return hashes_arr | ||
|
||
def convert_to_array(data_string): | ||
list = data_string.split(',') | ||
return list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from .result import Result | ||
|
||
class Validator(): | ||
def __init__(self, params): | ||
self.params = params | ||
self.errors = [] | ||
self.normalizer_params = self.normalize(params) | ||
|
||
def validate(self): | ||
result = Result(status="ok", result="()") | ||
|
||
if 'geo' not in self.params: | ||
self.add_error("geo", "missing geo parameter") | ||
|
||
if 'technology' not in self.params: | ||
self.add_error("technology", "missing technology parameter") | ||
|
||
if 'rank' not in self.params: | ||
self.add_error("rank", "missing rank parameter") | ||
|
||
return Result(errors=self.errors, result=self.params) | ||
|
||
def add_error(self, key, error): | ||
self.errors.append([key, error]) | ||
|
||
def normalize(self, params): | ||
return "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import functions_framework | ||
from .libs.validator import Validator | ||
from .libs.utils import output | ||
from .libs.queries import list_data | ||
|
||
@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) | ||
|
||
# Set CORS headers for the main request | ||
headers = {"Access-Control-Allow-Origin": "*"} | ||
args = request.args.to_dict() | ||
|
||
validator = Validator(params=args) | ||
result = validator.validate() | ||
|
||
if result.failure(): | ||
print("error", result.errors) | ||
return output(result) | ||
|
||
response = list_data(result.result) | ||
|
||
return output(response, headers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
functions-framework | ||
google-cloud-firestore | ||
pytest |