Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: Tests

on: pull_request
on:
push:
branches:
- main
pull_request:

jobs:
build:
Expand All @@ -27,3 +31,8 @@ jobs:
- name: Run Tox
# Run tox using the version of Python in `PATH`
run: tox -e py
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<img src="https://github.com/nitrictech/python-sdk/raw/main/docs/assets/dot-matrix-logo-python.png" alt="Nitric Python SDK Logo"/>
</p>

![test status](https://github.com/nitrictech/python-sdk/actions/workflows/test.yaml/badge.svg?branch=main)
[![codecov](https://codecov.io/gh/nitrictech/python-sdk/branch/main/graph/badge.svg?token=SBFRNSZ4ZF)](https://codecov.io/gh/nitrictech/python-sdk)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=nitrictech_python-sdk&metric=alert_status)](https://sonarcloud.io/dashboard?id=nitrictech_python-sdk)

# Nitric Python SDK

The Python SDK supports the use of the cloud-portable [Nitric](https://nitric.io) framework with Python 3.7+.
Expand Down
10 changes: 9 additions & 1 deletion nitric/api/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def query(
)

def sub_collection_depth(self) -> int:
"""Return the depth of this collection, which is a count of the parents above this collection."""
if not self.is_sub_collection():
return 0
else:
Expand Down Expand Up @@ -271,14 +272,17 @@ class Document:

@property
def id(self):
"""Return the document's unique id."""
return self._ref.id

@property
def collection(self) -> CollectionRef:
"""Return the CollectionRef for the collection that contains this document."""
return self._ref.parent

@property
def ref(self):
"""Return the DocumentRef for this document."""
return self._ref


Expand All @@ -289,6 +293,10 @@ class QueryResultsPage:
paging_token: any = field(default_factory=lambda: None)
documents: List[Document] = field(default_factory=lambda: [])

def has_more_pages(self) -> bool:
"""Return false if the page token is None or empty (both represent no more pages)."""
return bool(self.paging_token)


class QueryBuilder:
"""Document query builder for retrieving documents from a collection based on filters."""
Expand Down Expand Up @@ -416,7 +424,7 @@ async def fetch(self) -> QueryResultsPage:
)

return QueryResultsPage(
paging_token=results.paging_token,
paging_token=results.paging_token if results.paging_token else None,
documents=[_document_from_wire(documents=self._documents, message=result) for result in results.documents],
)

Expand Down
10 changes: 7 additions & 3 deletions nitric/faas/faas.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@

async def _register_faas_worker(
func: Callable[
[Trigger], Union[Coroutine[Any, Any, Union[Response, None, dict, bytes]], Union[Response, None, dict, bytes]]
[Trigger],
Union[
Coroutine[Any, Any, Union[Response, None, dict, list, set, bytes]],
Union[Response, None, dict, list, set, bytes],
],
]
):
"""
Expand Down Expand Up @@ -74,7 +78,7 @@ async def _register_faas_worker(
if isinstance(response, bytes):
full_response.data = response
# convert dict responses to JSON
elif isinstance(response, dict):
elif isinstance(response, (dict, list, set)):
full_response.data = bytes(json.dumps(response), "utf-8")
if full_response.context.is_http():
full_response.context.as_http().headers["Content-Type"] = "application/json"
Expand Down Expand Up @@ -106,7 +110,7 @@ async def _register_faas_worker(
channel.close()


def start(handler: Callable[[Trigger], Coroutine[Any, Any, Union[Response, None, dict, bytes]]]):
def start(handler: Callable[[Trigger], Coroutine[Any, Any, Union[Response, None, dict, list, set, bytes]]]):
"""
Register the provided function as the trigger handler and starts handling new trigger requests.

Expand Down
7 changes: 7 additions & 0 deletions nitric/faas/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ class Response(object):
data: Union[bytes, str, None, Any] = field(default=None)

def data_to_bytes(self) -> bytes:
"""
Return the data from this response. If not already bytes, the data will be converted.

None returns an empty byte array.
str will be converted directly to bytes
All other types are converted to JSON using json.dumps, then to bytes from the JSON string.
"""
if self.data is None:
return bytes()
elif isinstance(self.data, bytes):
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ envlist = linter,py3.8
deps =
-e .[dev]
commands =
pytest .
pytest --cov=./nitric --cov-report=xml

[testenv:linter]
deps =
Expand Down