Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
Piao committed Jul 1, 2024
1 parent cb019db commit adf0b64
Show file tree
Hide file tree
Showing 16 changed files with 808 additions and 0 deletions.
Empty file.
19 changes: 19 additions & 0 deletions client/template_web_client/openapi_server/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env python3

import connexion

from openapi_server import encoder


def main():
app = connexion.App(__name__, specification_dir='./openapi/')
app.app.json_encoder = encoder.JSONEncoder
app.add_api('openapi.yaml',
arguments={'title': 'Prediction Microservice'},
pythonic_params=True)

app.run(port=8080)


if __name__ == '__main__':
main()
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import connexion
from typing import Dict
from typing import Tuple
from typing import Union

from openapi_server.models.metric import Metric # noqa: E501
from openapi_server import util
from openapi_server.prediction_service import utils


def get_metric_by_id(metric_id): # noqa: E501
"""Retrieve historical and predicted time series by ID
Returns a time series metric # noqa: E501
:param metric_id: Id of the metric to return
:type metric_id: str
:rtype: Union[Metric, Tuple[Metric, int], Tuple[Metric, int, Dict[str, str]]
"""
return utils.query(metric_id)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from typing import List

19 changes: 19 additions & 0 deletions client/template_web_client/openapi_server/encoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from json import JSONEncoder

from openapi_server.models.base_model import Model


class JSONEncoder(JSONEncoder):
include_nulls = False

def default(self, o):
if isinstance(o, Model):
dikt = {}
for attr in o.openapi_types:
value = getattr(o, attr)
if value is None and not self.include_nulls:
continue
attr = o.attribute_map[attr]
dikt[attr] = value
return dikt
return JSONEncoder.default(self, o)
3 changes: 3 additions & 0 deletions client/template_web_client/openapi_server/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# flake8: noqa
# import models into model package
from openapi_server.models.metric import Metric
68 changes: 68 additions & 0 deletions client/template_web_client/openapi_server/models/base_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import pprint

import typing

from openapi_server import util

T = typing.TypeVar('T')


class Model:
# openapiTypes: The key is attribute name and the
# value is attribute type.
openapi_types: typing.Dict[str, type] = {}

# attributeMap: The key is attribute name and the
# value is json key in definition.
attribute_map: typing.Dict[str, str] = {}

@classmethod
def from_dict(cls: typing.Type[T], dikt) -> T:
"""Returns the dict as a model"""
return util.deserialize_model(dikt, cls)

def to_dict(self):
"""Returns the model properties as a dict
:rtype: dict
"""
result = {}

for attr in self.openapi_types:
value = getattr(self, attr)
if isinstance(value, list):
result[attr] = list(map(
lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
value
))
elif hasattr(value, "to_dict"):
result[attr] = value.to_dict()
elif isinstance(value, dict):
result[attr] = dict(map(
lambda item: (item[0], item[1].to_dict())
if hasattr(item[1], "to_dict") else item,
value.items()
))
else:
result[attr] = value

return result

def to_str(self):
"""Returns the string representation of the model
:rtype: str
"""
return pprint.pformat(self.to_dict())

def __repr__(self):
"""For `print` and `pprint`"""
return self.to_str()

def __eq__(self, other):
"""Returns true if both objects are equal"""
return self.__dict__ == other.__dict__

def __ne__(self, other):
"""Returns true if both objects are not equal"""
return not self == other
Loading

0 comments on commit adf0b64

Please sign in to comment.