From b625ce7b133baf07eee3c97485aa06f96950fe0b Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Wed, 25 Dec 2024 15:29:50 -0800 Subject: [PATCH 1/2] Added ContextOptions for optional settings --- switcher_client/__init__.py | 4 +++- switcher_client/client.py | 18 +++++++++++------- switcher_client/context.py | 11 +++++++++-- tests/test_client_context.py | 27 ++++++++++++++++++++++++++- 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/switcher_client/__init__.py b/switcher_client/__init__.py index 7e75958..772a6a8 100644 --- a/switcher_client/__init__.py +++ b/switcher_client/__init__.py @@ -1,5 +1,7 @@ from .client import Client +from .context import ContextOptions __all__ = [ - 'Client' + 'Client', + 'ContextOptions', ] \ No newline at end of file diff --git a/switcher_client/client.py b/switcher_client/client.py index 2958398..0ae759f 100644 --- a/switcher_client/client.py +++ b/switcher_client/client.py @@ -1,16 +1,19 @@ -from switcher_client.context import Context +from typing import Optional + +from switcher_client.context import Context, ContextOptions from switcher_client.context import DEFAULT_ENVIRONMENT class Client: - context = None + context: Optional[Context] = None @staticmethod def build_context( domain: str, - url: str, - api_key: str, - component: str, - environment: str | None = DEFAULT_ENVIRONMENT): + url: Optional[str] = None, + api_key: Optional[str] = None, + component: Optional[str] = None, + environment: Optional[str] = DEFAULT_ENVIRONMENT, + options = ContextOptions()): """ Build the context for the client @@ -19,9 +22,10 @@ def build_context( :param api_key: Switcher-API key generated for the application/component :param component: Application/component name :param environment: Environment name + :param options: Optional parameters """ - Client.context = Context(domain, url, api_key, component, environment) + Client.context = Context(domain, url, api_key, component, environment, options) @staticmethod def clear_context(): diff --git a/switcher_client/context.py b/switcher_client/context.py index ee5e7c5..940a996 100644 --- a/switcher_client/context.py +++ b/switcher_client/context.py @@ -1,9 +1,16 @@ DEFAULT_ENVIRONMENT = 'default' +DEFAULT_LOCAL = False + +class ContextOptions: + def __init__(self, local = DEFAULT_LOCAL, snapshot_location = None): + self.local = local + self.snapshot_location = snapshot_location class Context: - def __init__(self, domain, url, api_key, component, environment): + def __init__(self, domain, url, api_key, component, environment, options = ContextOptions()): self.domain = domain self.url = url self.api_key = api_key self.component = component - self.environment = environment \ No newline at end of file + self.environment = environment + self.options = options \ No newline at end of file diff --git a/tests/test_client_context.py b/tests/test_client_context.py index b583aca..662c5f9 100644 --- a/tests/test_client_context.py +++ b/tests/test_client_context.py @@ -1,8 +1,10 @@ import pytest -from switcher_client import Client +from switcher_client import Client, ContextOptions def test_context(): + """ Test building and verifying context """ + Client.build_context( domain='My Domain', url='https://api.switcherapi.com', @@ -12,10 +14,17 @@ def test_context(): try: Client.verify_context() + + assert Client.context.domain == 'My Domain' + assert Client.context.url == 'https://api.switcherapi.com' + assert Client.context.api_key == '[API_KEY]' + assert Client.context.component == 'MyApp' except ValueError as e: pytest.fail(f'Context verification failed: {e}') def test_clear_context(): + """ Test clearing context """ + Client.build_context( domain='My Domain', url='https://api.switcherapi.com', @@ -27,3 +36,19 @@ def test_clear_context(): with pytest.raises(ValueError): Client.verify_context() + +def test_context_with_optionals(): + """ Test building context with optional parameters - local and snapshot_location """ + + Client.build_context( + domain='My Domain', + options=ContextOptions( + local=True, + snapshot_location='./snapshots' + ) + ) + + options = Client.context.options + + assert options.local == True + assert options.snapshot_location == './snapshots' \ No newline at end of file From 9e1f1a10be4e71e72348d833f57e925c849a7566 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Wed, 25 Dec 2024 15:32:49 -0800 Subject: [PATCH 2/2] chore: updated SonarQube action --- .github/workflows/master.yml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml index 8fee03f..4745fa6 100644 --- a/.github/workflows/master.yml +++ b/.github/workflows/master.yml @@ -30,12 +30,8 @@ jobs: - name: Test run: pytest -v --cov=./switcher_client --cov-report xml - - name: Fix code coverage path - working-directory: ./ - run: sed -i 's@'$GITHUB_WORKSPACE'@/github/workspace/@g' coverage.xml - - name: SonarCloud Scan - uses: sonarsource/sonarcloud-github-action@master + uses: sonarsource/sonarqube-scan-action@v4.1.0 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} \ No newline at end of file