Skip to content

Commit

Permalink
create stream endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
wkrzywiec committed Feb 21, 2025
1 parent 8042529 commit b312a55
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
2 changes: 1 addition & 1 deletion services/mankkoo/mankkoo/account/account_db.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from apiflask import Schema
from apiflask.fields import String, Boolean, Float, File
from apiflask.fields import String, Boolean, Float

import mankkoo.database as db

Expand Down
39 changes: 38 additions & 1 deletion services/mankkoo/mankkoo/controller/stream_controller.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
import uuid

from apiflask import APIBlueprint, Schema, abort
from apiflask.fields import String, Boolean
from apiflask.fields import String, Boolean, Mapping

import mankkoo.event_store as es

from mankkoo.stream import stream_db as database
from mankkoo.stream.stream_db import Event, Stream, StreamDetails
from mankkoo.base_logger import log

stream_endpoints = APIBlueprint('stream_endpoints', __name__, tag='Stream')

class StreamCreate(Schema):
type = String()
metadata = Mapping()

class StreamCreateResult(Schema):
id = String()

@stream_endpoints.route("", methods=['POST'])
@stream_endpoints.input(StreamCreate, location='json')
@stream_endpoints.output(StreamCreateResult, status_code=201)
@stream_endpoints.doc(summary='Create a new stream', description='Create a new stream')
def create_stream(body: StreamCreate):
log.info(f"Received request to create a new stream. Body: {body}...")

allowed_types = ['account', 'investment', 'real-estate', 'retirement', 'stocks']

if body is None or "type" not in body:
abort(400, message=f"Failed to create a new stream. Invalid request body was provided. The 'type' of a new stream must be provided.")

if body["type"] not in allowed_types:
abort(400, message=f"Failed to create a new stream. Invalid request body was provided. The 'type' must have one of values: {allowed_types}, but '{body['type']}' was provided.")

metadata = body["metadata"] if "metadata" in body else dict()
stream = es.Stream(uuid.uuid4(), body["type"], 0, metadata)
es.create([stream])

result = StreamCreateResult()
result.id = stream.id
return result


class StreamsQuery(Schema):
active = Boolean()
Expand Down Expand Up @@ -49,6 +83,9 @@ def stream_by_id(stream_id):
return stream


# update metadata
# update tags

@stream_endpoints.route("/<stream_id>/events")
@stream_endpoints.output(Event(many=True), status_code=200)
@stream_endpoints.doc(
Expand Down
12 changes: 12 additions & 0 deletions services/mankkoo/tests/controller/stream_controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@

import mankkoo.event_store as es

def test_stream_is_created__if_type_is_provided(test_client):
pass

def test_stream_is_created__if_type_and_metadata_are_provided(test_client):
pass

def test_stream_is_not_created__if_type_is_not_provided(test_client):
pass

def test_stream_is_not_created__if_invalid_type_is_provided(test_client):
pass

def test_all_streams_are_listed__if_no_filters_are_provided__and_stream_names_are_correctly_set(test_client):
# GIVEN
streams = [
Expand Down

0 comments on commit b312a55

Please sign in to comment.