Skip to content

Commit 663bc36

Browse files
committed
first endpoint report
1 parent e50d76f commit 663bc36

File tree

12 files changed

+193
-0
lines changed

12 files changed

+193
-0
lines changed

.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# IDES
2+
.vscode
3+
4+
# Terraform
5+
.terraform
6+
*.tfstate
7+
*.tfstate.*
8+
*.terraform.lock.hcl
9+
secret.tfvars
10+
override.tf
11+
override.tf.json
12+
*_override.tf
13+
*_override.tf.json
14+
*tfplan*
15+
.terraformrc
16+
terraform.rc
17+
*.tfvars
18+
*.local.tfvars
19+
20+
/terraform
21+
22+
# env
23+
.envrc
24+
25+
# CLI
26+
.DS_Store
27+
28+
# asdf
29+
/.tool-versions
30+
31+
# python
32+
__pycache__
33+
.pytest_cache

conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import pytest
2+
from functions import *

functions/report/libs/__init__.py

Whitespace-only changes.

functions/report/libs/queries.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import os
2+
from google.cloud import firestore
3+
from .result import Result
4+
5+
DB = firestore.Client(project=os.environ.get('PROJECT'))
6+
7+
def list_data(params):
8+
ref = DB.collection(u'reports')
9+
10+
query = ref
11+
12+
if 'start' in params:
13+
query = query.where('date', '>=', params['start'])
14+
if 'end' in params:
15+
query = query.where('date', '<', params['end'])
16+
if 'category' in params:
17+
query = query.where('category', '==', params['category'])
18+
if 'geo' in params:
19+
query = query.where('geo', '==', params['geo'])
20+
21+
documents = query.stream()
22+
23+
data = {}
24+
25+
for doc in documents:
26+
data[doc.id] = doc.to_dict()
27+
28+
return Result(result=data)

functions/report/libs/result.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
class Result():
3+
def __init__(self, status=None, result=None, errors=[]):
4+
self._status = status
5+
self.result = result
6+
self.errors = errors
7+
8+
def success(self) -> bool:
9+
return not self.failure()
10+
11+
def failure(self) -> bool:
12+
return len(self.errors) > 0
13+
14+
@property
15+
def status(self):
16+
if self._status != None:
17+
return self._status
18+
19+
return "ok" if self.success else "error"
20+

functions/report/libs/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import json
2+
3+
def output(result):
4+
status = 200 if result.success() else 400
5+
payload = result.result if result.success() else result.errors
6+
return (json.dumps(payload), status)

functions/report/libs/validator.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from .result import Result
2+
3+
class Validator():
4+
def __init__(self, params):
5+
self.params = params
6+
self.errors = []
7+
self.normalizer_params = self.normalize(params)
8+
9+
def validate(self):
10+
result = Result(status="ok", result="()")
11+
12+
if 'geo' not in self.params:
13+
self.add_error("geo", "missing geo parameters")
14+
15+
return Result(errors=self.errors, result=self.params)
16+
17+
def add_error(self, key, error):
18+
self.errors.append([key, error])
19+
20+
def normalize(self, params):
21+
return ""

functions/report/main.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import functions_framework
2+
from .libs.validator import Validator
3+
from .libs.utils import output
4+
from .libs.queries import list_data
5+
6+
@functions_framework.http
7+
def dispatcher(request):
8+
args = request.args.to_dict()
9+
10+
validator = Validator(params=args)
11+
result = validator.validate()
12+
13+
if result.failure():
14+
print("error", result.errors)
15+
return output(result)
16+
17+
response = list_data(result.result)
18+
19+
return output(response)

functions/report/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
functions-framework
2+
google-cloud-firestore
3+
pytest

requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
functions-framework
2+
google-cloud-firestore
3+
google-cloud-error-reporting
4+
mock-firestore
5+
pytest

0 commit comments

Comments
 (0)