Skip to content

Commit

Permalink
Update Python API client
Browse files Browse the repository at this point in the history
Add configurable SSLContext
Remove Unused model
  • Loading branch information
mj23000 committed Sep 20, 2024
1 parent 2ade123 commit aace15c
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 90 deletions.
2 changes: 1 addition & 1 deletion docs/mozart-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3866,7 +3866,7 @@ info:
- [Beosound Level](https://www.bang-olufsen.com/en/dk/speakers/beosound-level)\n\
- [Beosound Theatre](https://www.bang-olufsen.com/en/dk/soundbars/beosound-theatre)\n\
\n\nThis API documentation has been generated for version 3.4.1.8 of the Mozart\
\ platform and API generation version 3.4.1.8.7.\n\n## Python API\n\nThe Python\
\ platform and API generation version 3.4.1.8.8.\n\n## Python API\n\nThe Python\
\ package has been generated using the [OpenAPI Generator](https:/openapi-generator.tech/)\
\ version 7.8.0. On top of the generated API, a helper file, mozart_client.py,\
\ has been made that makes the API more pythonic. We recommend using this.\n\
Expand Down
2 changes: 1 addition & 1 deletion python_client/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 2.1
Name: mozart_api
Version: 3.4.1.8.5
Version: 3.4.1.8.8
Summary: Mozart platform API
Home-page: https://pypi.org/project/mozart-api/
License: MIT
Expand Down
2 changes: 1 addition & 1 deletion python_client/mozart_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
""" # noqa: E501


__version__ = "3.4.1.8.7"
__version__ = "3.4.1.8.8"

# import apis into sdk package
from mozart_api.api.beolink_api import BeolinkApi
Expand Down
16 changes: 12 additions & 4 deletions python_client/mozart_api/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,28 @@ class ApiClient:
_pool = None

def __init__(
self, configuration=None, header_name=None, header_value=None, cookie=None
self,
configuration=None,
header_name=None,
header_value=None,
cookie=None,
rest_client=None,
) -> None:
# use default configuration if none is provided
if configuration is None:
configuration = Configuration.get_default()
self.configuration = configuration

self.rest_client = rest.RESTClientObject(configuration)
# Add support for user-defined SSLContext through the RESTClientObject
if rest_client is None:
self.rest_client = rest.RESTClientObject(configuration)
else:
self.rest_client = rest_client
self.default_headers = {}
if header_name is not None:
self.default_headers[header_name] = header_value
self.cookie = cookie
# Set default User-Agent.
self.user_agent = "OpenAPI-Generator/3.4.1.8.7/python"
self.user_agent = "OpenAPI-Generator/3.4.1.8.8/python"
self.client_side_validation = configuration.client_side_validation

async def __aenter__(self):
Expand Down
2 changes: 1 addition & 1 deletion python_client/mozart_api/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def to_debug_report(self):
"OS: {env}\n"
"Python Version: {pyversion}\n"
"Version of the API: 0.2.0\n"
"SDK Package Version: 3.4.1.8.7".format(
"SDK Package Version: 3.4.1.8.8".format(
env=sys.platform, pyversion=sys.version
)
)
Expand Down
69 changes: 0 additions & 69 deletions python_client/mozart_api/models/beolink_experiences_request.py

This file was deleted.

18 changes: 15 additions & 3 deletions python_client/mozart_api/mozart_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from collections import defaultdict
from dataclasses import dataclass
from datetime import time
from ssl import SSLContext
from typing import Callable, Literal

from aiohttp import ClientSession
Expand All @@ -21,6 +22,7 @@
from inflection import underscore
from mozart_api.api.mozart_api import MozartApi
from mozart_api.api_client import ApiClient
from mozart_api.rest import RESTClientObject
from mozart_api.configuration import Configuration
from mozart_api.exceptions import ApiException
from mozart_api.models import Art, PlaybackContentMetadata
Expand Down Expand Up @@ -130,7 +132,7 @@ def get_highest_resolution_artwork(metadata: PlaybackContentMetadata) -> Art:
class MozartClient(MozartApi):
"""User friendly Mozart REST API and WebSocket client."""

def __init__(self, host: str) -> None:
def __init__(self, host: str, ssl_context: SSLContext | None = None) -> None:
"""Initialize Mozart client."""
self.host = host
self.websocket_connected = False
Expand All @@ -151,10 +153,20 @@ def __init__(self, host: str) -> None:
self._notification_callbacks.default_factory = lambda: None

# Configure MozartApi object.
self.configuration = Configuration(host="http://" + self.host)
self.configuration = Configuration(host=f"http://{self.host}")
self.configuration.verify_ssl = False

super().__init__(ApiClient(self.configuration))
if ssl_context is None:
super().__init__(ApiClient(self.configuration))
elif ssl_context:
super().__init__(
ApiClient(
self.configuration,
rest_client=RESTClientObject(
configuration=self.configuration, ssl_context=ssl_context
),
)
)

async def close_api_client(self) -> None:
"""Close the API ClientSession."""
Expand Down
21 changes: 12 additions & 9 deletions python_client/mozart_api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,24 @@ def getheader(self, name, default=None):

class RESTClientObject:

def __init__(self, configuration, pools_size=4, maxsize=None) -> None:
def __init__(
self, configuration, pools_size=4, maxsize=None, ssl_context=None
) -> None:

# maxsize is number of requests to host that are allowed in parallel
if maxsize is None:
maxsize = configuration.connection_pool_maxsize

ssl_context = ssl.create_default_context(cafile=configuration.ssl_ca_cert)
if configuration.cert_file:
ssl_context.load_cert_chain(
configuration.cert_file, keyfile=configuration.key_file
)
if ssl_context is None:
ssl_context = ssl.create_default_context(cafile=configuration.ssl_ca_cert)
if configuration.cert_file:
ssl_context.load_cert_chain(
configuration.cert_file, keyfile=configuration.key_file
)

if not configuration.verify_ssl:
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
if not configuration.verify_ssl:
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE

connector = aiohttp.TCPConnector(limit=maxsize, ssl=ssl_context)

Expand Down
2 changes: 1 addition & 1 deletion python_client/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "mozart_api"
version = "3.4.1.8.7"
version = "3.4.1.8.8"
description = "Mozart platform API"
authors = [
"BangOlufsen <[email protected]>",
Expand Down

0 comments on commit aace15c

Please sign in to comment.