From 413370a8184a12c3801e231616aabf266ffca688 Mon Sep 17 00:00:00 2001
From: David Trattnig <david.trattnig@o94.at>
Date: Thu, 25 Jun 2020 21:25:30 +0200
Subject: [PATCH] Extended API.

---
 src/rest/controllers/internal_controller.py | 21 ++++---
 src/rest/models/__init__.py                 |  3 +-
 src/rest/models/clock_info.py               |  2 +
 src/rest/models/inline_response400.py       | 62 +++++++++++++++++++++
 src/rest/models/play_log_entry.py           | 32 ++++++++++-
 src/rest/swagger/swagger.yaml               | 26 +++++++--
 src/rest/test/test_internal_controller.py   |  7 ++-
 7 files changed, 131 insertions(+), 22 deletions(-)
 create mode 100644 src/rest/models/inline_response400.py

diff --git a/src/rest/controllers/internal_controller.py b/src/rest/controllers/internal_controller.py
index c000739..7dcd695 100644
--- a/src/rest/controllers/internal_controller.py
+++ b/src/rest/controllers/internal_controller.py
@@ -3,6 +3,7 @@ import six
 
 from src.rest.models.clock_info import ClockInfo  # noqa: E501
 from src.rest.models.health_info import HealthInfo  # noqa: E501
+from src.rest.models.inline_response400 import InlineResponse400  # noqa: E501
 from src.rest.models.play_log_entry import PlayLogEntry  # noqa: E501
 from src.rest.models.status_entry import StatusEntry  # noqa: E501
 from src.rest import util
@@ -10,7 +11,6 @@ from src.rest import util
 from flask import current_app
 from dateutil.parser import parse
 
-
 def activate_engine(engine_number):  # noqa: E501
     """Set active engine
 
@@ -21,19 +21,23 @@ def activate_engine(engine_number):  # noqa: E501
 
     :rtype: None
     """
-    service = current_app.config['SERVICE']
+    return 'do some magic!'
 
 
-def add_playlog():  # noqa: E501
+def add_playlog(body):  # noqa: E501
     """Adds an entry to the playlog
 
     Stores the passed playlog entry  # noqa: E501
 
+    :param body: 
+    :type body: dict | bytes
 
-    :rtype: PlayLogEntry
+    :rtype: None
     """
+    if connexion.request.is_json:
+        body = PlayLogEntry.from_dict(connexion.request.get_json())  # noqa: E501
     service = current_app.config['SERVICE']
-    service.store_playlog()
+    service.store_playlog(body)
 
 
 def clock_info():  # noqa: E501
@@ -44,7 +48,6 @@ def clock_info():  # noqa: E501
 
     :rtype: ClockInfo
     """
-    service = current_app.config['SERVICE']
     return 'do some magic!'
 
 
@@ -56,7 +59,6 @@ def get_active_engine():  # noqa: E501
 
     :rtype: StatusEntry
     """
-    service = current_app.config['SERVICE']
     return 'do some magic!'
 
 
@@ -70,7 +72,6 @@ def get_engine_health(engine_number):  # noqa: E501
 
     :rtype: HealthInfo
     """
-    service = current_app.config['SERVICE']
     return 'do some magic!'
 
 
@@ -84,7 +85,6 @@ def get_report(year_month):  # noqa: E501
 
     :rtype: List[PlayLogEntry]
     """
-    service = current_app.config['SERVICE']
     return 'do some magic!'
 
 
@@ -94,7 +94,7 @@ def list_playlog(date_time, page=None, limit=None):  # noqa: E501
     Get paginated playlog entries for since the given timestamp.  # noqa: E501
 
     :param date_time: Get entries after this timestamp (e.g. &#x27;2020-08-29T09:12:33.001Z&#x27;)
-    :type date_time: dict | bytes
+    :type date_time: str
     :param page: The number of items to skip before starting to collect the result set
     :type page: int
     :param limit: The numbers of items to return per page
@@ -117,5 +117,4 @@ def log_engine_health(engine_number):  # noqa: E501
 
     :rtype: None
     """
-    service = current_app.config['SERVICE']
     return 'do some magic!'
diff --git a/src/rest/models/__init__.py b/src/rest/models/__init__.py
index 379af5f..dde7e80 100644
--- a/src/rest/models/__init__.py
+++ b/src/rest/models/__init__.py
@@ -5,7 +5,8 @@ from __future__ import absolute_import
 # import models into model package
 from src.rest.models.clock_info import ClockInfo
 from src.rest.models.health_info import HealthInfo
+from src.rest.models.inline_response400 import InlineResponse400
 from src.rest.models.play_log_entry import PlayLogEntry
 from src.rest.models.playlist_entry import PlaylistEntry
-from src.rest.models.timeslot import Timeslot
 from src.rest.models.status_entry import StatusEntry
+from src.rest.models.timeslot import Timeslot
diff --git a/src/rest/models/clock_info.py b/src/rest/models/clock_info.py
index 88d3c83..5475019 100644
--- a/src/rest/models/clock_info.py
+++ b/src/rest/models/clock_info.py
@@ -91,6 +91,8 @@ class ClockInfo(Model):
         :param current_timeslot: The current_timeslot of this ClockInfo.
         :type current_timeslot: Timeslot
         """
+        if current_timeslot is None:
+            raise ValueError("Invalid value for `current_timeslot`, must not be `None`")  # noqa: E501
 
         self._current_timeslot = current_timeslot
 
diff --git a/src/rest/models/inline_response400.py b/src/rest/models/inline_response400.py
new file mode 100644
index 0000000..8428ccd
--- /dev/null
+++ b/src/rest/models/inline_response400.py
@@ -0,0 +1,62 @@
+# coding: utf-8
+
+from __future__ import absolute_import
+from datetime import date, datetime  # noqa: F401
+
+from typing import List, Dict  # noqa: F401
+
+from src.rest.models.base_model_ import Model
+from src.rest import util
+
+
+class InlineResponse400(Model):
+    """NOTE: This class is auto generated by the swagger code generator program.
+
+    Do not edit the class manually.
+    """
+    def __init__(self, message=None):  # noqa: E501
+        """InlineResponse400 - a model defined in Swagger
+
+        :param message: The message of this InlineResponse400.  # noqa: E501
+        :type message: str
+        """
+        self.swagger_types = {
+            'message': str
+        }
+
+        self.attribute_map = {
+            'message': 'message'
+        }
+        self._message = message
+
+    @classmethod
+    def from_dict(cls, dikt):
+        """Returns the dict as a model
+
+        :param dikt: A dict.
+        :type: dict
+        :return: The inline_response_400 of this InlineResponse400.  # noqa: E501
+        :rtype: InlineResponse400
+        """
+        return util.deserialize_model(dikt, cls)
+
+    @property
+    def message(self):
+        """Gets the message of this InlineResponse400.
+
+
+        :return: The message of this InlineResponse400.
+        :rtype: str
+        """
+        return self._message
+
+    @message.setter
+    def message(self, message):
+        """Sets the message of this InlineResponse400.
+
+
+        :param message: The message of this InlineResponse400.
+        :type message: str
+        """
+
+        self._message = message
diff --git a/src/rest/models/play_log_entry.py b/src/rest/models/play_log_entry.py
index 79d0e7e..5cf3831 100644
--- a/src/rest/models/play_log_entry.py
+++ b/src/rest/models/play_log_entry.py
@@ -14,7 +14,7 @@ class PlayLogEntry(Model):
 
     Do not edit the class manually.
     """
-    def __init__(self, track_start=None, track_artist=None, track_album=None, track_title=None, track_duration=None, track_type=None, timeslot_id=None, show_name=None):  # noqa: E501
+    def __init__(self, track_start=None, track_artist=None, track_album=None, track_title=None, track_duration=None, track_type=None, timeslot_id=None, show_name=None, log_source=None):  # noqa: E501
         """PlayLogEntry - a model defined in Swagger
 
         :param track_start: The track_start of this PlayLogEntry.  # noqa: E501
@@ -33,6 +33,8 @@ class PlayLogEntry(Model):
         :type timeslot_id: int
         :param show_name: The show_name of this PlayLogEntry.  # noqa: E501
         :type show_name: str
+        :param log_source: The log_source of this PlayLogEntry.  # noqa: E501
+        :type log_source: int
         """
         self.swagger_types = {
             'track_start': datetime,
@@ -42,7 +44,8 @@ class PlayLogEntry(Model):
             'track_duration': int,
             'track_type': int,
             'timeslot_id': int,
-            'show_name': str
+            'show_name': str,
+            'log_source': int
         }
 
         self.attribute_map = {
@@ -53,7 +56,8 @@ class PlayLogEntry(Model):
             'track_duration': 'track_duration',
             'track_type': 'track_type',
             'timeslot_id': 'timeslot_id',
-            'show_name': 'show_name'
+            'show_name': 'show_name',
+            'log_source': 'log_source'
         }
         self._track_start = track_start
         self._track_artist = track_artist
@@ -63,6 +67,7 @@ class PlayLogEntry(Model):
         self._track_type = track_type
         self._timeslot_id = timeslot_id
         self._show_name = show_name
+        self._log_source = log_source
 
     @classmethod
     def from_dict(cls, dikt):
@@ -244,3 +249,24 @@ class PlayLogEntry(Model):
         """
 
         self._show_name = show_name
+
+    @property
+    def log_source(self):
+        """Gets the log_source of this PlayLogEntry.
+
+
+        :return: The log_source of this PlayLogEntry.
+        :rtype: int
+        """
+        return self._log_source
+
+    @log_source.setter
+    def log_source(self, log_source):
+        """Sets the log_source of this PlayLogEntry.
+
+
+        :param log_source: The log_source of this PlayLogEntry.
+        :type log_source: int
+        """
+
+        self._log_source = log_source
diff --git a/src/rest/swagger/swagger.yaml b/src/rest/swagger/swagger.yaml
index 656651d..ce882bf 100644
--- a/src/rest/swagger/swagger.yaml
+++ b/src/rest/swagger/swagger.yaml
@@ -113,7 +113,7 @@ paths:
         "400":
           description: bad input parameter
       x-openapi-router-controller: src.rest.controllers.internal_controller
-  /playlog/add:
+  /playlog/store:
     post:
       tags:
       - internal
@@ -121,15 +121,21 @@ paths:
       description: |
         Stores the passed playlog entry
       operationId: add_playlog
+      requestBody:
+        content:
+          application/json:
+            schema:
+              $ref: '#/components/schemas/PlayLogEntry'
+        required: true
       responses:
         "200":
-          description: Stored playlog
+          description: Successfully created a new playlog
+        "400":
+          description: Invalid request
           content:
             application/json:
               schema:
-                $ref: '#/components/schemas/PlayLogEntry'
-        "400":
-          description: bad input parameter
+                $ref: '#/components/schemas/inline_response_400'
       x-openapi-router-controller: src.rest.controllers.internal_controller
   /playlog/since/{date_time}:
     get:
@@ -339,11 +345,15 @@ components:
         show_name:
           type: string
           example: Electronic Music from Brazil
+        log_source:
+          type: integer
+          example: 1
       example:
         track_album: Bricolage
         track_start: 2020-08-29T09:12:33.001Z
         track_title: Chomp Samba
         show_name: Electronic Music from Brazil
+        log_source: 1
         track_artist: Amon Tobin
         timeslot_id: 1
         track_duration: 303
@@ -432,6 +442,7 @@ components:
           track_start: 2020-08-29T09:12:33.001Z
           track_title: Chomp Samba
           show_name: Electronic Music from Brazil
+          log_source: 1
           track_artist: Amon Tobin
           timeslot_id: 1
           track_duration: 303
@@ -516,4 +527,9 @@ components:
         album: XXYYXX
         title: Alone
         type: 1
+    inline_response_400:
+      type: object
+      properties:
+        message:
+          type: string
 
diff --git a/src/rest/test/test_internal_controller.py b/src/rest/test/test_internal_controller.py
index b90612d..4f18483 100644
--- a/src/rest/test/test_internal_controller.py
+++ b/src/rest/test/test_internal_controller.py
@@ -49,9 +49,12 @@ class TestInternalController(BaseTestCase):
 
         Adds an entry to the playlog
         """
+        body = PlayLogEntry()
         response = self.client.open(
-            '/api/v1/playlog/add',
-            method='POST')
+            '/playlog/store',
+            method='POST',
+            data=json.dumps(body),
+            content_type='application/json')
         self.assert200(response,
                        'Response body is : ' + response.data.decode('utf-8'))
 
-- 
GitLab