diff --git a/functions/CreateDataset/__init__.py b/functions/CreateDataset/__init__.py new file mode 100644 index 0000000..43b4467 --- /dev/null +++ b/functions/CreateDataset/__init__.py @@ -0,0 +1,57 @@ +import logging + +import azure.functions as func + +from json import dumps as jsondump, loads as jsonload +from dataclasses import dataclass, asdict +from typing import Optional + + + +@dataclass(frozen=True) +class AiKompasMessage: + msg_type: int + obj_id: Optional[str] + + + @classmethod + def fromServiceBusMessage(cls, msg: func.ServiceBusMessage) -> 'AiKompasMessage': + s = msg.get_body().decode('utf-8') + + return AiKompasMessage(**jsonload(s)) + + + def toJson(self) -> str: + return jsondump(asdict(self)) + + +@dataclass(frozen=True) +class AiKompasDataset: + id: int + data: dict + + + +def main(req: func.HttpRequest, msg: func.Out[str], doc: func.Out[func.Document]) -> func.HttpResponse: + logging.info('[CreateDataset] Starting') + + data = req.params.get('data') + if not data: + try: + req_body = req.get_json() + except ValueError: + pass + else: + data = req_body.get('data') + + if data: + newdoc = func.Document.from_dict(dict(data=data)) + doc.set(newdoc) + s = AiKompasMessage(msg_type=1, obj_id=newdoc.get('id')) + msg.set(s.toJson()) + return func.HttpResponse(jsondump(data)) + else: + return func.HttpResponse( + "This HTTP triggered function executed successfully. Pass data in the query string or in the request body for a personalized response.", + status_code=200 + ) diff --git a/functions/CreateDataset/function.json b/functions/CreateDataset/function.json new file mode 100644 index 0000000..d29ca8e --- /dev/null +++ b/functions/CreateDataset/function.json @@ -0,0 +1,36 @@ +{ + "scriptFile": "__init__.py", + "bindings": [ + { + "authLevel": "function", + "type": "httpTrigger", + "direction": "in", + "name": "req", + "methods": [ + "get", + "post" + ] + }, + { + "type": "http", + "direction": "out", + "name": "$return" + }, + { + "name": "msg", + "type": "serviceBus", + "direction": "out", + "queueName": "tst", + "connection": "AzureWebJobsServiceBus" + }, + { + "type": "cosmosDB", + "direction": "out", + "name": "doc", + "databaseName": "aikompas", + "collectionName": "dataset", + "createIfNotExists": "true", + "connectionStringSetting": "AzureCosmosDBConnectionString" + } + ] +} diff --git a/functions/CreateDataset/sample.dat b/functions/CreateDataset/sample.dat new file mode 100644 index 0000000..2e60943 --- /dev/null +++ b/functions/CreateDataset/sample.dat @@ -0,0 +1,3 @@ +{ + "name": "Azure" +} \ No newline at end of file