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

+33
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

+2
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

+28
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

+20
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

+6
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

+21
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

+19
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

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
functions-framework
2+
google-cloud-firestore
3+
pytest

requirements.txt

+5
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

tests/test_report/libs/test_result.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import pytest
2+
from functions.report.libs.result import Result
3+
4+
def test_success():
5+
r = Result(status="success")
6+
assert r.success() == True
7+
assert r.failure() == False
8+
9+
def test_failure():
10+
r = Result(errors=["some error"])
11+
assert r.success() == False
12+
assert r.failure() == True
13+
14+
def test_default_status():
15+
r = Result()
16+
assert r.status == "ok"
17+
assert r.success() == True
18+
assert r.failure() == False
19+
20+
def test_custom_status():
21+
r = Result(status="custom")
22+
assert r.status == "custom"
23+
assert r.success() == True
24+
assert r.failure() == False
25+
26+
def test_result():
27+
r = Result(result="some result")
28+
assert r.result == "some result"
29+
30+
def test_errors():
31+
r = Result(errors=["some error"])
32+
assert r.errors == ["some error"]

tests/test_report/libs/test_utils.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from functions.report.libs.utils import *
2+
from functions.report.libs.result import Result
3+
import json
4+
5+
def test_output():
6+
# Create a mock result object with a successful status
7+
result_success = Result(status="success", result={"message": "Hello, world!"})
8+
9+
# Call the output function with the mock result object
10+
output_result_success = output(result_success)
11+
12+
# Verify that the output has the correct HTTP status code and payload
13+
assert output_result_success[0] == 200
14+
assert json.loads(output_result_success[1]) == {"message": "Hello, world!"}
15+
16+
# Create a mock result object with an error status
17+
result_error = Result(status="error", errors=["Invalid request"])
18+
19+
# Call the output function with the mock result object
20+
output_result_error = output(result_error)
21+
22+
# Verify that the output has the correct HTTP status code and payload
23+
assert output_result_error[0] == 400
24+
assert json.loads(output_result_error[1]) == ["Invalid request"]

0 commit comments

Comments
 (0)